• 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 upload mpdf file after generating to s3 bucket in php

Laravel Collections. Is there some kind of assertStructure method?

Form repeater send wrong data with last element in Laravel

Put content of wordpress page inside div

get data-value with variable value

I need to link Google Sheet with my Laravel

Error when uploading certain .png files "Interlace handling should be turned on when using png_read_image"

Generate random username based on full name php

Laravel eloquent update record without loading from database

SilverStripe unable to populate multiple member Childs

How to render html from a @foreach loop in a textarea

How to show selected value using javascript in laravel

Updating and Synchronizing Woocommerce Subscriptions to Custom Date

Download & Save Zoom Recording in directory by PHP

PHP 8.1: strftime() is deprecated

About Contact Privacy policy Terms and conditions