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

1

6

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?

Papism answered 8/8, 2017 at 15:43 Comment(3)
use array inside select like this select(['tourist_id', 'tourist_version'])Astrograph
In your first block of code $versions[slug1] looks like a typo; should be throwing a Use of undefined constant slug1, assumed 'slug1' error, since you're missing the '', $versions['slug1'].Roentgen
@timlewis, thank you, changed that.Papism
T
15

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!

Tucson answered 9/8, 2017 at 1:40 Comment(1)
thank you, Risan! Seems that I have to read a lot more about objects and identifiers...:)Papism

© 2022 - 2024 — McMap. All rights reserved.