• 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

Search for array row with specific column value and return another value from qualifying row

Access relation of pivot table in Laravel

PHP7 CLI attempts to load xdebug twice - "Cannot load Xdebug - it was already loaded"

Laravel You requested 1 items, but there are only 0 items available

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

Laravel unique validation on multiple columns

Display the WooCommerce product price inside a custom function

laravel controller function parameters

Transpose and flatten multiple rows of array data [duplicate]

Laravel Livewire: jQuery not working in child component loaded via @if statement

Laravel 8: Array to string conversion while calling route:list

Star and Half Star Rating in Laravel

Laravel 5 controller sending JSON integer as string

generate an Excel file using PHP

Match csv filenames to table names and import

About Contact Privacy policy Terms and conditions