Home > front end >  Orderby Desc in Eloquent Laravel for related table
Orderby Desc in Eloquent Laravel for related table

Time:07-15

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 >{{$user->first_name}}</td>
                    <td >{{$user->last_name}}</td>
                    <td >{{$user->email}}</td>
                    <td >{{$user->account_type}}</td>
                    <td >{{$user->account_status}}</td>
                    <td >{{$user->profile->image}}</td>
                    <td >{{$user->profile->business_name}}</td>
                    <td >{{$user->profile->address}}</td>
                    <td >{{$user->profile->city}}</td>
                    <td >{{$user->profile->state}}</td>
                    <td >{{$user->profile->country}}</td>
                    <td >{{$user->profile->postal}}</td>
                    <td  data-date="1628071164">{{$user->profile->dob}}</td>
                    <td >{{$user->profile->phone}}</td>
                    <td >{{$user->profile->facebook}}</td>
                    <td >{{$user->profile->linkedin}}</td>
                    <td >{{$user->profile->twitter}}</td>
                    <td >{{$user->profile->instagram}}</td>
                    <td >{{$user->profile->website}}</td>
                    <td> <a href="#" ><svg xmlns="http://www.w3.org/2000/svg"  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="#" ><svg xmlns="http://www.w3.org/2000/svg"  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?

CodePudding user response:

You are getting that error because if a user does not belong to a profile then...

$user->profile === null;

There by $user->profile->image will throw an error.

There are two things you can do.

PHP8 has a feature which is the question mark operator.

$user?->profile?->image

The question mark operator is syntax that is basically the same as

isset($user->profile) && isset($user->profile->image)

The other thing you can do is to add a constraint to your query that will only return users that belong to a profile.

User::whereHas('profile')->with('profile')->get();

CodePudding user response:

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.

  • Related