• 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

Handle error for duplicate entries - PHP/MySQL

Form repeater send wrong data with last element in Laravel

PhP how to calculate moments with variables rows

How to fix this error : Ask your hosting provider to use PHP 7.2.5 or higher for both HTTP and CLI?

Connecting an HTML webpage to a SQL Server

Laravel eloquent update record without loading from database

Laravel : How to Create Dropdown to Select FOREIGN KEY from Other Table?

MySQL - Connection failed: No route to host

How can i hide dt if dd got empty value

PHP rotate matrix counter-clockwise

How can I get data from PHP to Android TextView?

How to fix " Uncaught TypeError: Argument 1 passed to JpGraphException::defaultHandler() " problem in php 7.1.3 and jpgraph 4.2.6?

Querying only one row from a one to many relationship laravel

How to KeyBy where multiple items have the same key

How to make a foreign key not using primary key

About Contact Privacy policy Terms and conditions