$maincategory = Category::all(['id', 'category']);
$maintable = Category::orderBy('id', 'DESC')->get();
$subcategory = Subcategory::all(['id', 'subcategory']);
$subtable = Subcategory::orderBy('id', 'DESC')->get();
return view('admin.news.create', compact('maincategory', $maincategory, 'maintable','subcategory',$subcategory,'subtable'));
am getting error on this
return view('admin.news.create', compact('maincategory', $maincategory, 'maintable','subcategory',$subcategory,'subtable'));
how to solve it?
CodePudding user response:
The error says: compact: must be string or string of Array, so, if your variables
are more than one, pass it this way:
return view('admin.news.create', compact(
['maincategory',
'maintable',
'subcategory',
'subtable'
]
);
But if your variable
is just one, then thats when you use string
return view('admin.news.create', compact('maincategory'));
CodePudding user response:
You can not use query result collection in compact
.
return view('admin.news.create', compact('maincategory','subcategory'));
Here compact('maincategory', ...)
will send $maincategory
and subcategory
will send $subcategory
to the view admin.news.create
.
You can also use with() like this
return view('admin.news.create')->with('maincategory',$maincategory)->with('subcategory',$subcategory)
You can access it like this in your view blade
foreach($maincategory as $category)
{
// Code
}
foreach($subcategory as $s_category)
{
// Code
}