• 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 do I get friend list from Friends table with counts of friends of my friends

oauth-private.key does not exist or is not readable

Reload parent page after submit in iframe

how to fix Service provider class not found when using repository?

What is my SQL missing?

Creating command to backup MySql database in Laravel

How to delete old images from public folder on update using Laravel

PHP drop down list using array's and foreach (else and for) code

*** ERROR *** The PHP configuration loaded file is: c:/wamp64/bin/php/php8.1.0/php.ini - should be: c:/wamp64/bin/apache/apache2.4.53.1/bin/php.ini

Why does array_map() with null as callback create an "array of arrays"?

Combine multiple columns into an array as one of the key in a results set

php 7 php.ini upload_max_filesize not working

Using Associative arrays

How do I set the maximum php memory limit

Auto increment id JSON

About Contact Privacy policy Terms and conditions