SELECT lamparas.id,lampara, contenedor_id, contenedor, encendido_apagado, lamparas.estado
FROM lamparas
INNER JOIN contenedores ON lamparas.contenedor_id = contenedores.id
Assuming you are using Laravel 8 and Eloquent you could try something like:
$results = DB::table('lamparas')
->join('contenedores', 'lamparas.contenedor_id', '=', 'contenedores.id')
->select('lamparas.id', 'lampara', 'contenedor_id', 'contenedor', 'encendido_apagado', 'lamparas.estado')
->get();
You should really have a read of the docs: https://laravel.com/docs/8.x/queries
Related
I encountered a problem when trying to use with() function along with join:
$query = Model::query()->with([
'relationOne',
'relationTwo',
...
]);
$query->join(DB::raw("(
select *
from <models_table>
where <some_condition>
) as new_model"), 'new_model.id', '=', '<models_table>.id');
$query->paginate($rpp);
After paginate($rpp) call I received all items with appropriate relations appended, but without joined table (aka new_model). Is there a way to retrieve new_model along with relations ?
Have you tried to add select statement to emphasize the tables you want to get?
$query->join(DB::raw("(
select *
from <models_table>
where <some_condition>
) as new_model"), 'new_model.id', '=', '<models_table>.id')
->select(['<models_table>.*', 'new_model.*']);
Please try the below code hope it will help you.
$query = Model::query()->with([
'relationOne',
'relationTwo',
...
])
$query = $query->join(DB::raw("(
select *
from <models_table>
where <some_condition>
) as new_model"), 'new_model.id', '=', '<models_table>.id')
$query = $query->paginate($rpp);
I'm very new to Laravel, how can I make this query in Laravel using Eloquent model:
SELECT
(
SELECT departments.deptname FROM school.counts
LEFT JOIN reference.departments
ON counts.departmentcode = department.departmentcode
WHERE counts.countid = a.countsid
) AS departmentDesc
FROM school.subjecthdr a
LEFT JOIN school.subjectdtls b
ON a.subjectid = b.subjectid;
I don't wish to use raw queries, is there any way? I still appreciate raw query suggestions if there's any.
I think you still need some raw queries.
$dept_desc = \DB::table('school.counts')
->leftjoin('reference.departments', 'counts.departmentcode', '=', 'departments.departmentcode')
->whereRaw('counts.countid = a.countsid')
->selectRaw('departments.deptname');
\DB::table('school.subjecthdr AS a')
->leftjoin('subjectdtls AS b', 'a.subjectid', '=', 'b.subjectid')
->selectRaw('(' . $dept_desc->toSql() . ') AS departmentDesc')
->get();
PS: I think you leftjoin subjectdtls b but it seems you don't need it.
And you are using not only one database, if you want to use Eloquent\Builder,
you need to do something like this:
How to use multiple databases in Laravel
I have the following query:
select i.name,i.id itemId,its.item_id from items i
LEFT JOIN items_subscribers its
ON i.id = its.item_id
and its.user_id = 1
Which I transformed into Eloquent Way like this:
$records = Item::where('items_subscribers.user_id',$user_id)
->leftJoin('items_subscribers','items.id','=','items_subscribers.item_id')
->select(['items.name','items.id','items_subscribers.item_id as selected_item_id'])->get();
When I run print_r(DB::getQueryLog()); it prints following query:
select `items`.`name`, `items`.`id`, `items_subscribers`.`item_id` as `selected_item_id` from `items` left join `items_subscribers` on `items`.`id` = `items_subscribers`.`item_id` where `items_subscribers`.`user_id` = ?
The query yields nothing. I want to use AND instead of WHERE. How'd I do this?
I am using Laravel 4.2
Try using a closure and adding the condition in there:
$records = Item::leftJoin('item_subscribers', function($join) use ($user_id){
$join->on('items.id','=','items_subscribers.item_id');
$join->where('items_subscribers.user_id', '=', $user_id);
})
->select(['items.name','items.id','items_subscribers.item_id as selected_item_id'])
->get();
Just add another on method at the leftJoin part instead of using the where method. - https://stackoverflow.com/a/26714765/3208719
I am using LARAVEL 4 with MySQL back-end. I am novice to it.
I have a statement that returns records from 3 different tables as below :
$templates = Template::with('children')
->leftJoin('template_masters', function($join) {
$join->on('templates.template_master_id', '=', 'template_masters.id');
})
->leftJoin('surveyes', function($join) {
$join->on('templates.survey_id', '=', 'surveyes.id');
})
->get([
'templates.id',
'templates.survey_id',
'surveyes.title', // Here I want the IFNULL() condition e.g. IFNULL('surveyes.title','templates.title')
'templates.type',
'templates.created_at',
'template_masters.is_default'
]);
Basically this creates a query something like :
select `templates`.`id`,
`templates`.`survey_id`,
`surveyes`.`title`,
`templates`.`type`,
`templates`.`created_at`,
`template_masters`.`is_default`
from `templates`
left join `surveyes` on `templates`.`survey_id` = `surveyes`.`id`
left join `template_masters` on `templates`.`template_master_id` = `template_masters`.`id`
But I want this query like :
select `templates`.`id`,
`templates`.`survey_id`,
IFNULL(`surveyes`.`title`, `templates`.`title`),
`templates`.`type`,
`templates`.`created_at`,
`template_masters`.`is_default`
from `templates`
left join `surveyes` on `templates`.`survey_id` = `surveyes`.`id`
left join `template_masters` on `templates`.`template_master_id` = `template_masters`.`id`
In short, instead of surveyes.title, I want IFNULL(surveyes.title,templates.title).
How can I achieve this in ->GET([]) statement of given Eloquent ORM?
Thanks.
You need to use raw statement:
...
->get([
'templates.id',
'templates.survey_id',
DB::raw('IFNULL(surveyes.title,template_masters.title) as title'),
// or if you use namespace:
\DB::raw('IFNULL(surveyes.title,template_masters.title) as title'),
'templates.type',
'templates.created_at',
'template_masters.is_default'
]);
Can I write something like this:
$post = Post::join(['author'])->find($postId);
$authorName = $post->author->name;
To produce only ONE select with inner join (no 2 selects) and without using DB query builder
SELECT
post.*,
author.*
FROM post
INNER JOIN author
ON author.id = post.author_id
WHERE post.id = ?
You can do it in Eloquent using the join method:
$post = Post::join('author', function($join)
{
$join->on('author.id', '=', 'post.author_id');
})
->where('post.id', '=', $postId)
->select('post.*', 'author.*')
->first();
Please note that your results in $post will be an object where their attributes will correspond to the result set, if two columns has the same name it will be merged. This happen when using:
->select('post.*', 'author.*')
To avoid this, you should create alias to those columns in the select clause as shown below:
->select('post.id AS post_id', 'author.id AS author_id')
Try
Post::join('author',function($join){
$join->on('author.id','=','post.author_id');
})->where('post.id','=',$postId)->select('post.*','author.*');