试图从表“ mobiles”及其在Stock中的相关条目获取数据。当我的搜索关键字是移动设备型号时,请参见下面的“移动设备”表
和相关表格“股票”也在下面
移动型号为:
class Mobile extends Model
{
public function company(){
return $this->hasMany(Company::class);
}
public function stocks(){
return $this->hasMany(Stock::class);
}
}
股票模型为:
class Stock extends Model
{
public function company(){
return $this->belongsTo(Company::class, 'company_id');
}
public function mobiles(){
return $this->belongsTo(Mobile::class, 'model_id');
}
}
股票的索引方法是:
public function index(Request $request)
{
$request->session()->put('search', $request
->has('search') ? $request->get('search') : ($request->session()
->has('search') ? $request->session()->get('search') : ''));
$request->session()->put('field', $request
->has('field') ? $request->get('field') : ($request->session()
->has('field') ? $request->session()->get('field') : 'model_id'));
$request->session()->put('sort', $request
->has('sort') ? $request->get('sort') : ($request->session()
->has('sort') ? $request->session()->get('sort') : 'asc'));
$stocks = new Stock();
$stocks = $stocks->where('model_id', 'like', '%' . $request->session()->get('search') . '%')
->orderBy($request->session()->get('field'), $request->session()->get('sort'))
->paginate(5);
if ($request->ajax()) {
return view('pages.stock.index_stock', compact('stocks'));
} else {
return view('pages.stock.ajax', compact('stocks'));
}
}
问题是我可以使用模型ID进行搜索,因为在“股票”表的“模型ID”列上设置了搜索过滤器。如何编写表“ mobiles”中存在的移动模型名称进行搜索,搜索模型后,它应显示与搜索到的移动模型相关的完整信息。
谢谢。