I have a problem tying to convert the following MySQL query to the Laravel (5.5) Eloquent query builder.
$start = '2018-01-22'; // Some random starting point for the Query
$query = "SELECT * FROM cdr c WHERE soort=3 AND con_duur > 0 AND con_duur
>= (select kortbel_seconden from queue q where q.queue_id=c.queue_id) AND `start_tijd >= '$start'";`
I have the following Models:
// CDR Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class CDR extends Model
{
protected $table = 'cdr';
protected $primaryKey = 'cdr_id';
public function Queue()
{
return $this->hasOne('App\Models\Queue', 'queue_id', 'queue_id');
}
}
// Queue Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Queue extends Model
{
protected $table = 'queue';
protected $primaryKey = 'queue_id';
public function cdr()
{
return $this->belongsTo('App\Models\CDR', 'queue_id', 'queue_id');
}
}
So far I have the following code in my Controller:
App\Models\CDR::with('queue')
->where('soort', '3')
->where('con_duur', '>', '0')
->where('start_tijd', '>=' , $start)
->where('con_duur', '>=', ' ') // this is where the sub select from the `queue` table should be : (select kortbel_seconden from queue q where q.queue_id=c.queue_id)
->get();
I’m stuck at the point of the sub select, is there a way to do this with Laravel’s Query Builder?
Thanks!
Consider this code:
DB::table('cdr AS c')
->select("*")
->where('c.soort','=',3)
->where('c.con_duur','>',0)
->where('c.con_duur','>=',function($query){
$query->select('kortbel_seconden')
->from('queue AS q')
->where('q.queue_id', '=', 'c.queue_id');
})
->where("c.start_tijd",">=",$start)
->get();
This part of the query:
->where('c.con_duur','>=',function($query){
$query->select('kortbel_seconden')
->from('queue AS q')
->where('q.queue_id', '=', 'c.queue_id');
})
is used to achieve the below part of the query:
`c`.`con_duur` >=
(SELECT
kortbel_seconden
FROM
queue q
WHERE q.queue_id = c.queue_id)
The above query results can be achieved by the following query as well through join:
DB::table('cdr AS c')
->select("c.*")
->join('queue AS q', 'q.queue_id', '=', 'c.queue_id')
->where('c.soort','=',3)
->where('c.con_duur','>',0)
->where('c.con_duur','>=','q.kortbel_seconden')
->where("c.start_tijd",">=",$start)
->get();
For more details you can visit:
https://laravel.com/docs/5.5/queries#where-clauses
Related
how can I make this query to query builder laravel 5.4?
select * from gb_employee where employee_id not in (select gb_emp_client_empid from gb_emp_client_lines where gb_emp_client_clientid =1) ;
Use a left join for this
$employee=DB::table('gb_employee as e')
->select('e.*')
->leftJoin('gb_emp_client_lines as el', 'e.employee_id', '=', 'el.gb_emp_client_empid')
->whereNull('el.gb_emp_client_empid',)
->get()
;
doing via eloquent way
class Employee extends Model
{
public function client_lines()
{
return $this->hasMany('App\ClientLines', 'gb_emp_client_empid', 'employee_id');
}
}
$employees = Employee::doesntHave('client_lines')->get();
I think I solved it. . .
$employees = DB::table('gb_employee')
->whereNotIn('employee_id', function($query) use ($client_id)
{
$query->select('gb_emp_client_empid')
->from('gb_emp_client_lines')
->where('gb_emp_client_clientid',$client_id);
})
->get();
i want get all posts with last three comment on each post. my relation is
public function comments()
{
return $this->hasMany('App\Commentpostfeed','post_id')->take(3);
}
This would return only 3 comments total whenever I called it instead of 3 comments per post.
i use this way :
1 :
Postfeed::with(['comment' => function($query) {
$query->orderBy('created_at', 'desc')->take(3); }]);
2 :
$postings = Postfeed::with('comments')->get();
but getting same result. please help me out for this problem.
Can you try like that ?;
Postfeed::with('comment')->orderBy('id','desc')->take(3);
Using plain mysql (If using Mysql) query you can get 3 recent comments per post using following query which rejoins comment table by matching created_at
SELECT p.*,c.*
FROM posts p
JOIN comments c ON p.`id` = c.`post_id`
LEFT JOIN comments c1 ON c.`post_id` = c1.`post_id` AND c.`created_at` <= c1.`created_at`
GROUP BY p.`id`,c.`id`
HAVING COUNT(*) <=3
ORDER BY p.`id`,c.`created_at` DESC
Sample Demo
Using laravel's query builder you can write similar to
$posts = DB::table('posts as p')
->select('p.*,c.*')
->join('comments c', 'p.id', '=', 'c.post_id')
->leftJoin('comments as c1', function ($join) {
$join->on('c.post_id', '=', 'c1.post_id')->where('c.created_at', '<=', 'c1.created_at');
})
->groupBy('p.id')
->groupBy('c.id')
->having('COUNT(*)', '<=', 3)
->orderBy('p.id', 'asc')
->orderBy('c.created_at', 'desc')
->get();
You can create a scope in the BaseModel like this :
<?php
class BaseModel extends \Eloquent {
/**
* query scope nPerGroup
*
* #return void
*/
public function scopeNPerGroup($query, $group, $n = 10)
{
// queried table
$table = ($this->getTable());
// initialize MySQL variables inline
$query->from( DB::raw("(SELECT #rank:=0, #group:=0) as vars, {$table}") );
// if no columns already selected, let's select *
if ( ! $query->getQuery()->columns)
{
$query->select("{$table}.*");
}
// make sure column aliases are unique
$groupAlias = 'group_'.md5(time());
$rankAlias = 'rank_'.md5(time());
// apply mysql variables
$query->addSelect(DB::raw(
"#rank := IF(#group = {$group}, #rank+1, 1) as {$rankAlias}, #group := {$group} as {$groupAlias}"
));
// make sure first order clause is the group order
$query->getQuery()->orders = (array) $query->getQuery()->orders;
array_unshift($query->getQuery()->orders, ['column' => $group, 'direction' => 'asc']);
// prepare subquery
$subQuery = $query->toSql();
// prepare new main base Query\Builder
$newBase = $this->newQuery()
->from(DB::raw("({$subQuery}) as {$table}"))
->mergeBindings($query->getQuery())
->where($rankAlias, '<=', $n)
->getQuery();
// replace underlying builder to get rid of previous clauses
$query->setQuery($newBase);
}
}
And in the Postfeed Model :
<?php
class Postfeed extends BaseModel {
/**
* Get latest 3 comments from hasMany relation.
*
* #return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function latestComments()
{
return $this->comments()->latest()->nPerGroup('post_id', 3);
}
/**
* Postfeed has many Commentpostfeeds
*
* #return Illuminate\Database\Eloquent\Relations\HasMany
*/
public function comments()
{
return $this->hasMany('App\Commentpostfeed','post_id');
}
}
And to get the posts with the latest comments :
$posts = Postfeed::with('latestComments')->get();
Ps :
Source
For many to many relationships
You can do it like this,
Postfeed::with('comments',function($query){
$query->orderBy('created_at', 'desc')->take(3);
})
->get();
I have places and locations tables.
Place could have many locations. Location belongs to Place.
Place:
id
title
Location:
id
place_id
floor
lat
lon
class Location extends Model {
public function place()
{
return $this->belongsTo('App\Place');
}
}
And
class Place extends Model {
public function locations()
{
return $this->hasMany('App\Location');
}
}
And i need to find places, that belongs only to 1st floor. select * from places inner join locations on places.id = locations.place_id where locations.floor = 1
How does it should be done in Eloquent?
Is something similar to
Place::where('locations.floor', '=', 1)->get() exists?
Yes, i know there is whereHas:
Place::whereHas('locations', function($q)
{
$q->where('floor', '=', 1);
})->get()
but it generates a bit complex query with counts:
select * from `places` where (select count(*) from `locations` where `locations`.`place_id` = `places`.`id` and `floor` = '1') >= 1
does not this works?
class Location extends Model {
public function place()
{
return $this->belongsTo('App\Place');
}
}
$locations = Location::where('floor', '=', 1);
$locations->load('place'); //lazy eager loading to reduce queries number
$locations->each(function($location){
$place = $location->place
//this will run for each found location
});
finally, any orm is not for database usage optimization, and it is not worth to expect nice sql's produced by it.
I haven't tried this, but you have eager loading and you can have a condition:
$places = Place::with(['locations' => function($query)
{
$query->where('floor', '=', 1);
}])->get();
Source
Try this :
Place::join('locations', 'places.id', '=', 'locations.place_id')
->where('locations.floor', 1)
->select('places.*')
->get();
I am working on a project with SlimPHP and Eloquent.
I am trying to run a RAW SQL query within a Model's method, like this:
/models/Form.php
<?php
namespace models;
class Form extends \Illuminate\Database\Eloquent\Model {
protected $table = 'forms';
public function getResponses($form_id)
{
// HERE
$select = \Illuminate\Support\Facades\DB::select('select 1');
return 1;
}
}
I am using Capsule to bootstrap the ORM.
The code above gives me:
Fatal error: Call to a member function select() on a non-object in
/vagrant/vendor/illuminate/support/Illuminate/Support/Facades/Facade.php
on line 208
Documentation is of very help in this case, could you shed some light on this?
thanks
Read the setup instructions on github closely and make sure you follow them correctly.
With Capsule you should use Illuminate\Database\Capsule\Manager or as DB "Facade".
$select = \Illuminate\Database\Capsule\Manager::select('select 1');
I usually import it and define an alias:
use Illuminate\Database\Capsule\Manager as DB;
// ...
$select = DB::select('select 1');
Plus if you need to do raw query, it may help to call setAsGlobal() after bootEloquent() like this
$capsule->addConnection($sqliteDb);
$capsule->bootEloquent();
$capsule->setAsGlobal(); // <--- this
In Slim 4,
we can use $this->getConnection()->select($sql);
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model{
protected $table = 'products';
public $timestamps = false;
protected $fillable = ["name", "price", "quantity", "supplier", "image", "category"];
public function getProductsByCategory(){
$sql = 'SELECT p.id,
p.name,
p.price,
p.quantity,
p.supplier,
p.image,
p.category,
(SELECT IF(COUNT(*) >= 1, TRUE, FALSE) FROM favorite f WHERE f.user_id = 1 AND f.product_id = p.id) AS isFavourite,
(SELECT IF(COUNT(*) >= 1, TRUE, FALSE) FROM carts c WHERE c.user_id = 1 AND c.product_id = p.id) AS isInCart
FROM products p';
return $this->getConnection()->select($sql);
}
}
I'm trying to write this SQL query with Eloquent ORM but still no success:
SELECT *
FROM article
LEFT JOIN article_category
ON article.category_id = article_category.id
WHERE article_category.name_url = 'html'
LIMIT 10`
This is what I've came up with so far (I try to write it with only one query just like above):
ArticleCategory::where('name_url', '=', 'html')->with('articles')->get();
But it shows an error:
Column not found:
1054 Unknown column 'article.article_category_id' in 'where clause'
(SQL: select * from `article` where `article`.`article_category_id` in (1))
My models:
class Article extends Eloquent {
protected $table = 'article';
public function categories() {
return $this->belongsTo('ArticleCategory', 'category_id');
}
}
class ArticleCategory extends Eloquent {
protected $table = 'article_category';
public function articles() {
return $this->hasMany('Article');
}
}
You can change your relationship function to use the correct ID.
public function articles() {
return $this->hasMany('Article', 'category_id');
}
It expects the column category_id to actually be named article_category_id. It expects this because it is referencing the table artice_catigory, so article_category_id makes sense.
If possible, just rename your column in the table article to article_category_id and everything should be good.
You can use left join using eloquent orm as follows
Article::leftJoin('article_category', 'article.category_id', '=', 'article_category.id')
->select(['*'])->where('article_category.name_url','html')->take(10)->get();