0

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:

Error message

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>
2
  • 1
    please add the error in detail Commented Jul 25, 2018 at 1:59
  • I add Image On my Error...........Please Check It Commented Jul 25, 2018 at 2:05

5 Answers 5

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

0

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);

1 Comment

or just change your controller to this return view('admin.pages.manage_category')->with('data',$all_category);
0

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 !!!

Comments

0

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

Comments

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.