Hello there, welcome to the random post.
Ok, I was looking to add multiple data with compact method in Laravel from controller, here is quick example solution how I did it.
My initial code was like below in one of the controller method I was working on, skipping writing whole function code, posting down what was inside the function and how I turned it out to after on my question search on Google.
Example 1:
(returning single category data to the category.edit.blade.php)
$category = Category::findOrFail($id);
return view('category.edit', compact('category','gvd', 'parentCategories'));
Example 2:
(passing multiple data)
$category = Category::findOrFail($id);
$gvd = $this->generalViewData; //this controller property returning array data to $gvd variable, then passing to compact below
$allParentCategoryIds = Category::select('parentId')->pluck('parentId');
// Looping on data
foreach ($allParentCategoryIds as $pid) {
$parentCategories = Category::select('name', 'categoryId')->where('categoryId', '=', $pid)->get();
}
return view('category.edit', compact('category','gvd', 'parentCategories'));
Here you could also note in case if come down here by searching about how to loop controller side in laravel? so from code example you could get the answer.
I am using Laravel 8 for current development.
Hope you find it easy and handful.
Thanks for reading & Happy Learning!