How to convert SQL query to Laravel eloquent - sql

how can I convert my raw sql query to laravel query? I have issues with 'to_date' and tried many things.
This is my code:
select n_reserva,cod_hab_r1,cod_hab_r2,cod_hab_r3,fecha_reserva,fecha_entrada,fecha_salida,tour,dni_r,nombre,apellidos
from reservadas
join huespedes on huespedes.dni=reservadas.dni_r
where fecha_entrada=(select to_date(sysdate) from dual);
My suggest for laravel is this:
DB::table('reservadas')
->select('n_reserva','cod_hab_r1','cod_hab_r2','cod_hab_r3','fecha_reserva',
'fecha_entrada','fecha_salida','tour','dni_r','nombre','apellidos')
->join('huespedes','huespedes.dni','=','reservadas.dni_r')
->where('fecha_entrada','=',function($query){
$query->from('dual')
->select(DB::raw("to_date(sysdate)"));
})
->get();
SQL code works fine, so the problem is in the second one.

I mostly use an online converter to convert my raw SQL query to a query builder for example you can use the below website to convert your code from raw to query builder
https://sql2builder.github.io/
your query
select n_reserva,cod_hab_r1,cod_hab_r2,cod_hab_r3,fecha_reserva,fecha_entrada,fecha_salida,tour,dni_r,nombre,apellidos
from reservadas
join huespedes on huespedes.dni=reservadas.dni_r
where fecha_entrada=(select to_date(sysdate) from dual);
the output
DB::table('reservadas')
->select('n_reserva', 'cod_hab_r1', 'cod_hab_r2', 'cod_hab_r3', 'fecha_reserva', 'fecha_entrada', 'fecha_salida', 'tour', 'dni_r', 'nombre', 'apellidos')
->join('huespedes','huespedes.dni','=','reservadas.dni_r')
->where('fecha_entrada','=',function($query) {
$query->from('dual')
->select(DB::raw("'to_date'(sysdate));
})
->get();

Related

Can't join FREETEXTTABLE with typeORM functions

I'm working on a graphQL backend with TypeORM
I found FREETEXT, CONTAiNS, FREETEXTTABLE and CONTAINSTABLE options for fulltext searching in my SQL database.
As FREETEXTTABLE has a "RANK column, it is more useful and I’m using this option.
I added required settings to the database and by below query it’s working correctly when applying the query directly into the database:
SELECT * FROM content
INNER JOIN freetexttable( content , *, 'test text') newtable ON newtable.[KEY] = "id"
ORDER BY newtable.RANK desc
OFFSET 0 ROWS FETCH NEXT 12 ROWS ONLY
But when I try to use it with TypeORM like below I have some errors.
import { getRepository } from "typeorm";
let repo = getRepository(Content)
return repo.createQueryBuilder("qb")
.innerJoin(FREETEXTTABLE( qb , *, 'light'), newtable,newtable.[KEY] = qb.id)
.skip(0)
.take(12)
.getManyAndCount();
And it returns this error:
"Error: Invalid object name 'FREETEXTTABLE( qb , *, 'light')'."
Could you please let me know if there is a problem with my code or maybe you know a better option for fulltext search in SQL database
I was searching for the same issue. I was unable to find a solution to use FREETEXTTABLE with typeorm, so I had to implement it using raw SQL. using below code
repo.query(" RAW SQL ")

How to convert a table column data inside eloquent in laravel?

I am trying to convert the 'CHECKTIME' table column datetime to string for using the 'LIKE' operator in sql. The LIKE operator is not support the datetaime data type. So, I have to convert it into string to use LIKE. How can I do it in laravel eloquent? My Code in Controller:
$dailyData=CheckInOutModel::join('USERINFO', 'USERINFO.USERID', '=', 'CHECKINOUT.USERID')
->select('USERINFO.USERID as id','USERINFO.BADGENUMBER as bd', 'USERINFO.NAME as name','USERINFO.Image as photo','CHECKINOUT.*')
->where(CONVERT(VARCHAR(25),' CHECKINOUT.CHECKTIME'),'LIKE',$thisDay.'%')
->orderBy('CHECKINOUT.CHECKTIME','desc')->get();
The SQL Query that work to return the data:
SELECT CHECKINOUT.CHECKTIME
FROM CHECKINOUT
JOIN USERINFO ON USERINFO.USERID=CHECKINOUT.USERID
WHERE CONVERT(VARCHAR(25), CHECKINOUT.CHECKTIME, 126) LIKE '2021-11-23%';
First off, you can use the whereRaw() (see link) method to inject raw SQL in your query.
This would be something like this:
$dailyData=CheckInOutModel::join('USERINFO', 'USERINFO.USERID', '=', 'CHECKINOUT.USERID')
->select('USERINFO.USERID as id','USERINFO.BADGENUMBER as bd', 'USERINFO.NAME as name','USERINFO.Image as photo','CHECKINOUT.*')
->whereRaw('CONVERT(VARCHAR(25), CHECKINOUT.CHECKTIME) LIKE ?', $thisDay.'%')
->orderBy('CHECKINOUT.CHECKTIME','desc')->get();
But since you are working with dates, it's recomended to use dates all the way and not convert the data to compare strings.
Another approach would be to use whereBetween directly with dates. (see link)
Exemple :
$dailyData=CheckInOutModel::join('USERINFO', 'USERINFO.USERID', '=', 'CHECKINOUT.USERID')
->select('USERINFO.USERID as id','USERINFO.BADGENUMBER as bd', 'USERINFO.NAME as name','USERINFO.Image as photo','CHECKINOUT.*')
->whereBetween(
'CHECKINOUT.CHECKTIME',
[
$thisDay,
date('Y-m-d', strtotime('+1 day', $thisDay))
]
)->orderBy('CHECKINOUT.CHECKTIME','desc')->get();
I have over indented the relevent section to make it clear.
Reach out in comments if you have any issue or suggestion to improve it :)

How to convert SQL Query to CodeIgniter?

How to convert this query :
SELECT pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah
FROM pembelian_detail_tb
INNER JOIN produk_tb ON pembelian_detail_tb.kode_produk = produk_tb.kode_produk;
I have try many code, and i still got undefine.
Try this
$query = $this->db->select('pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah')
->from('pembelian_detail_tb')
->join('produk_tb', 'pembelian_detail_tb.kode_produk = produk_tb.kode_produk', 'inner')
->get();
I like to use Query Bindings once a join is involved in a query:
https://www.codeigniter.com/userguide3/database/queries.html
search page for "query bindings"
this escapes values for you of course and if you need to debug dump $this->db->last_query();

Use SQL query in Laravel

this is my sql Query:
SELECT DISTINCT batch_job_instance.JOB_NAME, MAX(batch_job_execution.CREATE_TIME) AS CREATED_TIME, batch_job_execution.END_TIME, batch_job_execution.STATUS FROM `batch_job_execution` INNER JOIN batch_job_instance ON batch_job_execution.JOB_INSTANCE_ID = batch_job_instance.JOB_INSTANCE_ID WHERE batch_job_execution.CREATE_TIME LIKE '%2016-12-05%' GROUP BY batch_job_instance.JOB_INSTANCE_ID ORDER BY MAX(batch_job_execution.CREATE_TIME) DESC
my Question, how i use that query in laravel? i am new in laravel, can someone help me? i need this for my project Thanks :)
You can run this SQL query as ot is if you had problem in converting in laravel format.
You can use :
DB::select(DB::RAW(your_sql_query)):
Hope this will help.
You can create a function within your model and query the data using the bellow format
public static function getJobs()
{
$jobs = DB::table('batch_job_execution')
->select(
DB::raw('
DISTINCT batch_job_instance.JOB_NAME,
MAX(batch_job_execution.CREATE_TIME) AS CREATED_TIME,
batch_job_execution.END_TIME,
batch_job_execution.STATUS
'
)
)
->join('batch_job_instance', 'batch_job_execution.JOB_INSTANCE_ID', '=', 'batch_job_instance.JOB_INSTANCE_ID')
->where('batch_job_execution', 'LIKE', '%2016-12-05%')
->groupBy('batch_job_instance.JOB_INSTANCE_ID')
->orderBy(DB::raw('MAX(batch_job_execution.CREATE_TIME)'),'DESC')
->get();
// ->toSql();
return $jobs;
}

Solr, select where in select

I'm trying to do a query with a select in another.
I would like to do something like:
select?q=*:* AND id_user=5&fl=id_other
select?q=test AND -id(the result of the other select)&fl=id
So:
select?q=test AND -id(select?q=* AND id_user=5&fl=id_other)&fl=id
Is it possible? Or I have to do two separates selects?
$ids = select?q=*:* AND id_user=5&fl=id_other
$result = select?q=test AND -id(implode(' AND ', $ids))&fl=id
Thanks!
There is a nested query support in Solr. You can _query_ parameter to embed query into another. Check this tutorial for more information.