我对 Laravel 很陌生
试图通过标签建立一个搜索系统,就像我有一个products
表,每个产品在product_tags
表中有多个标签。product_tags
表有两列product_id
和product_tag
$search = $req->get('search'); // Input from user
$products = Product::with(['images', 'tags'])
->where('id', 'LIKE' , '%'.$search.'%')
->orWhere('product_name', 'LIKE' , '%'.$search.'%')
->orWhere('product_price', 'LIKE' , '%'.$search.'%')
->get();
dd($products);
产品模式
class Product extends Model
{
use HasFactory;
public function images()
{
return $this->hasMany(ProductImage::class, 'product_id', 'id')->orderBy('id', 'desc');
}
public function tags()
{
return $this->hasMany(ProductTag::class, 'product_id', 'id')->orderBy('id', 'desc');
}
}