Hi, I need to create a hasmany relationship, but unfortunately I have a problem.
The tables are composed as follows:
Giochi:
id - name - developer
1 - Call of Duty - 1
2 - Assassin's Creed - 2,3,4
Developer:
id - name
1- Activision
2 -Ubisoft Quebec
3 -Ubisoft Montreal
4 -Ubisoft
Since I put commas in developer ids, I had to do an "explode". So far I have always done so in the controllers:
$take_devs = explode(',', $giochi->developer);
$list_dev = array();
foreach($take_devs as $value)
{
$list_dev[] = Developer::where('id', $value)->first();
}
Unfortunately, however, now, I should create a hasmany relationship to create a query
Developer::with('giochi')->get();
I tried with:
Models: Giochi
public function devs()
{
$dev = Developer::where('slug', $this->slug)->first();
$giochi = Giochi::all();
$take_devs = explode(',', $this->developer);
$list_dev = array();
foreach($take_devs as $value)
{
$list_dev[] = Developer::where('id', $value)->first();
}
}
Controller
$dev = Giochi::with('devs')->get();
But I get the error: Call to a member function addEagerConstraints() on null...
What is the most practical way to create a relationship between two tables when you have multiple column values?