• 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

send email using gmail-api and google-api-php-client

Alert message after submitting form in PHP

Dropdown with current value from Mysql

Call authenticate manually in router middleware

How to validate Envato Purchase Code in PHP

How to connect to MSSQL Server with PHP from Ubuntu 18.04?

Is it possible to change the table name in the migration file-laravel

How to get unique slug to same post-title for other time too?

best way to store error messages in Laravel (session or variable)

How to install ZeroMQ for PHP on an Alpine Linux container?

Filter WooCommerce products with post__in and additional meta queries

Laravel 4 Redirect::back() not going to previous page (refreshing current page)

How can I access an array/object?

Using spatie/media-library, how to rename a collection of uploaded files?

Reorder attribute dropdown terms in Woocommerce single variable products

About Contact Privacy policy Terms and conditions