How to check eloquent queries in laravel 5? - laravel

Im trying to check the eloquent queries to check for n+1 problems.
Im using this "Event::listen('illuminate.query', function($sql){
var_dump($sql);
});" on top of web.php routes file but no query appears. It seems that is because of the version of laravel, that is laravel 5.
Do you know in this version how to properly check the eloquent queries?

Add this to AppServiceProvider::boot() (documentation):
DB::listen(function ($query) {
var_dump($query);
});

try this library dude https://github.com/barryvdh/laravel-debugbar , you will know all of your query

Related

Create pagination from groupBy in laravel 5.8

$payments = Payment::with('borrowerUser','collectorUser')
->get()
->groupBy('date_paid');
which gives me this result
My question is, is it possible for me to put a pagination in this scenario?
I already tried
$payments = Payment::listings()
->with('borrowerUser','collectorUser')
->groupBy('date_paid')->paginate(5);
and put {{$payments->listings()}} on my blade
but that doesn't work.
any suggestions? thank you in advance!
put this code in controller:
$payments = Payment::with('borrowerUser','collectorUser')
->groupBy('date_paid')->paginate(5);
put below code in blade file after for loop is completed
{!! $payments ->render() !!}
This is the must read content from the laravel documentation
Currently, pagination operations that use a groupBy statement cannot
be executed efficiently by Laravel. If you need to use a groupBy with
a paginated result set, it is recommended that you query the database
and create a paginator manually.
Documentation link
You can use orderBy clause after groupBy to get the result, but still it won't give you efficient results

Laravel Scout does not respect Algolia's order

I try to use Laravel Scout with Algolia as my search engine.
I use it like this
MyModel::search('my query')->paginate()->pluck('id');
Problem is, Algolia returns the correct order let's say some records with ids 3, 1, 2 but when Laravel fetches the records on the database, the ids are like 1, 2, 3 (ordered by id).
Am I using Scout wrong?
Regarding the issue #341 on the scout repository, it seems to be a scout problem being investigated.
EDIT
The issue is now fixed as of PR #369 ( version > 7.0)
You have to edit vendor file in your laravel project:
vendor/laravel/scout/src/Searchable.php
and edit return statement of getScoutModelsByIds() function into:
$ids_ordered = implode(',', $ids);
return $query->whereIn(
$this->getScoutKeyName(), $ids
)->orderByRaw(DB::raw("FIELD(id, $ids_ordered)"))->get();

Laravel Scout toSql does not exist

I am trying to get SQL query as a string from query builder. toSql() method works fine without laravel scout. I am using this package for Fulltext search. Here is my code.
Post::toSql(); // Working without scout => select * form post
But I am getting an error when try to search
Post::search('test')->toSql();
Method Laravel\Scout\Builder::toSql does not exist
How Laravel Scout can be extended to add a method toSql to it.
Try this
DB::enableQueryLog();
Post::search('test');
dd(DB::getQueryLog());

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;
}

Checking value of a related model in Laravel

I have users. Users have a relationship with a country, and country has a region.
I would like to access all users who are within a region, and I am doing it through their related country, like this:
user->country->region
When I tried to use whereHas, like this:
$users = User::whereHas('country', function ($query) {
$query->where('region', 3);
})
->get();
I got an error that said there was an unexpected } on the 3rd line. I would like to know what is the best method of doing this.
Use Laravel Eloquent relationship, In Laravel eloqunent there are Has Many Through option available for as per your requirement.
Please check this :-> Here is laravel documentation link

Resources