Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent retrieve the first element and also keep the query result intact?

Please have a look at the following code:

$msgs = Message::where(function($query) use ($start,$end){

    $query->whereBetween('created_at', array($start, $end));

})->orderBy('created_at', 'DESC');

$first = $msgs->first()->created_at;
$ids = $msgs->lists('id');

What I'm trying to do is to get the latest date of the query result set and get all the ids from the result at the same time. But the code doesn't work because the $msgs was changed by the $msgs->first() function. I'm wondering if there is a way to let me get the first element of the query result without affecting the whole result set? Thanks

like image 956
Bagusflyer Avatar asked May 21 '14 01:05

Bagusflyer


People also ask

How do you pluck in Laravel eloquent?

While developing in eloquent, the column name can be passed as an argument in order to extract the values. Pluck () also accepts a second argument and if it happens to be an Eloquent collection, it will be another column name. To further strengthen the pluck() function, the wherein() function can be used.

How do I save a query in Laravel?

so that's why when you do $user->toSql(); you get select all statement. DB::listen(function ($query) { echo $query->sql; }); $user = New User; $user->name = "test"; $user->mail = "[email protected]"; $user->save(); two, QueryLog if you need more details.

What is difference between GET and all in Laravel?

all() is a static method on the Eloquent\Model . All it does is create a new query object and call get() on it. With all() , you cannot modify the query performed at all (except you can choose the columns to select by passing them as parameters). get() is a method on the Eloquent\Builder object.

What is pluck in Laravel query?

Laravel Pluck() is a Laravel Collections method used to extract certain values from the collection. You might often would want to extract certain data from the collection i.e Eloquent collection.


2 Answers

$first = $msgs->first(); // this does ->take(1) under the hood

Now your builder has limit = 1 clause.

So basically you need this:

// also you can use pluck:
$first = $msgs->pluck('created_at'); // returns first row`s single field
//then:
$ids = $msgs->take(9999)->lists('id'); // some high value depending on your data

Your solution is not the best, as it loads all the rows and creates objects of them, while all you need is list of ids.

You can also use first() method of the Collection to fetch its first item, instead of that foreach loop:

$messages = $msgs->get();
$first = $msgs->first()->created_at;
like image 91
Jarek Tkaczyk Avatar answered Sep 18 '22 10:09

Jarek Tkaczyk


I found a solution:

        $msgs = Message::whereBetween('created_at', array($start, $end))                    
                            ->whereIn('advertiser_id', $array)            
                            ->orderBy('created_at', 'DESC');

        $messages = $msgs->get();
        foreach($messages as $message)
        {
            $first = $message->created_at;
            break;
        }               

Surprisingly easy. But I don't know if it's an efficient way or not.

like image 43
Bagusflyer Avatar answered Sep 21 '22 10:09

Bagusflyer