I'm facing a curious problem in my project with Laravel 5.1.
I have a query with some joins using query builder which is returning data, ok so far so good. If I use var_dump or print_r I can see the data as expected, but for some reason my code is not identifying the return as an object, see bellow part of the query.
$stockData = DB::table('tunits')
->whereNotIn("tunits.id", function ($query) {
$query->select(DB::raw('movements.tun_id'))
->from('movements')
->whereRaw('movements.tun_id = tunits.id')
->where('movements.mov_status', '=', '1');
})
->join('locations', 'tunits.loc_id', '=', 'locations.id')->first();
The result of print_r
stdClass Object ( [id] => 1 [loc_id] => 15 )
What is strange is, I have an if condition to check if the return is an object and it goes to the else, RETURN IS NOT AN OBJECT.
if(is_object($stockData)){
Log::debug('RETURN IS AN OBJECT');
}
else{
Log::debug('RETURN IS NOT AN OBJECT');
}
To make this more interesting, if I add one more log entry in my "if" condition trying to get a property which does not exists what obviously will raise an error, then the return will be different and it goes directly to the "if " instead to the "else" like in previously case, see the code changed:
if(is_object($stockData)){
Log::debug('RETURN IS AN OBJECT');
Log::debug('New entry => '.$stockData->some_Property)
}
else{
Log::debug('RETURN IS NOT AN OBJECT');
}
this time log says, the RETURN IS AN OBJECT, "BUT" now there is one more error of course due the invalid property
[2017-12-27 14:36:00] development.ERROR: Undefined property: stdClass::$some_Property BlaService.php113
Finally my question is, why the return is not an valid object? In the print_r I can see "stdClass Object" . If I try to access an valid property as we can see in the "print_r" it returns the same error, invalid propery, I really don't get it.
What I've tried
- Move the query to a database view and then select it. Doesn't worked.
- Also was created an model to this database view to avoid query builder and get the data by "Eloquent" even in my case being used in an incorrect way. Doesn't worked.
- If I use Eloquent to get data from a simple table "without any join" it works and the code says it's a valid object.
Anyone have any idea what might be happening?
Thanks !
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2Cd1Mqs
via IFTTT
Aucun commentaire:
Enregistrer un commentaire