How can I make this query in laravel? [closed] - laravel

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I can't find how to make this query
SELECT * FROM paquetes
WHERE (
SELECT estatuses.nombre FROM estatus_paquete
INNER JOIN estatuses ON estatuses.id = estatus_paquete.estatus_id
WHERE (estatus_paquete.paquete_id = paquetes.id)
ORDER BY estatus_paquete.created_at DESC LIMIT 1
) = 'En envio'
AND paquetes.deleted_at IS NULL

It's a tricky query, but you can make it line by line with the following syntax.
$query = DB::query()
->select('*')
->from('paquetes')
->where(function ($sub) {
$sub->select('estatuses.nombre')
->from('estatus_paquete')
->join('estatuses', 'estatuses.id', 'estatus_paquete.estatus_id')
->whereColumn(['estatus_paquete.paquete_id', 'paquetes.id'])
->orderByDesc('estatus_paquete.created_at')
->limit(1);
}, 'En envio')
->whereNull('paquetes.deleted_at');
// dump($query->toSql());
$query->get();

Related

how can i reduce repeated code in laravel? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
i am new in laravel,
i have array like
{
music : {'a' =>1, 'b'=> 2},
video :{'c' =>10, 'd'=> 20}
}
i got this by using this code
$video = ResourcesVideo::where('resource_type', 'video')->get();
$music = ResourcesVideo::where('resource_type', 'music')->get();
$response['data'] = [
"videos" => $video,
"music" => $music,
];
on this code i got above array. so my quetions is.
how can i reduce that code. because many things are similer except where condition tried many ways but no success.
You can create array of resources and loop through it.. so in case there are more resource types in future you don't need to repeat code.
$resource_types = ['video', 'music'];
foreach ($resource_types as $resource_type) {
$resources[$resource_type] = ResourcesVideo::where([
'resource_type' => $resource_type,
])->get();
}
$response['data'] = $resources ?? [];

How to group bag in pig [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
First I have data and I group
A = LOAD './test.txt' USING PigStorage(' ') AS (id:int, time:int, value:float);
B = GROUP A BY time;
For example result I have structure like this.
1001 {(1,1001,0.2),(3,1001,0.3),(2,1001,0.3),(4,1001,0.6)}
1002 {(2,1002,0.5),(1,1002,0.3),(3,1002,0.1),(4,1002,0.6)}
1003 {(4,1003,0.2),(1,1003,0.8),(2,1003,0.4),(3,1003,0.5)}
But I want
1001 {(1,1001,0.2),(2,1001,0.3),(3,1001,0.3),(4,1001,0.6)}
1002 {(1,1002,0.3),(2,1002,0.5),(3,1002,0.1),(4,1002,0.6)}
1003 {(1,1003,0.8),(2,1003,0.4),(3,1003,0.5),(4,1003,0.2)}
Use NESTED FOREACH
C = FOREACH B {
sort_by_id = ORDER A BY id;
GENERATE group, sort_by_id ;
};

SUPER SIMPLE LINQ to filter row does not work [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Spent about 4 hours trying to figure this out without success.
IQueryable<userTOM> Infolist = userTOMs();
var rbtUserId = Infolist.Where(x => x.UserName == FormsUserName);
userTOMs <-- Does not exist.
Remade the namespace many times but will not appear.
IQueryable does not run immediately on the database. In order to run the query, one has to run functions such as .ToList(), or .Count() on the IQueryable.
Therefore, nothing in your pasted code triggers the database request, and the ID will not be retrieved. I would modify your request to this:
IQueryable<userTOM> Infolist = userTOMs();
userTOM rbtUser = Infolist.FirstOrDefault(x => x.UserName == FormsUserName);
Guid rbtUserId; //assuming this will be populated with a Guid value
if (rbtUser != null) {
rbtUserId = rbtUser.Id; //assuming "ID" is the property name
}

Summing and subtracting in Linq to Sql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hello all I want to achieve something like the SQL Select statement with Linq to Sql. Any help will be appreciated.
SELECT SUM(Debit-Credit) AS LBalance FROM dbo.LeaveLedger
WHERE StaffId =1 AND LYEAR='2000'
Assuming Entity Framework:
Context.Table.Where(x => x.StaffId == 1 and x.LYEAR == "2000")
.Sum(y => (y.Debit - y.Credit));
Something like this?
var sum =
db.LeaveLedger
.Where(ll => ll.StaffId == 1 and ll.LYEAR == "2000")
.Sum(ll => (ll.Debit - ll.Credit))
Since Mansfield has already shown the expression syntax, I'll have a go with the classic query:
var LBalance = (from p in dbo.LeaveLedger
where p.StaffId == 1 && p.LYEAR == "2000"
select (p.Debit - p.Credit).Sum();

convert SQL to LINQ group by min, count [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
How convert SQL to LINQ
SELECT
[good_id]
,MIN([good_price]) as minPrice
,Count([distributor_id]) as distrCount
FROM [Provizor].[dbo].[PRICES] where region_id=22
GROUP BY [good_id]
ORDER BY distrCount desc
How to do this in LINQ grouping
Something like this:
var query = dbo.Prices
.Where(x => x.region_id == 22)
.GroupBy(x => x.good_id)
.Select(g => new
{
minPrice = g.Min(x => x.good_price),
distrCount = g.Count(x=> x.distributor_id!=null)
}
.OrderByDescending(x => x.distrCount);

Resources