0

How to convert the following Laravel (v8) Query to Eloquent? That is possible?

'house_id' => DB::table('houses') ->select('id') ->where('user_id', Auth::user()->id) ->orderByDesc('created_at')->value('id')

The current query is doing the job, but I'm curious to know if the same can be done with Eloquent. In other words, there's a more elegant way to do the same?

Sorry if my question doesn't make sense at all.

Thank you very much for any clarification.

2 Answers 2

1

Laravel has a latest() function on the query builder which orders results in descending order by created_at date. So you could simplify your query to as follows:

House::where('user_id', Auth::user()->id)->latest()->first()->id;

This might look confusing, latest()->first(), but latest() returns a collection so if you use first() to access the most recent record and then grab the id from the model.

If you find yourself using the above frequently, you could take it further and define a scope on your User model.

public function houses()
{
    return $this->hasMany(House::class);
}

public function scopeLatestHouse(Builder $query) {
    return $this->houses()->latest()->first();
}

Now all you need to do is:

$latestHouse = Auth::user()->latestHouse()->id;
Sign up to request clarification or add additional context in comments.

2 Comments

By the way, please can you refer to a good source (tutorial) where I can learn more about Eloquent (not at Laravel documentation). I will appreciate that!!
@ack2v Laravel 8 from Scratch is a good resource and has sections on Eloquent. Laracasts is a good resource in general. Not all courses are free but they are good quality and worth subscribing for when you feel it's appropriate (i.e. some of the more advanced topics).
0

If your model is called House : $house = House::query()->where('user_id', Auth::user()->getAuthIdentifier())->select('id')->orderBy('created_at', 'DESC')->get() or use ->first() if you want to get only one element. Then you can call $house->id.

7 Comments

Hi Takachi, I just want to get the ID value of the house. Yes, my Model is called House.
Edited my answer. But select('id') only select the id of the house, you can also use ->first('id') instead of ->select('id) and ->get()
Like that? House::query() ->where('user_id', Auth::user()->getAuthIdentifier()) ->select('id') ->orderBy('created_at', 'DESC') ->first('id') ->get(); In response, I want just an Integer, nothing else.
House::query()->where('user_id', Auth::user()->getAuthIdentifier())->orderBy('created_at', 'DESC')->first('id')
DB::table('houses') ->select('id') ->where('user_id', Auth::user()->id) ->orderByDesc('created_at')->value('id'); Returns 50 (for instance) House::query()->where('user_id', Auth::user()->getAuthIdentifier())->orderBy('created_at', 'DESC')->first('id') Returns the follow: App\Models\House {#1442 ▼ #table: "houses" #guarded: [] #fillable: array:8 [▶] # ... #attributes: array:1 [▼ "id" => 50 ] How to grab the ID?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.