您好我想在laravel 5.4
中为以下SQL形成一个查询:
select A.*, concat(B.firstname, ' ', B.lastname) as clientname, C.category as category from payments A, clients B, categories C where A.client_id = B.id and A.category_id = C.id
正确地给出了所有外键,并且还正确创建了模型。
我的目标是让所有主记录名称如category
,client name
等直接进入子表记录集。
答案 0 :(得分:0)
将您的行查询粘贴到DB::row()
并打印数据,如下所示
$data= DB::row("select A.*, concat(B.firstname, ' ', B.lastname) as clientname, C.category as category from payments A, clients B, categories C where A.client_id = B.id and A.category_id = C.id")->get();
dd($data);
如果您的查询是正确且妥善管理的关系而不是其检索数据
答案 1 :(得分:0)
如果您已经提出Models
而不是为什么不使用ORM?
使用模型,
payment::leftJoin('clients ','payments.client_id','=','clients.id')
->leftJoin('categories ','payments.category_id ','=','categories .id')->select(['payment.*','categories.category',DB::raw("CONCAT(clients.firstname,' ',clients.lastname) as clientname")])->get();
答案 2 :(得分:0)
你可以试试这个:
DB::table('payments')
->select('payments.*',DB::raw('CONCAT(clients.firstname," ",clients.lastname) as clientname'),categories.category)
->join('clients','clients.id','=','payments.client_id')
->join('categories','categories.id','=','payments.category_id')
->get();
希望对你有所帮助!!!