• 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 to sort by a custom appended relation to model

XML to CSV with PHP converter [problem with images grabing]

Image upload not working through ajax Laravel

Remove product downloads section in woocommerce email notifications

Sagepay Error The Vendor failed to provide a RedirectionURL

Dropzone: Submit both form data and dropzone at once

Sum array values

Select option menu read from database and use it's values

Target Laravelista is not instantiable

Having issue with matching rows in the database using PDO

php mysql + create a friend request system

Laravel - Implicit route model binding with soft deleted data

Error while importing CSV file in database

How to get result of the formula on import xlsx with maatwebsite using Laravel? I get formula and not value of the formula

Call to a member function givePermissionTo() on null

About Contact Privacy policy Terms and conditions