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.
(returning single category data to the category.edit.blade.php)
$category = Category::findOrFail($id);
return view('category.edit', compact('category','gvd', 'parentCategories'));
(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!
Break! I didn’t plan it. One day I just didn’t feel like opening Instagram—and then…
AI tools Let’s be real—AI sounds like either a robot apocalypse or something only tech…
Summer vacation is a great time for kids to explore, have fun, and learn new…
Goal: Understand transformers, large language models, and the architecture behind ChatGPT. Tutorial Suggestions: ✅ “Transformers…
Goal: Build apps or tools with ChatGPT or GPT-4 API. Tutorial Suggestions: ✅ OpenAI API…
Goal: Learn how to prompt better, write content, brainstorm, code, etc. Tutorial Suggestions: ✅ OpenAI's…