My schema is as 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 to a specific category and the category belongs to a specific channel.
my question is:
how can i search all the articles with the category name and channel name (the relationship is ready in my code)?
i have tried
$articles = App\Article::latest()->with('category')->with('channel')->get();
but it does not work, anyone who can help me? thank you for your time.
If you want to search through the related tables you should use joins like 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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.