• 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

Wordpress 'post_type_link' hides permalink

Split array into 4-element chunks then implode into strings

Best way to scrolldown onpageload

CSV to PHP class properties

How to put php code inside opening and closing shortcodes

Fatal error: Uncaught mysqli_sql_exception: Unknown database 'test' in ... (How do I fix that? Using PHP)

401 Unauthorized only occurring on some pages in Laravel 8

Laravel Unknown Column 'updated_at'

How to get Laravel's CSRF Token from Another Website?

Dynamic dropdown Ajax PHP request

target [LaravelFortifyContractsRegisterViewResponse] is not instantiable

How to programmatically find public properties of a class from inside one of it's methods

ldap is missing from system when installing adldap2 in laravel

Combine array with same value and add other [duplicate]

Creating live search with AJAX and CodeIgniter

About Contact Privacy policy Terms and conditions