• 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

CSV to PHP class properties

How should I get started using PHPUnit as a testing framework for a bunch of functions and classes I have already made?

Extract house numbers from address string

How to loop sql table data inside a html table

how to loop through json response data using ajax jquery?

PHP web3 ERC20 token function call

PHPSpreadsheet - How Do I Use Auto Migration Tool

MySQL default time format UTC or GMT?

Xdebug 3 not showing in phpinfo on m1 Monterey

How to fix this error : Ask your hosting provider to use PHP 7.2.5 or higher for both HTTP and CLI?

I want to display a sweetalert after the inserting of data in my database

(Laravel) How to delete multiple models including optional relationships?

Download & Save Zoom Recording in directory by PHP

Laravel - Browser displays message again when I press back button

virtctl works when executed via command line but not from php exec()

About Contact Privacy policy Terms and conditions