Laravel 5.2 - Display all SQLs executed in Eloquent not working - laravel

I used to use the following code in routes.php to display all SQLs executed in Eloquent:
// Display all SQL executed in Eloquent
Event::listen('illuminate.query', function($query)
{
var_dump($query);
});
But it doesn't work in Laravel 5.2 any more. Is it possible to get this feature back in 5.2? Thanks.

I found this replacement:
DB::listen(function ($event) {
dump($event->sql);
dump($event->bindings);
});

Related

WhereNotIn not working in eager loading laravel?

I Am using in Laravel 8 app next lines of codes, the problem is it is not filtering anything.
When I am using whereIn instead my app returns items as expected but when I add whereNotIn something goes wrong and not returning items opposite items to whereIn but all items.
$callback = function ($query) {
$query->whereHas('documents', function($query) {
$query->whereNotIn('document_type_id', $this->negative_documents);
});
};
$companies = $companies->whereHas('people', $callback)->with('people', $callback);
Any ideas?

How can i get the current month of a record from a loop in my blade

I am trying to get records from two tables USER & SALES table...but i need only the sales done for the current month for each user.
i am using this function to get all records from the two tables
$everyone = User::with('sales')->get();
Then in my view i am doing this
#foreach($everyone as $item)
$item->sales->sum('sales')
#endforeach
but i want to get the sales only for the current month in the view..how can i add where month in the view
I have tried
$item->sales->whereMonth('created_at', Carbon::now()->month)->sum('sales')
but not working
You can use whereHas() method, whereHas() works basically the same as has() but allows you to specify additional filters for the related model to check :
$everyone = User::whereHas('sales', function ($query) {
$query->whereMonth('created_at', Carbon::now()->format('M'));
})->get();
This method doesn't support Laravel 5.4 anymore. This method is for Laravel 5.5 and higher
The below code has not tested by me. Hope this works :
$everyone = User::with(['sales' => function ($query) {
$query->whereMonth('created_at', Carbon::now()->format('M'));
}])
->get();

How to retrive data from sql table with where condition using get() in laravel 5.8

how to retrieve multiple rows from table with where condition using laravel 5.8
I tried with joins but i am not getting data. below provided code is not working in my instance.
I am able to get first row but i am looking for multiple rows.
public function show($id)
{
$event = Event::find($id);
$speaker = Speaker::where('event_id', '=', $id)->get();
return view('events-show')->with('event', $event)->with('speaker', $speaker);
}
I want to retrieve two tables data using laravel 5.8 and display in view page.
try this
$speaker=Speaker::join('events','events.id','speakers.event_id')->where('events.id', '=', $id)->get();
return view('events-show')->with('speaker', $speaker);

Laravel paginate not working using with query

I can view all my posts and include their respective owner and category in the query.
public function index()
{
$posts = Post::with('user', 'category')->get();
return response()->json([
'posts' => $posts,
], 200);
}
Note: I used the with helper because thr front-end of the site is in vuejs.
Now I want to add pagination in my code but I get the following error:
"Method Illuminate\Database\Eloquent\Collection::with does not exist."
This is what I tried:
$posts = Post::paginate(2)->with('user', 'category')->get();
How can I use the laravel pagination?
For get the result you must be use paginate, get, first methods the end of the query
Post::with('user', 'category')->paginate(2);

Display all SQL executed in Eloquent in laravel 5.4

i have this route in my web.php:
Route::get('task' , function(){
return App\Carsoul::all();
});
this code return:
[{"id":12,"image":"1502286679.jpg"},{"id":14,"image":"1502287112.jpg"}]
i want to display query that return above result so i add this code before Route:
Event::listen('illuminate.query', function($query){
var_dump($query);
});
expect to return like:SELECT* FROM 'carsouls' ...
but return same result:
[{"id":12,"image":"1502286679.jpg"},{"id":14,"image":"1502287112.jpg"}]
what is the mistake?
Hi if you want to show the query as a string in Eloquant you can try
Model::orderBy('id','ASC')->toSql() it returns complete query.
Hope this helps

Resources