• 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

Download & Save Zoom Recording in directory by PHP

Method IlluminateAuthRequestGuard::logout does not exist Laravel Passport

WordPress Subquery returns more than 1 row on SELECT

Doctrine 2 mapping referencing unique key

How to write PHP in XSLT

Cache clear probem in Larave

Create a new line whenever an array value reaches more than 10 characters

Open a popup box after receiving result from ajax

carbon generated datetime not stored correctly into the database

Which is faster php date functions or carbon?

PHP my timezone is no setting up in PHP.ini File in xampp

Why do I have invalid argument supply for foreach in Laravel for json response? [duplicate]

How to Make Laravel Eloquent "IN" Query?

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given [duplicate]

Cross-Origin Resource Sharing (CORS) witth Laravel and Vuejs

About Contact Privacy policy Terms and conditions