title: Trying to get property of a non-object problem in laravel
body: <p>I am working on a laravel project. In this project, if a non-user tries to edit a post then it will redirect to the posts page without letting the user make any edit. But I am getting this error</p>
  <blockquote>
  <p>Trying to get property of non-object (View: C:\xampp\htdocs\lsapp\resources\views\posts\show.blade.php).</p>
  </blockquote>
  <blockquote>
  <p>ErrorException (E_ERROR) Trying to get property of non-object (View: C:\xampp\htdocs\lsapp\resources\views\posts\show.blade.php) Previous exceptions and may be it might help Trying to get property of non-object (0) ErrorException {#221 ▼ #message: &quot;Trying to get property of non-object&quot; #code: 0 #file: &quot;C:\xampp\htdocs\lsapp\storage\framework\views\b4b78b7a31531d4e8ddf98112cc09d54ba62ed99.php&quot; #line: 3 #severity: E_NOTICE }</p>
  </blockquote>
  <p>Here is the edit code:</p>
  <pre><code> public function edit($id)
      {
          $post = Post::find($id);
          if(auth()-&gt;user()-&gt;id !== $post-&gt;user_id){
              return redirect ('posts')-&gt;with('error','Unauthorized page');
          } 
          return view('posts.edit')-&gt;with('post',$post);
      }
  </code></pre>
  <p>PostsController.php</p>
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use Illuminate\Http\Request;
  use App\Post;
  use DB;
  
  class PostsController extends Controller
  {
      /**
       * Create a new controller instance.
       *
       * @return void
       */
      public function __construct()
      {
          $this-&gt;middleware('auth',['except'=&gt;['index','show']]);
      }
      /**
       * Display a listing of the resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function index()
      {
          //$posts=Post::all();
          //$posts=Post::orderBy('title','desc')-&gt;get();
          //return Post::where('title','post two')-&gt;get();
          //$posts=DB::select(&quot;SELECT * FROM posts&quot;);
          //$posts= Post::orderBy('title','desc')-&gt;take(1)-&gt;get();
          $posts= Post::orderBy('created_at','desc')-&gt;paginate(1);
          
          return view('posts.index')-&gt;with('posts',$posts);
      }
  
      /**
       * Show the form for creating a new resource.
       *
       * @return \Illuminate\Http\Response
       */
      public function create()
      {
          return view('posts.create');
  
      }
  
      /**
       * Store a newly created resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\Response
       */
      public function store(Request $request)
      {
          $this-&gt;validate($request,[
              'title'=&gt;'required',
              'body'=&gt;'required'
          ]);
          //Create posts
          $post=new Post;
          $post-&gt;title = $request-&gt;input('title');
          $post-&gt;body = $request-&gt;input('body');
          $post-&gt;user_id=auth()-&gt;user()-&gt;id;
          $post-&gt;save();
  
          return redirect('posts')-&gt;with('success','Post Created');
      }
  
      /**
       * Display the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function show($id)
      {
          $post = Post::find($id); 
          return view('posts.show')-&gt;with('post',$post);
      }
  
      /**
       * Show the form for editing the specified resource.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function edit($id)
      {
          $post = Post::find($id);
          if(auth()-&gt;user()-&gt;id !== $post-&gt;user_id){
              return redirect ('posts')-&gt;with('error','Unauthorized page');
          } 
          return view('posts.edit')-&gt;with('post',$post);
      }
  
      /**
       * Update the specified resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
      public function update(Request $request, $id)
      {
          $this-&gt;validate($request,[
              'title'=&gt;'required',
              'body'=&gt;'required'
          ]);
          //Create posts
          $post=Post::find($id);
          $post-&gt;title = $request-&gt;input('title');
          $post-&gt;body = $request-&gt;input('body');
          $post-&gt;save();
  
          return redirect('posts')-&gt;with('success','Post Updated');
      }
  
      /**
       * Remove the specified resource from storage.
       *
       * @param  int  $id
       * @return \Illuminate\Http\Response
       */
  
      public function destroy($id)
      {
          $post=Post::find($id);
          $post-&gt;delete();
          return redirect('posts')-&gt;with('success','Post Deleted');
  
      }
  }
  </code></pre>
  <p>show.blade.php:</p>
  <pre><code>@extends('layouts.app')
  
  @section('content')
         &lt;a href=&quot;{{route('posts.index')}}&quot; class=&quot;btn btn-default&quot;&gt;Back&lt;/a&gt; 
         &lt;h1&gt;{{$post-&gt;title}}&lt;/h1&gt;
  
         &lt;div&gt;
              {{$post-&gt;body}}
         &lt;/div&gt;
         &lt;hr&gt;
         @if(!Auth::guest())
         @if(Auth::user()-&gt;id==$post-&gt;user_id)
         &lt;small&gt;Witten on {{$post-&gt;created_at}} by {{$post-&gt;user-&gt;name}}&lt;/small&gt;
  
         &lt;hr&gt;
         &lt;a href=&quot;{{$post-&gt;id}}/edit&quot; class=&quot;btn btn-default&quot;&gt;Edit&lt;/a&gt;
         {!!Form::open(['action'=&gt;['PostsController@destroy',$post-&gt;id],'method'=&gt;'POST', 'class'=&gt;'pull-right'])!!}
         {{Form::hidden('_method','DELETE')}}
         {{Form::submit('Delete',['class'=&gt;'btn btn-danger'])}}
         {!!Form::close()!!}
         @endif
  
         @endif
         @endsection
  </code></pre>
  <p>here is my route:</p>
  <pre><code>Route::get('/index', 'PagesController@index');
  Route::get('/about', 'PagesController@about');
  Route::get('/services', 'PagesController@services');
  
  Route::resource('/posts','PostsController');
  
  Auth::routes();
  
  Route::get('/dashboard', 'DashboardController@index');
  </code></pre>
  <p>Here is user.php in case it helps</p>
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Notifications\Notifiable;
  use Illuminate\Foundation\Auth\User as Authenticatable;
  
  class User extends Authenticatable
  {
      use Notifiable;
  
      /**
       * The attributes that are mass assignable.
       *
       * @var array
       */
      protected $fillable = [
          'name', 'email', 'password',
      ];
  
      /**
       * The attributes that should be hidden for arrays.
       *
       * @var array
       */
      protected $hidden = [
          'password', 'remember_token',
      ];
  
      public function posts(){
          return $this-&gt;hasMany('App\Post');
      }
  }
  </code></pre>