A
A
Alexander2015-06-25 19:54:16
Laravel
Alexander, 2015-06-25 19:54:16

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);
    }

Work Model:
class WorkType extends Model
{
    protected $table = 'work_type';

    public function has()
    {
        return $this->hasMany('App\Work')->get();
    }
}

all two tables are filled with two records
but the browser shows
<?php
Collection {#304 ▼
#items: []
}
?>
Why ? what am I doing wrong?
PS methods and variables are not logically set, I know.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vyacheslav Plisko, 2015-06-25
@AmdY

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 question

Ask a Question

731 491 924 answers to any question