Php 如何使用req连接表->;身份证件
我希望能够组合两个查询语句,从位置表($detail)中获取信息,如链接请求的id($tour)。由于您使用模型,因此可以使用雄辩的关系来加载相关数据。首先,在Php 如何使用req连接表->;身份证件,php,laravel,Php,Laravel,我希望能够组合两个查询语句,从位置表($detail)中获取信息,如链接请求的id($tour)。由于您使用模型,因此可以使用雄辩的关系来加载相关数据。首先,在巡更模型中: public function getTourDetail(Request $req) { //Get link detail $tour = Tour::where('id',$req->id)->first(); //I want to take location.city of th
巡更
模型中:
public function getTourDetail(Request $req)
{
//Get link detail
$tour = Tour::where('id',$req->id)->first();
//I want to take location.city of the location table
$detail = Tour::join('location','tour.id_location','=','location.id')
->whereColumn([
['tour.id_location','=','location.id']
])
->get(array(
'tour.id as id_tour',
'location.image',
'tour.name',
'tour.id_location',
'location.city'
));
return view('page.tour-detail',compact('tour','detail'));
}
然后加载Tour
并获取相关位置:
public function location()
{
return $this->belongsTo(Location::class, 'id_location')
}
第一件事,若你们使用的是模型,那个么使用雄辩的关系将是一个更好的办法来处理你们这样的情况。但是,如果您想加入您的表,则以下是方法:
$tour = Tour::find($req->id);
$relatedLocation = $tour->location;
注意:如果您从提交的表格中获得id
,则将代码的第一部分替换为:-
public function getTourDetail($id)
{
$tour = Tour::where('id',$id)->first();
//I want to take location.city of the location table
$detail = DB::table('location')
->join('tour','tour.id_location','=','location.id')
->select(
'tour.id as id_tour',
'location.image',
'tour.name',
'tour.id_location',
'location.city'
)->get();
return view('page.tour-detail',compact('tour','detail'));
}
public function getTourDetail(Request $request)
{
$tour = Tour::where('id',$request->id)->first();