Behind the scene the PDO is being used and it's the way that PDO
does as a prepared query, for example check this:
$title = 'Laravel%';
$author = 'John%';
$sql = "SELECT * FROM books WHERE title like ? AND author like ? ";
$q = $conn->prepare($sql);
$q->execute(array($title,$author));
In the run time during the execution of the query by execute()
the ?
marks will be replaced with value passed execute(array(...))
. Laravel/Eloquent
uses PDO
and it's normal behavior in PDO
(PHP Data Objects). There is another way that used in PDO
, which is named parameter/placeholder
like :totle
is used instead of ?
. Read more about it in the given link, it's another topic. Also check this answer.
Update: On the run time the ?
marks will be replaced with value you supplied, so ?
will be replaced with 1
. Also this query
is the relational query, the second part after the first query has done loading the id
s from the posts
table. To see all the query logs, try this instead:
$queries = \DB::getQueryLog();
dd($queries);
You may check the last two queries to debug the queries for the following call:
$response = $this->posts
->where('author_id', '=', 1)
->with(array('postComments' => function($query) {
$query->where('comment_type', '=', 1);
}))
->orderBy('created_at', 'DESC')
->limit($itemno)
->get();
Update after clarification:
You may use something like this if you have setup relation in your Posts
model:
// $this->posts->with(...) is similar to Posts::with(...)
// if you are calling it directly without repository class
$this->posts->with(array('comments' =. function($q) {
$q->where('comment_type', 1);
}))
->orderBy('created_at', 'DESC')->limit($itemno)->get();
To make it working you need to declare the relationship in your Posts
(Try to use singular name Post
if possible) model:
public function comments()
{
return $this->hasmany('Comment');
}
Your Comment
model should be like this:
class Comment extends Eloquent {
protected $table = 'post_comments';
}