Query Builder, where clause - query-builder

How to make in Query Builder (Doctrine) construction WHERE condition A AND (condition B OR condition C) ??
I have tried
->where
->andwhere
->orwhere
And
->where
->orwhere
->orwhere
But both dont work

It can be done this way:
->where("bp.active = :active")
->andWhere("p.name1 Like :name OR p.name2 Like :name")

Related

Laravel 8 - How I do where clause in table added with join

Hi I want to know how can i do this query in Laravel 8 , I tried adding the join clause but not work as expected, i need join clause? Or maybe there is another form to do it. I search others examples but i donĀ“t see anythat help me. The query is the next:
DB::table('escandallo_p as esc')
->select("esc.material", "esc.referencia", "esc.ancho", "esc.proveedor", "esc.punto",
"esc.precio", "esc.consumo", "esc.veces", "esc.001", "esc.002", "esc.003", "esc.004",
"esc.005", "esc.006", "esc.007", "esc.008", "esc.009", "esc.010", "esc.011", "esc.um", "esc.merma", "esc.importe", "esc.tipo", "esc.xtalla", "esc.fase",
DB::raw("(select anulado from prototipos_p as p where p.prototipo = '".$scandal[0]->prototipo."' and p.tipo = 'c' and p.referencia = esc.referencia )"),
// ignore
//original query "(select anulado from prototipos_p as p where p.prototipo = ",$request->prototipo," and p.tipo = 'c' and p.referencia = esc.referencia ) as 'anulado'",
// "(select clase from prototipos_p as p where p.prototipo = ",$request->prototipo," and p.tipo = 'c' and p.referencia = esc.referencia ) as 'clase'")
//Converted query ->select('pro.anulado')->where('pro.prototipo', $request->prototipo)
// ->where("p.prototipo", "=", $request->prototipo)
->where("esc.id_escandallo", "=", $request->id_escandallo)
->where("esc.id_version", "=", $request->version)
->orderBy("id","asc")
->get();
!!!! I need to pass the esc.referencia to the sub select query
The second select is the conversion of the select inside "" ( i know this is wrong is only for explain it).
Thank you in advance for any suggestion.
Best regards
EDIT: I can solve my problem with DB::raw, but if anyone know others methos are welcome!
You need to pass callback to the join query to add the extra query to the laravel's join method,
Example from Laravel Doc:
DB::table('users')
->join('contacts', function ($join) {
$join->on('users.id', '=', 'contacts.user_id')
->where('contacts.user_id', '>', 5);
})
->get();
It is explained in Laravel's doc, Advanced Join Clauses
There is Subquery support too Subquery Joins,
Eg:
$latestPosts = DB::table('posts')
->select('user_id', DB::raw('MAX(created_at) as last_post_created_at'))
->where('is_published', true)
->groupBy('user_id');
$users = DB::table('users')
->joinSub($latestPosts, 'latest_posts', function ($join) {
$join->on('users.id', '=', 'latest_posts.user_id');
})
->get();
These two might help you to achieve what you are trying
After test joins, joinSub, whereIn and other forms of doing this, I solved my problem using the DB::raw():
DB::table('escandallo_p as esc')
->select('parameters',....,
DB::raw("(SELECT column //(ONLY ONE)
FROM table
WHERE column = '".$parameter."' ...) AS nombre"),
)
->where('column', "=", $parameter)
->orderBy("id","asc")
->get();

How to write Joins inside Left join in Laravel

I have a query which has sub query with 2 joins inside a Left join and I was trying to convert it to Laravel
LEFT JOIN (
orders xo
JOIN factories xs
ON ( xs.factory_id = xo.factory_id )
JOIN setup sp
ON ( sp.factory_id = xs.legacy_factory_id)
)
ON ( xo.production_id = po.production_id )
I tried something like this
->leftJoin('orders AS xo', function ($query) use ($input) {
$query->join('factories AS xs','xs.factory_id','=','xo.factory_id')
->join('setup AS sp','sp.factory_id','=','xs.legacy_factory_id');
},function($join) {
$join->on('xo.production_id','=','po.production_id');
Would like some help with this convertion
You can use Subquery Joins like so
$SubQuery= DB::table('orders AS xo')
->join('factories AS xs','xs.factory_id','=','xo.factory_id')
->join('setup AS sp','sp.factory_id','=','xs.legacy_factory_id');
// then use the subQuery like below:
// here a is the alias to the subquery
DB::table('your_table as po')
->leftjoinSub($SubQuery, 'a', function ($join) { $join->on('a.production_id', '=', 'po.production_id'); })
There was a problem that you don't wanna do Eloquent: Relationships?
look the below link:
https://laravel.com/docs/9.x/eloquent-relationships#main-content
Additionally, You will need a patch for this problem hasOne or hasMany.

eloquent query with orderBy subquery

How to convert the following query to eloquent
DB::select("SELECT fp.*
FROM forum_posts fp
ORDER BY (
SELECT( CASE WHEN MAX(fa.created_at) > fp.created_at THEN MAX(fr.created_at) ELSE fp.created_at END )
FROM forum_answers fa
WHERE fp.id_post = fa.id_post
)
DESC");
Just to exemplify what I'm trying to achieve
Post::orderBy(
'SELECT( CASE WHEN MAX(fa.created_at) > fp.created_at THEN MAX(fr.created_at) ELSE
fp.created_at END )', 'DESC'
)->get()
#josei, Eloquent order method supports subqueries and there is a number of ways you can use to achieve the same result
Post::select('forum_posts.*')->orderByRaw('...')
// or
Post::query()->select('forum_posts.*')->orderByDesc(
Post::where() ...
)
// or
Post::query()->select('forum_posts.*')->orderBy(DB::raw('...'))
Hope this helps, good luck!

How do this sentence in query builder

I have a question, i need do this query in query builder:
SELECT
ps_orders.id_order,
ps_customer.firstname,
ps_customer.lastname,
ps_customer.email,
ps_address.address1,
ps_address.postcode,
ps_address.city,
ps_address.phone_mobile,
ps_orders.id_order,
ps_orders.reference,
ps_order_detail.product_name,
ps_order_detail.product_quantity,
ps_shop.name AS tienda,
ps_carrier.name AS transportista
FROM
ps_customer
INNER JOIN ps_address ON ps_customer.id_customer = ps_address.id_customer
INNER JOIN ps_orders ON ps_address.id_customer =
ps_orders.id_customer AND ps_orders.id_address_delivery =
ps_address.id_address
INNER JOIN ps_order_detail ON ps_orders.id_order =
ps_order_detail.id_order
INNER JOIN ps_shop ON ps_order_detail.id_shop = ps_shop.id_shop
INNER JOIN ps_carrier ON ps_orders.id_carrier = ps_carrier.id_carrier
WHERE
ps_orders.id_order =54
I did this query in query builder:
$naviones=DB::connection('naviones')->table('ps_customer')
->join('ps_address','ps_customer.id_customer','=','ps_address.id_customer')
->join('ps_orders','ps_address.id_customer','=','ps_orders.id_customer')
->join('ps_order_detail','ps_orders.id_order','=','ps_order_detail.id_order')
->join('ps_shop','ps_order_detail.id_shop','=','ps_shop.id_shop')
->join('ps_carrier','ps_orders.id_carrier','=','ps_carrier.id_carrier')
->select('ps_orders.id_address_delivery','ps_orders.id_order','ps_customer.firstname','ps_customer.lastname','ps_customer.email','ps_address.address1',
'ps_address.postcode','ps_address.city','ps_address.phone_mobile','ps_orders.id_order','ps_orders.reference',
'ps_order_detail.product_name','ps_order_detail.product_quantity','ps_shop.name as tienda','ps_carrier.name as transportista')
->where('ps_orders.id_order','=',$idpedido)->get();
My question is: How can i do for made this query section?
INNER JOIN ps_orders ON ps_address.id_customer =
ps_orders.id_customer AND ps_orders.id_address_delivery =
ps_address.id_address
I hope that yours will can help me. Thank you.
You can use join something like this
->join('table2 AS b', function($join){
$join->on('a.field1', '=', 'b.field2')
->where('b.field3', '=', true)
->where('b.field4', '=', '1');
})
in case if you don't want to compare then you can use multiple "on" chained function like below
$query->join('ps_orders', function($join)
{
$join->on('ps_orders.id', '=', 'ps_address.id_customer')
->on('ps_orders.id_address_delivery', '=', 'ps_address.id_address')
});

How to use orderBy and where clause together in Laravel5 ORM

In laravel5 I need to put orderBy and where clause together, as in:
$patches = Patch::orderBy('PatchCode', 'DESC')
->where('AccountID', '=', Auth::user()->AccountID)->get();
But orderBy is not working. How can I achieve that?
Here is the code which will definitely work. Just put the orderBy at the end. Like:
$patches = Patch::where('AccountID', '=', Auth::user()->AccountID)
->orderBy('PatchCode', 'DESC')
->get();

Resources