title: Laravel 5.2 : How to retrieve data from database in one to one eloquent relationship
body: <p>I want to access data for a particular user and update the data.As the database is empty for that reason when i wanted to go to the update page for updating the data it's showing the following error.</p>
  
  <blockquote>
    <p>ErrorException in 14cfe21a93ffc779c1e985ab027ee400af4e7d8b.php line
    12: Trying to get property of non-object (View:
    C:\xampp\htdocs\protfolio\resources\views\update.blade.php)</p>
  </blockquote>
  
  <p>I'm using one to one  eloquent relationship.
  single user can create his own profile and update the profile.</p>
  
  <p>My User Model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Foundation\Auth\User as Authenticatable;
  
  class User extends Authenticatable
  {
  
      protected $fillable = [
          'name', 'email', 'password',
      ];
  
  
      protected $hidden = [
          'password', 'remember_token',
      ];
  
       public function post()
      {
          return $this-&gt;hasOne('App\Post'); //Profile is your profile model
      }
  }
  </code></pre>
  
  <p>My Post Model:</p>
  
  <pre><code>&lt;?php
  
  namespace App;
  
  use Illuminate\Database\Eloquent\Model;
  
  class Post extends Model
  {
        protected $fillable = [
      'first_name', 'middle_name', 'last_name','gender',                            'dob','nationality','nid','email','phone_no','about_me'
  ];
       public function user()
      {
         return $this-&gt;belongsTo('App\User'); //Profile is your profile model
      }
  }
  </code></pre>
  
  <p>Here is the route:</p>
  
  <pre><code>&lt;?php
  
      Route::group(['middleware' =&gt; ['web']], function () {
  
      Route::get('/', function () {
      return view('welcome');
      })-&gt;name('home');
  
      Route::get('/dashboard', [
      'uses' =&gt;'PostController@getDashboard',
      'as' =&gt; 'dashboard',
      'middleware' =&gt; 'auth'
      ]);
  
      Route::post('/update',[
      'uses'=&gt;'PostController@postUpdate',
      'as' =&gt; 'update',
      'middleware' =&gt;'auth'
      ]);
  
      Route::get('/account',[
      'uses' =&gt; 'UserController@getAccount',
      'as' =&gt;'account',
       'middleware' =&gt;'auth'
      ]); 
  
    Route::get('/create',[
      'uses' =&gt; 'UserController@getCreate',
      'as' =&gt;'create',
      'middleware' =&gt;'auth'
      ]);
  
    Route::post('/save',[
      'uses'=&gt;'PostController@postCreate',
      'as' =&gt;'save'
  
      ]);
  
  });
  </code></pre>
  
  <p>My Post Controller:</p>
  
  <pre><code>    &lt;?php
  
      namespace App\Http\Controllers;
  
      use Illuminate\Http\Request;
      use Illuminate\Http\Response;
      use App\Http\Requests;
      use App\Post;
      use App\User;
      use Illuminate\Support\Facades\Auth;
      use Illuminate\Support\Facades\Storage;
      use Illuminate\Support\Facades\File;
  
      class PostController extends Controller
      {
          public function getDashboard()
     {        
        $posts = DB::table('posts')-&gt;where('id', Auth::user()-&gt;id);     
            return view('dashboard',['posts'=&gt;$posts]);  
      }
          public function postUpdate(Request $request)
      {
            $this-&gt;validate($request,[
              'first_name'=&gt; 'required|max:120',
              'middle_name'=&gt; 'required|max:120',
              'last_name' =&gt; 'required|max:120',
              'gender'=&gt; 'required',
              'dob'=&gt;'required',
              'nationality'=&gt;'required',
              'nid'=&gt;'required',
              'email' =&gt; 'required|email|unique:users',
              'phone_no'=&gt;'required',
              'about_me'=&gt;'required',
              ]);
       //   $data =fill($request-&gt;all());
        $data = $request-&gt;only('first_name', 'middle_name', 'last_name',
         'gender', 'dob', 'nationality', 'nid', 'email', 'phone_no', 'about_me');
         $request-&gt;user()-&gt;post()-&gt;update($data) ;  
          return redirect()-&gt;route('dashboard');
         }        
      }
  </code></pre>
  
  <p>And here is the update.blade.php</p>
  
  <pre><code>@extends('layouts.master')
  
  @section('content')
       @include('includes.message-block')
       &lt;div class="container" &gt;
              &lt;h3&gt; Update Profile &lt;/h3&gt;
          {!! Form::open(array('route' =&gt; 'update','class'=&gt;'form-horizontal','method'=&gt;'POST'))  !!}
      {{--    {!! Form::token(); !!}
          {!!   csrf_field() ; !!} --}}
              &lt;div class="form-group"&gt;
                  &lt;label&gt;First Name&lt;/label&gt;
                  &lt;input type="text" name="first_name" class="form-control"   value="{{$posts-&gt;first()-&gt;first_name}}"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Middle Name&lt;/label&gt;
                  &lt;input type="text" name="middle_name" class="form-control"  value="{{$posts-&gt;first()-&gt;middle_name}}"&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Last Name&lt;/label&gt;
                  &lt;input type="text" name="last_name" class="form-control"   value="{{$posts-&gt;first()-&gt;last_name}}"&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Gender&lt;/label&gt;
                  &lt;select class="form-control" name="gender" value="{{$posts-&gt;first()-&gt;gender}}"&gt;
                      &lt;option&gt;Male&lt;/option&gt;
                      &lt;option&gt;Female&lt;/option&gt;     
                  &lt;/select&gt;
              &lt;/div&gt;
              &lt;div class="form-group"&gt;
                  &lt;label&gt; Date Of Birth&lt;/label&gt;  
                  &lt;input type="date" name="dob" class="form-control"  value="{{$posts-&gt;first()-&gt;dob}}"&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Nationality&lt;/label&gt;
                  &lt;input type="text" name="nationality" class="form-control"   value="{{$posts-&gt;first()-&gt;nationality}}"&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label&gt;NID&lt;/label&gt;
                  &lt;input type="text" name="nid" class="form-control"   value="{{$posts-&gt;first()-&gt;nid}}"&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Email&lt;/label&gt;
                  &lt;input type="email" name="email" class="form-control"   value="{{$posts-&gt;first()-&gt;email}}"&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label&gt;Phone&lt;/label&gt;
                  &lt;input type="text" name="phone_no" class="form-control"   value="{{$posts-&gt;first()-&gt;phone_no}}"&gt;
              &lt;/div&gt;
  
              &lt;div class="form-group"&gt;
                  &lt;label&gt;About Me&lt;/label&gt;
                  &lt;textarea class="form-control" name="about_me"  rows="3"&gt;{{$posts-&gt;first()-&gt;about_me}}&lt;/textarea&gt;
              &lt;/div&gt;
  
  
              &lt;button type="submit" class="btn btn-default"&gt;Submit&lt;/button&gt;
          {!! Form::close() !!}
          &lt;/div&gt;
  
   @endsection
  </code></pre>