Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

I think many to many implies that $performance->performers will be a collection of objects (aka array), so you cannot just call name on it even if everything works in you code.

Use vardump or print_r to look at what you getting.

Last updated 2 years ago.
0

Since this is many-to-many relationship you will get an Collection of items, not a single one.

$performance = Performances::find($id);
$performers = $performance->performers()->get();

In view
@foreach($performers as $performer)
    {{$performer->name}}
@endforeach
Last updated 2 years ago.
0

When I do what maksymcierzniak suggests, I get a error "Call to undefined method". And when I do what mikhailkozlov suggests, I don't get any performer information at all.

Last updated 2 years ago.
0

not sure if singular/plural table names in pivot table matter but i use singular form. Also the second field in your pivot table must be name of first table. so my pivot table looks like.

performance_performer id performance_id performer_id

next in your controller, after $performance = Performances::find($id); do a dd($performance->performers);

do you get an (empty) array "performers"?

have a look at this tutorial http://vegibit.com/many-to-many-relationships-in-laravel/ it uses Generators by Jeff. Way, that makes, making m2m with pivot v easy.

Last updated 2 years ago.
0

2 things:

  1. Pivot table must have ids from both tables prefixed with table names
    CREATE TABLE `performances_performers` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    `performances_id` bigint(20) DEFAULT NULL,
    `performers_id` bigint(20) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  1. By default relationships are not loaded. If you want data right ways, you have to use lazy loading
    $performance = Performances::with('performers')->find($id);

Once you do all above, $performance->performers will have Collection of object and you can loop over it to get things you need, or look at vendor/laravel/framework/src/Illuminate/Support/Collection.php to see what fun helpers you can use to get results you need.

Last updated 2 years ago.
0

Your relations are wrong, suggested key names on pivot table are wrong too. Here's what you should do:

According to the Eloquent conventions:

// table:
// performances: id, ...
// performers: id, ...
// pivot table performance_performer: id, performance_id, performer_id, timestampts
   (pivot table name is combined  snake-case model names in alphabetical order,
    foreign keys are also like snake-case model_name_id, type INT UNSIGNED)

// Performance class (singular model name)
public function performers()
{
  return $this->belongsToMany('Performer'); // class name goes here
}

// Performer class
public function performances()
{
  return $this->belongsToMany('Performance');
}

// then all works just like that:
$performance = Performance::with('performers')->first(); // eager loading
$performance->performers; // collection of Performer models

// or lazy loading:
$performance = Performance::first();
$performance->load('performers');
$performance->performers; // the same collection

// or lazy loading under the hood using dynamir properties:
$performance = Performance::first();
$performance->performers; // the same collection

// you can also eagerly / lazily load related collections on the collection instead of a single performance:

$performances = Performance::with('performers')->get(); 
// collection of Performance models, each having Collection of Performer models eagerly loaded (2 queries)

$performances = Performance::all();
$performances->load('performers'); // lazy load relation on the collection (2 queries)

// all the above works the same for the other way around of course.

Now, to make it work with you current setup:

// Performers model
public function performances()
{
    return $this->belongsToMany('Performances', 'performances_performers', 'name_id', 'performances_id');
}

// Performances model
public function performers()
{
    return $this->belongsToMany('Performers', 'performances_performers', 'performances_id', 'name_id');
}

With such relations suggested solutions will work:

$performance = Performances::with('performers')->find($id);

return View::make('viewName')->with('performance', $performance);

// view
@foreach ($performance->performers as $performer)
   {{ $performer->name }} // or whatever property you want to print
@endforeach

To sum up, I highly encourage you to change your models' names, rename pivot table's FKs and stick to the Eloquent naming conventions if you want it to make things easier for you (and if you can do that with your project). Otherwise learn those conventions in order to know when you have to override some of the defaults on you models.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

jerauf jerauf Joined 16 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.