Laravel eloquent returns different type of result on local and live - laravel-5

On my localhost laravel eloquent returns object while on live server it returns an array.
Why is this happening? is there any setting I need to update?
$parent_user = User::where('email', $organiser->email)->first();
Laravel v5.2

Related

Laravel Scout issue with relationship

Using Laravel scout I want to delete a record (on an Agent model), everything works well except that I'm redirecting to a list of agents after the delete, and since the queue is yet to be processed the deleted agent comes back from Meilisearch, but the Eloquent model doesn't exist anymore, resulting in an error Attempt to read property "id" on null :
// getting agents to display in the view
Agent::search($this->search)
->orderBy('id', 'asc')
->paginate($this->perPage)
// showing an agent ID in a loop in the view
{{ $agent->user->id }}
If reloading the page the list is fine - the deleted user has successfully been deleted. How can I prevent this error?
It surprises me that scout contains the null values in the collection...
However, you can run a ->filter() (docs) on the returned collection to remove all null results in it:
Agent::search($this->search)->get()->filter();
The collection now does not contain any null values

Will laravel reconnect to the database for any where on returned model collection?

If we have a find like this:
$returnedModel = Flight::
with("passengers")
->find(1);
Now if I add a where on returnedModel like this:
$returnedModel->passengers()->where("name" , "hamid")->first();
Will Laravel reconnect to the database for any where on returned model collection?
I added DB::getQueryLog() and there is a query with this where! how can I get this without reconnect to database?
You need to access your passengers as a property, not a method, by the way you get a Laravel collection already loaded from the DB (thanks to the with eager-loading).
$returnedModel->passengers->firstWhere('name', 'hamid');

convert DB array output to Eloquent Model for using API Resources

i want to use DB::select and DB::table in my laravel project
how can i get Eloquent Models from them ?
they returns array and can't used with API Resources

Laravel 5.5 orderby count and paginate

I'm trying to get to order the results by count in laravel 5.5 but haven't found any simple yet working method so far.
What I have tried for example:
$videos = Video::withCount('com_num')->where('active', '1')->orderByDesc('com_num_count')->paginate(51);
But this just gives me:
Call to undefined method Illuminate\Database\Query\Builder::com_num()
Is this possible somehow while using the Laravel's pagination?

Algolia save only strings using Laravel 5.4

I use Laravel 5.4 and i integrate algolia with my project, but I have some problems with data type of columns.
If I run the command from the localhost, status columns (and others) appear like integer in algolia database, but if I run the same artisan command from the production environment, the status column is now string, and I can't use ->where('status', 1) in my code, because algolia can use only integers for where clauses.
Is there a problem with my database? But is the same database from my localhost, same mysql version...
I don't know what could be the reason of this, but I think I know a good work around.
Let me also say that Algolia can have string as a where clause, but it's an intentional limitation of Laravel Scout.
You can use the toSearchableArray in your model to customize the array sent to Algolia. I'd use it to cast the data.
https://laravel.com/docs/5.5/scout#configuring-searchable-data
You should have something like this:
public function toSearchableArray()
{
$data = $this->toArray();
$data['attribute'] = (int) $data['attribute'];
return $data;
}

Resources