Jeffrey has a video that explains this, but basically two SQL queries.
When creating relations with the Laravel ORM, what is the raw MySQL query behind it?
For example, if I have a Posts and a Comments model and "bind" (not sure if it's the proper term) comments to a post with...
class Post {
public function comments()
{
return $this->hasMany('App\Comment');
}
}
What is the raw MySQL query behind that? I'm kind of weak on MySQL so I just want to know to learn more about it and how should I structure my future database queries with the Laravel ORM.
@p0t4t0 there are so many ways:
//1
DB::enableQueryLog();
$queries = DB::getQueryLog();
dd($queries );
//2. you listen to it, by adding the following inside your boot method in //AppServiceProvider
DB::listen(function ($query) {
// $query->sql
// $query->bindings
// $query->time
});
//3. for a single query
//in your case get the comments of a single post
$post = Post::first();
$comments = $post->comments()->get();
$rawQuery = $comment->toSql();
//4. finally the best way is to install this package https://github.com/barryvdh/laravel-debugbar
//just require through composer
composer require barryvdh/laravel-debugbar
and then in .env file set the APP_DEBUG=true... you are done.
It has many more features, enabling/disabling at runtime, and bridge collectors... you can refer to the link for more options. cheers~
Please or to participate in this conversation.