• 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 find phpcs current default standard

Laravel Route issues with Route order in web.php

Move a child array to parent array and change parent/child name

Use same method for inertia response and json response Laravel Jetstream

Form repeater send wrong data with last element in Laravel

Detect if PHP session exists

get attribute from class not working php 7

array_key_exists(): The first argument should be either a string or an integer

How can I format this number correctly using PHP?

Can't exclude directories from .htaccess mobile redirect?

Laravel query builder binding parameters more than once

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given [duplicate]

How to display MySQL table using php and edit it in a web browser

getting error while enter Command => php artisan route:list

How to add a sidebar to Woocommerce Shop Page?

About Contact Privacy policy Terms and conditions