How to call model function in query builder [closed] - php

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 3 months ago.
Improve this question
Function From Customer to Order & Function check active order
Query
How to get customers where doesnt have relation in Order Models and customers where have relation in Order but the Order was inactive ??
Get Customer where doesnt have relation in order and customer where have relation in Order but Order state was inactive/expired

Try This
Customer::with('orders')->whereDoesntHave('orders')->orWhereHas('orders', function($query){
$query->select('orders.state')
->from('orders')
->whereColumn('orders.customer_id', 'customers.id');
}, 'expired')->get();

Related

What SQL query should I use to output the value of my 2 tables? [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 1 year ago.
Improve this question
I am a student and is new to MySQL
I have 2 tables, ors_uniform contains 7 products that has 9 sizes (XS-5XL).
ors_prices contains the fk of table1 and the prices per size (9 sizes)
I wanted it so that when I output the inventory of the products, the price will be shown next to it. I know how to do that using php already but I don't know what MySQL query to use.
I currently just use SELECT * FROM ors_uniform
2 tables
let's assume the fk is a foreign key and it is available in both tables.
select * from ors_uniform join ors_prices on ors_uniform.fk = ors_prices.fk;

Sort By date in hasMany relation first record for Laravel project [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 1 year ago.
Improve this question
I have a courses table and every course has many sessions,
So I need to sort courses by the first session of the course date for Laravel Project.
this way you can set a method in Course model to get the oldest Session model(which means first session):
public function oldestSession()
{
return $this->hasOne(Session::class)->oldestOfMany();
}
and then you may simply query your course model by adding constraints on that method :
$courses = Course::with(['oldestSession' => function ($query) {
$query->orderBy('created_at', 'desc');
}])->get();

I need to get sum of difference of two columns in each row [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
i have a table called sales, there are 100 rows, table has 2 fields, namely total and commission, i want to get SUM(total-commission) of all rows into a single value
You can user Eloquent Builder sum() function.
$sum = Sale::select(
DB::raw('commissioned_total as total - commission')
)
->sum('commissioned_total');
Or you can user Laravel Collection sum() function.
$sum = Sale::all()->sum(function($sale) {
return $sale->total - $sale->commission;
});
You can enhance this method more,
Define this commissionedTotal() function on Sale model, and use sum function as Higher Order Message.
Sale Model
public function commissionedTotal()
{
return $this->total - $this->commission;
}
Controller
$sum = Sale::all()->sum->commissionedTotal()
This third approach is more elegant and It's the Laravel preferred way.
As per you state in question there only two fileds a sample query may be:
SELECT SUM(s.total-s.commission) as myResult from sales as s WITH (NOLOCK);

Delete articles from db decrease/increase order php [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
First of all I wish you a new happy year! So I have 3 articles for example and each of them has an order, like 1,2,3. If I want to delete the last article for example, the order of the other articles still remain the same 1,2. But if I delete the first article the orders of the others should decrease with 1 and if I delete the middle one only the order of the last article should decrease.
Now how to do this in php? I refer to delete in database
Any idea is appreciated.
I don't have any code yet because I wanted to find first a solution, I only give the id of the article, the order and the category from which it belongs:
if($_POST['actiune'] == 'updateArticle'){
$id_article = $_GET['id'];
$ord = $_GET['order'];
$categ = $_GET['categ'];
}
You could do an update after each delete. Something like:
UPDATE [ArticleTable] SET [OrderColumn]=[OrderColumn]-1 WHERE [OrderColumn]>=[ORDERVALUE_FROM_DELETED_ITEM]

Need search a value from database from comma separated string [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have a table in database that is TEXT field. In this table i have stored my product ids . Now when i go to cart and get all product id of cart i want to match from database field and get only those ids that there stored in product id.
Please see image first.
In this field you will see "check_values" field. here all product ids are stored in comma separated.
Let me take a example
Like i have purchase a product that product id is 161. So i want to match 161 id from "check_value" filed and get only those ids (from images) that having 161.(11,14,15).
Hope you understand my question.
You can use FIND_IN_SET e.g.
SELECT * FROM images WHERE FIND_IN_SET(161, check_values) > 0

Categories