Laravel Query Builder where max id

You should be able to perform a select on the orders table, using a raw WHERE to find the max(id) in a subquery, like this: \DB::table(‘orders’)->where(‘id’, \DB::raw(“(select max(`id`) from orders)”))->get(); If you want to use Eloquent (for example, so you can convert your response to an object) you will want to use whereRaw, because some … Read more

Select entries between dates in doctrine 2

You can do either… $qb->where(‘e.fecha BETWEEN :monday AND :sunday’) ->setParameter(‘monday’, $monday->format(‘Y-m-d’)) ->setParameter(‘sunday’, $sunday->format(‘Y-m-d’)); or… $qb->where(‘e.fecha > :monday’) ->andWhere(‘e.fecha < :sunday’) ->setParameter(‘monday’, $monday->format(‘Y-m-d’)) ->setParameter(‘sunday’, $sunday->format(‘Y-m-d’));

Laravel query builder – re-use query with amended where statement

You can use clone to duplicate the query and then run it with different where statements. First, build the query without the from-to constraints, then do something like this: $query1 = $this->data_qry; $query2 = clone $query1; $result1 = $query1->where(‘from’, $from1)->where(‘to’, $to1)->get(); $result2 = $query2->where(‘from’, $from2)->where(‘to’, $to2)->get();

How to select distinct query using symfony2 doctrine query builder?

This works: $category = $catrep->createQueryBuilder(‘cc’) ->select(‘cc.categoryid’) ->where(‘cc.contenttype = :type’) ->setParameter(‘type’, ‘blogarticle’) ->distinct() ->getQuery(); $categories = $category->getResult(); Edit for Symfony 3 & 4. You should use ->groupBy(‘cc.categoryid’) instead of ->distinct()

Laravel Eloquent vs DB facade: Why use Eloquent and decrease performance? [closed]

Eloquent is Laravel’s implementation of Active Record pattern and it comes with all its strengths and weaknesses. Active Record is a good solution for processing a single entity in CRUD manner – that is, create a new entity with filled properties and then save it to a database, load a record from a database, or … Read more

How to select from subquery using Laravel Query Builder?

In addition to @delmadord’s answer and your comments: Currently there is no method to create subquery in FROM clause, so you need to manually use raw statement, then, if necessary, you will merge all the bindings: $sub = Abc::where(..)->groupBy(..); // Eloquent Builder instance $count = DB::table( DB::raw(“({$sub->toSql()}) as sub”) ) ->mergeBindings($sub->getQuery()) // you need to … Read more