Hi fellow StackOverflow users!
I'm struggling on a pretty simple task, and I'd be very greetful if one of you could help me out. I have 3 models with nested Many To Many Relationship.
My first model is Wave that has a M2M relationship with Employee :
Wave.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Wave extends Model
{
public function employees()
{
return $this->belongsToMany('App\Employee');
}
Employee that has a Many To Many with Wave and another Many To Many with Expertise
Employee.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Employee extends Model
{
public function waves()
{
return $this->belongsToMany('App\Wave');
}
public function expertises()
{
return $this->belongsToMany('App\Expertise');
}
Finally my Expertise model with a many to many with Employee:
Expertise.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Expertise extends Model
{
public function employees()
{
return $this->belongsToMany('App\Employee');
}
What I'd like to achieve here is create a custom property on Wave to get all distinct expertises that employees assigned to this wave possess.
It would look a one dimensional collection of distinct expertises.
It doesn't seem very complicated but I tried many things and can't get it working.
Last thing I tried was querying Expertise model using with and withPivotIn but without success:
Wave.php
public function getExpertisesAttribute()
{
$employees = $this->employees;
$expertises = Expertise::with('employees', function($q) use($employees) {
$q->wherePivotIn('employee_id', $employees->pluck('id')->toArray());
});
return $expertises;
}
I'm getting an error while running this query:
PHP Warning: mb_strpos() expects parameter 1 to be string, object given in /Users/Kaz/lab/MyProject/vendor/laravel/framework/src/Illuminate/Support/Str.php on line 107
FYI i'm using Laravel 5.8 on PHP 7.2
Thank you very much for reading this and I hope you'll be able to give me some help because I'm feeling a bit stuck and still it seems like an easy thing to do !
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire