doproj123
1 week ago

get value by model with relation 'hasMany'

Posted 1 week ago by doproj123

Hello Is have two models which looks like this:

class Producents extends Model{

...

public function artists(){
        return $this->hasMany('App\Artists', 'producer_id', 'producer_id');
    }
...

}
class Artists extends Model{

...

public function producents(){
        return $this->belongsTo('App\Producents', 'producer_id', 'producer_id');
    }
...

}

How I can get sum of artists which have same Producents? I tried something like this in my controller

class ProducentsController extends Controller{

public function show($id)
{
    $producent = Producents::find($id);
    return view('producents.show')->with('producent', $producent);
}

}

And in my view trying something like this

{{$producent->artists->name}}

But it not works. Is it even possible?

Please sign in or create an account to participate in this conversation.