• 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

Elasticsearch - Want to sort by field in all indices where that particular field available or not if not then avoid it

Dynamic dropdown Ajax PHP request

Create tags in laravel post publishing

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

Laravel Database Strict Mode

I want to display a sweetalert after the inserting of data in my database

how to see if database exists with PDO [duplicate]

Cant seem to get the Pagination to work on my WooCommerce REST API application?

Reducing authentication calls on external API (Laravel 5.6)

definer/invoker of view lack rights to use them

backup files to google drive using PHP

Can't exclude directories from .htaccess mobile redirect?

PhpSpreadSheet: How to save Workbook sheets in individual CSV files

Laravel Carbon throws error The format separator does not match

Nginx RTMP not recording

About Contact Privacy policy Terms and conditions