Answer the question
In order to leave comments, you need to log in
How to properly implement database binding?
There is a table work and work_type. A work can have multiple work_types, the garter is called one-to-many.
table:
work work_type
id id
work_type_id name
name_work
Controller :
public function index(WorkType $wk)
{
$works = $wk->has();
dd($works);
}
class WorkType extends Model
{
protected $table = 'work_type';
public function has()
{
return $this->hasMany('App\Work')->get();
}
}
Answer the question
In order to leave comments, you need to log in
In the description, you just need $this->hasMany('App\Work'), without calling ->get();
If you then access the relation as a method (with parentheses) ->has() - the Query Builder will be returned and you can set additional parameters like where - orderBy. To get the data, you need to access the connection as an attribute
$works = $wt->has;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question