0

I have a relation of category and sub category, I'm getting category data with related sub category using this query:

$data=Category::with('subcategory')->find($categoryID);

now i' getting category with related sub categories, that sub categories array i want to populate in the dropdown, Is there any way so that i can populate sub categories data without looping.

 {{ Form::select('subcategory[]',$subcat,[],['class'=>'form-control','multiple'=>true]) }}

1 Answer 1

1

In your controller you will need fetch sub categories ids like so

$sub_categories = $data->subcategory()->pluck('id')->toArray();

now you will pass $sub_categories to your view and in your form select

{{ Form::select('subcategory[]',$sub_categories,[],['class'=>'form-control','multiple'=>true]) }}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, i have tried but it contains array of data:[{"id":3,"sub_cat_id":2,"s_id":1,"created_at":"2020-07-17 16:48:07"},{"id":4,"sub_cat_id":2,"s_id":2,"created_at":"2020-07-17 16:48:07"}], i want to pass sub_cat_id in the third parameter so to make that sub category checked

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.