• 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 fix " Uncaught TypeError: Argument 1 passed to JpGraphException::defaultHandler() " problem in php 7.1.3 and jpgraph 4.2.6?

Target Laravelista is not instantiable

Sort multidimensional array by column value within a column

Post Form Data To phpmyadmin Database

PDOException SQLSTATE[HY000] [2002] No such file or directory

Fetch files from next cloud storage and display in Laravel website

PHPExcel_IOFactory::createWriter causes wrong behaviour

Target class controller does not exist - Laravel 8

What is PHP's equivalent of JavaScript's "array.every()"?

Reducing authentication calls on external API (Laravel 5.6)

How to keep value of selected value after form submission?

Unable to Edit config.inc.php

Load a .env file with PHP

SQLSTATE[HY000]: General error: 1 table posts has no column named *

Insert database rows from columns of data from associative array of indexed arrays

About Contact Privacy policy Terms and conditions