• Categories
    • PHP
    • phpMyAdmin
    • PHPMailer
    • FFMpeg
    • PHPEXcel
    • PHPDoc
    • PHPUnit
    • CakePHP
    • CakePHP 2.0
    • Cake PHP 2.1
    • CakePHP Model
    • Facebook PHP SDK
    • composer-php
    • PHP 7
    • PHP GD
    All Categories
  • About

How To Access A Column In M-M Relationship Table In Laravel

phpmysqllaravelormlaravel-9


PHP Snippet 1:

public function announcements() {
   return $this->belongsToMany(User::class, 'announcements')->withPivot('message');
}

PHP Snippet 2:

public function announcements() {
    return $this->belongsToMany(Event::class, 'announcements')->withPivot('message');
}

PHP Snippet 3:

    $ann = [];
    foreach ($event->announcements as $a) {
        $ann[] = [
            "name" => $a->name,
            "announcement" => $a->pivot->message,
            "created_at" => $a->pivot->created_at,
        ];
    }

PHP Snippet 4:

$event->announcements->pivot->message;

Related Snippets

Get lat/lon from google maps url ftid (hex)

How to give apache permission to write to home directory?

Add New Parameter to Existing URL using htaccess

LARAVEL: How to fetch id dynamically in a query builder?

Laravel eloquent update record without loading from database

MySQL upgrade causing unexpected results on simple WHERE clauses

PHP Download MP3 files from directory on server

Laravel multi auth - Authentication user provider [] is not defined

How to get the survey List from surveygizmo API in the Postman?(Trial Version)

Handling expired token in Laravel

Reorder attribute dropdown terms in Woocommerce single variable products

Unable to get password for the instance created from AMI

Timber: Single post pagination does not work (wp_link_pages)

Add and update products to session cart in Laravel

CodeIgniter 3 is generating a session file on each request, why?

About Contact Privacy policy Terms and conditions