I have two entities in laravel 5 Eloquent, Foo and FooType with oneToMany relation.
class Foo {
public function fooTypes(){
return $this->hasMany('App\FooType');
}
}
and FooType
class FooType{
public function foo(){
return $this->belongsTo('App\Foo');
}
}
question 1: how can I query using eloquent query builder and return Foos that has type_id
(this column is in FooType table) of 5;
something I tried:
Foo::fooTypes()->where('type_id', 5);
and any link for good tuts about queries like this.
question two: I find out this is hard to use Eloquent query builder is it bad practice to use normal laravel queries with DB, I mean by using db I cannot use orm or something like that(or what is the benefits of using eloquent query builder):
$foos = DB::table('foos')
->join('fooTypes', 'foo_id', '=', 'foos.id')
->where('fooTypes.type_id', '=', '5')
->select('foos.*')
->get();
this type of queries are much easy and there more tutorials about it.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire