Search code examples
laraveleloquentsql-order-by

Orderby Desc in Eloquent Laravel for related table


I get property 'image' of non-object error while trying to rearrange the order by which the data of two tables are being retrieved.

Here is my code for userController@index

$users = User::orderBy('id', 'desc')->with('profile')->paginate(2);
return view('multiauth::admin.user')->with('users', $users);

Code for admin.user view

@foreach ($users as $user)
                  <tr>
                    <td class="sort-image">{{$user->first_name}}</td>
                    <td class="sort-name">{{$user->last_name}}</td>
                    <td class="sort-name">{{$user->email}}</td>
                    <td class="sort-name">{{$user->account_type}}</td>
                    <td class="sort-name">{{$user->account_status}}</td>
                    <td class="sort-phone">{{$user->profile->image}}</td>
                    <td class="sort-email">{{$user->profile->business_name}}</td>
                    <td class="sort-address">{{$user->profile->address}}</td>
                    <td class="sort-city">{{$user->profile->city}}</td>
                    <td class="sort-state">{{$user->profile->state}}</td>
                    <td class="sort-country">{{$user->profile->country}}</td>
                    <td class="sort-zip">{{$user->profile->postal}}</td>
                    <td class="sort-date" data-date="1628071164">{{$user->profile->dob}}</td>
                    <td class="sort-phone">{{$user->profile->phone}}</td>
                    <td class="sort-name">{{$user->profile->facebook}}</td>
                    <td class="sort-name">{{$user->profile->linkedin}}</td>
                    <td class="sort-name">{{$user->profile->twitter}}</td>
                    <td class="sort-name">{{$user->profile->instagram}}</td>
                    <td class="sort-name">{{$user->profile->website}}</td>
                    <td> <a href="#" class="btn btn-white"><svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-pencil" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                      <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                      <path d="M4 20h4l10.5 -10.5a1.5 1.5 0 0 0 -4 -4l-10.5 10.5v4"></path>
                      <line x1="13.5" y1="6.5" x2="17.5" y2="10.5"></line>
                  </svg></a>
                  </td>
                  <td> <a href="#" class="btn btn-white"><svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-trash" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
                      <path stroke="none" d="M0 0h24v24H0z" fill="none"></path>
                      <line x1="4" y1="7" x2="20" y2="7"></line>
                      <line x1="10" y1="11" x2="10" y2="17"></line>
                      <line x1="14" y1="11" x2="14" y2="17"></line>
                      <path d="M5 7l1 12a2 2 0 0 0 2 2h8a2 2 0 0 0 2 -2l1 -12"></path>
                      <path d="M9 7v-3a1 1 0 0 1 1 -1h4a1 1 0 0 1 1 1v3"></path>
                  </svg></a>
                  </td>
                    
                  </tr>
                  @endforeach

Code for User Model

 public function profile()
{
    return $this->hasOne(Profile::class);
}

Code for Profile Model

public function user()
{
    return $this->belongsTo(User::class);
}

I'm using laravel 8. What I'm I missing please?


Solution

  • I found out it's not a syntax error, that the user records in the database are not properly related to the profile. i.e Userid column in the Profiles table is not related to the id in the Users table.