Home > OS >  I cant seem to pass value from controller to view, Laravel 8
I cant seem to pass value from controller to view, Laravel 8

Time:01-22

I can't seem to parse data that i got from db on controller to view, i have tried multiple solutions that i got from similiar question but none seem to work. i simply want to show my employee list on my admin page.

Here's my login controller The login function works just fine, its just doenst seem to parse the data i got from db to view

public function postLogin(Request $request){
        $list = "";
        $list = \DB::table('users')->where('role','1')->get();
        if(Auth::attempt($request -> only('username','password'))){
            if (Auth::user()->role == '0') {
                return view('admin',['daftar' => $list]);
            }else if (Auth::user()->role == '1') {
                return redirect('/EPage');
            }
        }
        return redirect('/');
    }

Here's my admin blade view

<thead>
        <tr>
          <th scope="col">#</th>
          <th scope="col">name</th>
          <th scope="col">email</th>
          <th scope="col">phone</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          @foreach($list as $lists)
          <th scope="row">1</th>
          <td>{{ $lists->name }}</td>
          <td>{{ $lists->email }}</td>
          <td>{{ $lists->phone }}</td>
          @endforeach
        </tr>
      </tbody>

Please help me understand my mistake, Thank you in advance.

i'm expecting the admin page with show user list with role equals to 1

CodePudding user response:

It looks like you're trying to pass the data from your controller to the view, but you're using the wrong variable name in the view. In your controller, you're passing the data to the view with the variable name "daftar", but in your view, you're trying to access the data with the variable name "list".

You should change this line in your view:

@foreach($list as $lists)
to this:


@foreach($daftar as $lists)

Also, In the view, you're also trying to use a variable name 'list' which is not defined. So, you need to change this line

<tr>
          @foreach($list as $lists)
to

<tr>
          @foreach($daftar as $lists)

You should also notice, in the controller, you're initializing the variable 'list' with an empty string, and then you're overwriting it with the data you're getting from the database. So, you don't need that initial value of list variable as an empty string.

It should look like this:

public function postLogin(Request $request){
        $list = \DB::table('users')->where('role','1')->get();
        if(Auth::attempt($request -> only('username','password'))){
            if (Auth::user()->role == '0') {
                return view('admin',['daftar' => $list]);
            }else if (Auth::user()->role == '1') {
                return redirect('/EPage');
            }
        }
        return redirect('/');
    }

That should fix the problem and allow you to access the data from the controller in your view. Make sure you clear the cache if you're still facing the problem

CodePudding user response:

After i tinker here and there, i finally found that i forgot to put an "s" on my user(s) table name. silly mistake but crucial hahaha it seems previously the variable that i parse are actually empty.

Here are my final controller that worked

public function postLogin(Request $request){
        if(Auth::attempt($request -> only('username','password'))){
            if (Auth::user()->role == '0') {
                $daftar = \DB::table('users')->where('role',1)->get();
                return view('admin',['daftar' => $daftar]);
            }else if (Auth::user()->role == '1') {
                return redirect('/EPage');
            }
        }
        return redirect('/');
    }

and here's my blade view

<tbody>
          @foreach($daftar as $lists)
        <tr>
          <th scope="row">1</th>
          <td>{{ $lists->name }}</td>
          <td>{{ $lists->email }}</td>
          <td>{{ $lists->Phone }}</td>
        </tr>
          @endforeach
      </tbody>

also thanks to waqar for correcting my previous mistakes

  • Related