im pretty new in using this so im stuck where i needed to display the output the result of a certain query as a dropdown in my view. In my controller, here is the query
//categories
$cats = DB::table('nsa_subcategory AS sc')
->join('nsa_maincategory AS mc' , 'sc.maincategoryid' , '=' , 'mc.maincategoryid')
->select('sc.subcategoryid' , 'sc.subcategoryname' , 'mc.maincategoryname')
->get();
// dd($cats);
i needed to display the subcategoryname
and maincategoryname
in the dropdown and get the value of subcategoryid
im stuck in here. any ideas on how to implement this? thank you so much in advance!
Try this
controller
$cats = DB::table('nsa_subcategory AS sc')
->join('nsa_maincategory AS mc' , 'sc.maincategoryid' , '=' , 'mc.maincategoryid')
->select('sc.subcategoryid' , 'sc.subcategoryname' , 'mc.maincategoryname')->lists('id', 'categorie name')
; // change this with the right data , the first wil be the value the second will be displayed
View
foreach ($cats as $maincat => $subcat) {
echo $subcat;
}
Dropdownlist
{{ Form::select('size', $cats,'Choose category') }}
even if dosent work, the essential part is the ->lists()
function, adding that to the query will prepare the data to be diplayed in a dropdown if you faced any kind of error , Post a comment and i will update the answer if needed
the said query will give you a Collection Object, but what you need instead is an array .. of key=>value pairs. The following should do:
$cats = DB::table('nsa_subcategory AS sc')
->join('nsa_maincategory AS mc' , 'sc.maincategoryid' , '=' , 'mc.maincategoryid')
->lists(DB::raw('CONCAT(sc.subcategoryname," " , mc.maincategoryname) AS cat_name'),'cat_id');
This will give you the desired result. And if you're using Laravel's form helper, you can directly pass it like this:
{{Form::select('name',$cats)}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.