• 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

auth pages not getting css in laravel

How to get monthly wise data in laravel

how to redirect the user back to desired URL after login page in PHP?

Clear javascript source cache laravel 5.8

SQLSTATE[HY000]: General error: 1 table posts has no column named *

Parsing JSON File to array in PHP

Passing a boolean value from checkbox in Laravel form

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

Error converting docx to pdf using Unoconv

Insert And Retrieve Data in MySQL with $.post Noob Question

Sentry on Symfony: how to exclude `NotFoundHttpException`

WordPress + Nginx on non-standard port behind AWS ELB results in broken links

add_action() function in wordpress not working [duplicate]

How can I pass the list to the component variable in Laravel?

Check for PDO Results, If None Display Message, If Yes then Loop Through

About Contact Privacy policy Terms and conditions