Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I've assigned Laravel Query Builder to a variable. It changes when being used

it's a WHY-question, not How-to one:)

I have assigned a Query Bulder to a variable $query:

$query = table::where(['id'=>1, 'this_version'=> 1]);
$versions['slug1'] = $query->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);

outputs array with 2(!) sub-arrays:

Array
(
    [slug1] => Array
        (
            [0] => Array
                (
                    [tourist_id] => 1
                    [tourist_version] => 1
                )

            [1] => Array
                (
                    [tourist_id] => 2
                    [tourist_version] => 1
                )

        )

)

But if I add another line using $query between my $query declaration and it's usage in getting $version[2] array, my $version[2] output is shortened to a 1-dimensional array:

$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);
// Added line:
$versions['slug0'] = $query->select('version_created')->first()->version_created;
//
$versions['slug1'] = $query->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);

outputs (note slug1 now has only 1 nested array):

Array
(
    [slug0] => 2017-08-08 08:25:26
    [slug1] => Array
        (
            [0] => Array
                (
                    [tourist_id] => 1
                    [tourist_version] => 1
                )

        )

)

it seems like the like this line:

$versions['slug0'] = $query->select('version_created')->first()->version_created;

has added "first()" method to the original $query . Am I right and, if yes, why does it happen?

like image 505
Sergej Fomin Avatar asked Aug 08 '17 15:08

Sergej Fomin


People also ask

How does laravel query builder work?

In Laravel the database query builder provides an easy interface to create and run database queries. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates, etc. and it works on all supported database systems like a champ.

Which is better eloquent or query builder in laravel?

When you are handling more data, it is better to use Laravel's DB facade query builder than Laravel's Eloquent ORM. From performance tests, inserting 1000 rows in a simple table takes Eloquent 1.2 seconds whereas the DB facade takes only 800 milliseconds. It has a simpler syntax than the DB facade.

What is fluent query builder in laravel?

The Fluent Query Builder is Laravel's powerful fluent interface for building SQL queries and working with your database. All queries use prepared statements and are protected against SQL injection. You can begin a fluent query using the table method on the DB class.


1 Answers

Well, this is because by default an object (in your case is the Query builder object) in PHP is passed by reference. You can read more about this here: PHP OOP References.

I quote from the above reference:

A PHP reference is an alias, which allows two different variables to write to the same value.

When you pass the query builder object to the $query variable, you actually just pass the reference to this object and not the copy of it.

$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);

So when you call the first() method on the second line, it actually modifies the query builder object.

$versions['slug0'] = $query->select('version_created')->first()->version_created;

Thus causing the upcoming query result to be limited to 1. In order to work around this issue, you can clone the query object like this:

$query = previoustour2_tourist::where(['tour2_id'=>$tour->id, 'this_version'=> 1]);
$versions['slug0'] = (clone $query)->select('version_created')->first()->version_created;
$versions['slug1'] = (clone $query)->select('tourist_id', 'tourist_version')->get()->toArray();
print_r($versions);

Hope this help!

like image 146
Risan Bagja Pradana Avatar answered Nov 22 '22 05:11

Risan Bagja Pradana