• 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

How can I resolve "Your requirements could not be resolved to an installable set of packages" error?

Limit login attempts in Laravel 5.7

How to solve Duplicate entry '0' for key 'PRIMARY' in Wordpress?

How to use a PHP MVC Controller, one thats invoked by calling a URL, to call and execute more than one function? [duplicate]

php code to send checkbox form results to email

Laravel Command Schedule Not Working Properly

wordpress function breaks wp-admin

What will the best solution for this multiple optional filter?

How to write PHP in XSLT

CodeIgniter extend CI_URI undefined method

How Can I Do LIMIT 1, 2 In WP_Query

getting error while enter Command => php artisan route:list

Make Calculator - When Button clicked the No shows in Textbox.

Required field only if another field has a value, must be empty otherwise

How to find the date a user created their Google account

About Contact Privacy policy Terms and conditions