title: Trying to get property of non-object laravel 5.3, index.blade
body: <p>I am stuck with this error in Laravel: </p>
  
  <blockquote>
    <p>Trying to get property of non-object (View: C:...\resources\views\admin\users\index.blade.php)</p>
  </blockquote>
  
  <p>Is anything wrong with my code?</p>
  
  <p>This My index</p>
  
  <pre><code>@extends('layouts.admin')
  
  
  @section('content')
  
      &lt;h1&gt;Users&lt;/h1&gt;
  
  
      &lt;table class="table"&gt;
          &lt;thead&gt;
          &lt;tr&gt;
              &lt;th&gt;Id&lt;/th&gt;
              &lt;th&gt;Name&lt;/th&gt;
              &lt;th&gt;Email&lt;/th&gt;
              &lt;th&gt;Role&lt;/th&gt;
              &lt;th&gt;Status&lt;/th&gt;
              &lt;th&gt;Created&lt;/th&gt;
              &lt;th&gt;Updated&lt;/th&gt;
              &lt;th&gt;Photo&lt;/th&gt;
          &lt;/tr&gt;
          &lt;/thead&gt;
          &lt;tbody&gt;
  
          @if($users)
  
              @foreach($users as $user)
  
          &lt;tr&gt;
              &lt;td&gt;{{$user-&gt;id}}&lt;/td&gt;
              &lt;td&gt;{{$user-&gt;name}}&lt;/td&gt;
              &lt;td&gt;{{$user-&gt;email}}&lt;/td&gt;
              &lt;td&gt;{{$user-&gt;role-&gt;name}}&lt;/td&gt;
              &lt;td&gt;{{$user-&gt;active == 1 ? 'Active' : 'Not Active'}}&lt;/td&gt;
              &lt;td&gt;{{$user-&gt;created_at-&gt;diffForHumans()}}&lt;/td&gt;
              &lt;td&gt;{{$user-&gt;updated_at-&gt;diffForHumans()}}&lt;/td&gt;
              &lt;td&gt;{{$user-&gt;photo_id}}&lt;/td&gt;
          &lt;/tr&gt;
                  @endforeach
              @endif
          &lt;/tbody&gt;
      &lt;/table&gt;
  @stop
  </code></pre>
  
  <p>This my Controller</p>
  
  <pre><code>public function index()
      {
          //
          $users = User::all();
  
          return view('admin.users.index', compact('users'));
      }
  
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create()
      {
          //
          $roles = Role::pluck('name','id')-&gt;all();
  
          return view('admin.users.create', compact('roles'));
      }
  
      /**
       * Store a newly created resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\Response
       */
      public function store(UsersRequest $request)
      {
              //Buat bikin create dengan method ini
  
          User::create($request-&gt;all());
  
          return redirect('/admin/users');
  
  //        return $request-&gt;all();
      }
  
      /**
       * Display the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function show($id)
      {
          //
          return view('admin.users.show');
      }
  
      /**
       * Show the form for editing the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function edit($id)
      {
          //
          return view('admin.users.edit');
      }
  
      /**
       * Update the specified resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function update(Request $request, $id)
      {
          //
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function destroy($id)
      {
          //
      }
  }
  </code></pre>
  
  <p>This my Request</p>
  
  <pre><code>class UsersRequest extends FormRequest
  {
      /**
       * Determine if the user is authorized to make this request.
       *
       * @return bool
       */
      public function authorize()
      {
          return true;
      }
  
      /**
       * Get the validation rules that apply to the request.
       *
       * @return array
       */
      public function rules()
      {
          return [
              //
              'name'=&gt;'required',
              'password'=&gt;'required',
              'email'=&gt;'required',
              'active'=&gt;'required',
              'role_id'=&gt;'required',
              'photo_id'=&gt;'required|image|mimes:jpeg,png,jpg,gif,svg|max:7098',
          ];
      }
  }
  </code></pre>