Hi I'm having trouble getting data from database after adding a new function inside my controller. Can anyone explain to me this error and what should I do?
I'm using this video as a reference and want to do something like his end result (see 1:09:28 of video), but I get this error:
public function manageCategory() {
$this->authCheck();
$all_category = DB::table('category')->get();
$manage = view('admin.pages.manage_category')->with('data', $all_category);
return view('admin.pages.manage_category')->with('main_content', $manage);
}
<tbody>
@foreach($data as $vData)
<tr>
<td><a href="#">{{ $vData->category_id }}</a></td>
<td class="hidden-phone">{{ $vData->category_name }}</td>
<td>{{ $vData->category_description }} </td>
<td><span class="label label-important label-mini">{{ $vData->category_published }}</span></td>
<td>
<button class="btn btn-success"><i class="icon-ok"></i></button>
<button class="btn btn-primary"><i class="icon-pencil"></i></button>
<button class="btn btn-danger"><i class="icon-trash "></i></button>
</td>
</tr>
@endforeach
</tbody>
Try removing last line and move return keyword like this:
public function manageCategory() {
$this->authCheck();
$all_category = DB::table('category')
->get();
return view('admin.pages.manage_category')
->with('data',$all_category);
}
I do not get why there have 2 views called. Maybe u or video maker did mistake
You should change your code to this
public function manageCategory() {
$this->authCheck();
$all_category = DB::table('category')->get();
return view('admin.pages.manage_category')->with('data',$all_category);
}
Your foreach should be like this
@foreach($main_content['data'] as $vData)
this is because in your controller you $data contains the category but you have assign data to '$manage'. And again you assign manage to another variable main_content
->with('main_content', $manage);
I'm not sure what you are trying to do here but this code will help you to fix your an error.
public function manageCategory() {
$this->authCheck();
$all_category = DB::table('category')->get();
view()->share('data',$all_category);
view()->share('main_content',$manage);
$manage = view('admin.pages.manage_category');
return view('admin.pages.manage_category');
}
View class Share method will help you to share variable to view. Good Luck !!!
I think this code is suite for you..
public function manageCategory() {
$this->authCheck();
$data = DB::table('category')
->get();
return view('admin.pages.manage_category')
->with('data', $data); //Directly pass data to view file
}
In your blade file ....
@if(!empty($data)) // add if
@foreach($data as $vData)
<tr>
<td><a href="#">{{ $vData->category_id }}</a></td>
<td class="hidden-phone">{{ $vData->category_name }}</td>
<td>{{ $vData->category_description }} </td>
<td><span class="label label-important label-mini">{{ $vData->category_published }}</span></td>
<td>
<button class="btn btn-success"><i class="icon-ok"></i></button>
<button class="btn btn-primary"><i class="icon-pencil"></i></button>
<button class="btn btn-danger"><i class="icon-trash "></i></button>
</td>
</tr>
@endforeach
</tbody>
@endif ////add end if