• 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

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

PHP and WebView - Cookie doesn't are the same

Require a class from another class - php

RuntimeException: Unable to create the cache directory (/var/www/sonata/app/cache/dev)

Update Order custom Field value with WooCommerce REST API

PHP array stringify

Laravel Livewire: jQuery not working in child component loaded via @if statement

How can I get new CSRF token in LARAVEL by using ajax

SMTP ERROR: MAIL FROM command failed: 530 5.7.0 Must issue a STARTTLS command first when using PHPMailer

Laravel 5.1 how to use {{ old('') }} helper on blade file for radio inputs

Limit WooCommerce products in cart only from one custom taxonomy

PHP: Insert marker every 3 iterations

CONCAT columns with Laravel 5 eloquent

Detect a fetch request in PHP

How do I call Validator from a namespace with an already existing Validator class

About Contact Privacy policy Terms and conditions