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
Related
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);
Hello I have a little problem with eloquent in Laravel 5.
I have this function:
$lists = Model::with(array('relation_with_registered_in_model'=>function($query){
$query->where("name","cccc");
}))->get();
My poblem is that return always all results, ignoring where clause.
I tried to print my query (generated with function) and if I execute the query in my phpmyadmin it return correct filtered results.
What I'm wrong?
You could try with single qoute
$lists = Model::with(array('relation_with_registered_in_model'=>function($query){
$query->where('name','cccc');
}))->get();
I have resolved and Understand my error:
In this posts it is explained https://laracasts.com/discuss/channels/general-discussion/problem-with-the-eager-loading-of-laravel-eloquent-orm?page=1
// gets only models that have relation matching the closure constraint
// no eager loading of the relation!
Model::whereHas('relation', function ($q) { ...} );
// gets ALL models
// and eager loads the relation, but only rows matching the constraint
Model::with(['relation' => function ($q) { ...} ]);
I know, we can do this in the controller:
User::with('post')->get();
It will get every user's post from the database, based on users.id.
But the problem is, I want to do this:
User::with(['post' => function($query) {
# Throw users.id here...
}])->get();
How to do that?
You should get the users first, and then load related posts with a separate query and merge them manually.
$users = User::get();
$posts = Post::whereIn('user_id', $users->pluck('id'))->get(); // Get your additional data in this query
$users->each(function ($user) use ($posts)
{
$user->posts = $posts->where('user_id', $user->id);
});
Note: I did not test the code above. It's just an example to show you how to accomplish what you are trying to do.
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);
});
I have a simple pagination from my model User:
$users = User::paginate(15);
Unfortunately, it returns the results ordered by id or something.
I want to order the results ASC first and then paginate on them.
Can't find any way to get that working in combination with the paginate() method, like:
$users = User::orderBy('username', 'asc')->paginate(15);
Any idea?
Thanks
I already test it like your code. It works. Can you give the error.
Route::get('/view-tags', function()
{
$tag = App\Tag::orderBy('name', 'asc')->paginate(5);
dd($tag);
});