问题描述:

hi so im trying to create a dropdown that is based form a sql joined query so far here is what i have (got a help from fellow stackoverflow-ers)

$cats = DB::table('nsa_subcategory')

->join('nsa_maincategory' , 'nsa_subcategory.maincategoryid' , '=' , 'nsa_maincategory.maincategoryid')

->lists(DB::raw('CONCAT(nsa_subcategory.subcategoryname , " | ", nsa_maincategory.maincategoryname)'),'nsa_subcategory.subcategoryid');

what im trying to do was join 2 tables display the subcategory and maic category but the value i would get is the subcategory id the code above produces this kind of error

any ideas what im doing wrong or any ideas on how to improve my code? thanks so much in advance!

网友答案:

Instead of putting the DB::raw in the ->lists, put it in your select and give it a name as in the code below category, then you retrieve it using ->lists

$cats  = DB::table('nsa_subcategory')
           ->select(DB::raw('CONCAT(nsa_subcategory.subcategoryname , " | ", nsa_maincategory.maincategoryname) AS category'),'nsa_subcategory.subcategoryid')
           ->join('nsa_maincategory' , 'nsa_subcategory.maincategoryid' , '=' , 'nsa_maincategory.maincategoryid')
           ->lists(category, subcategoryid);
相关阅读:
Top