skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    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


  2. You can send list data with view using compact like:

    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',compact('list'));
            }else if (Auth::user()->role == '1') {
                return redirect('/EPage');
            }
        }
        return redirect('/');
    }
    

    then you can use $list on your blade easily.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search