• 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

PHP using str_starts_with for array to exclude same as with wildcard

laravel automatically deletes server.php on php artisan serve

Laravel Collections. Is there some kind of assertStructure method?

How to fix bootstrap multiselect search data using ajax

Laravel get user data with profile

Live search query using JS and PHP for QA forum

str_word_count() function doesn't display Arabic language properly

How do I call Validator from a namespace with an already existing Validator class

Laravel - Browser displays message again when I press back button

Remove categories with all childs derived from parent category

Correct way to use LIKE '%{$var}%' with prepared statements?

PHP array stringify

strpos(): Argument #1 ($haystack) must be of type string, array given

Send Outlook 2010 email using PHP

ORA-12546: TNS:permission denied error connection to remote oracle server

About Contact Privacy policy Terms and conditions