php - how can i use relationship in laravel? -
my schema follows:
channel table: id int unsigned primary key auto increment, name varchar(30) not null, ... category table: id int unsigned primary key auto increment, channel_id int unsigned index, name varchar(30) not null, ... article table: id int unsigned primary key auto increment, category_id int unsigned index, title varchar(90) not null, content text not null, ...
so, every article belong specific category , category belongs specific channel.
my question is:
how can search articles category name , channel name (the relationship ready in code)?
i have tried
$articles = app\article::latest()->with('category')->with('channel')->get();
but not work, can me? thank time.
if want search through related tables should use joins this:
$articles = app\article::latest() ->select('article.*') ->join('category', 'category.id', '=', 'category_id') ->join('channel', 'channel.id', '=', 'channel_id') ->where('category.name', 'like', "%{$name}%") ->orwhere('channel.name', 'like', "%{$name}%") ->groupby('article.id') ->get();
Comments
Post a Comment