So I'm trying to use a query on selecting products that are on Critical Level. So basically, if the product's quantity is lower than its reorder_point, it'll be considered as Critical.
Here's my query that I'm using:
$products = DB::table('inventory')
->where('quantity', '<=', 'reorder_point')
->orderBy('quantity', 'asc')
->get();
But it only shows once the quantity of that row is set to 0 or less. So I'm asumming that the value of re_orderpoint in the where condition is 0.
But everything works when I use this query in phpMyAdmin:
SELECT * from inventory where quantity <= reorder_point
Laravel gives you whereColumn for comparing columns of same table. You can do it like this:
$products = DB::table('inventory')
->whereColumn('quantity', '<=', 'reorder_point')
->orderBy('quantity', 'asc')
->get();
See docs here.
Hope you understand.
Related
I want to get all the data from SALES and PURCHASES table but I want to show product name for the single time and also want to show the sum of its quanitity..
How can i do this in laravel 8 ?
What will be the query ??
$stock = DB::table('products')
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('product_id')
->get();
I dont have your whole query but use havingRaw to find the duplicates.
$dups = DB::table('tableName')
->select('col1_name','colX_name', DB::raw('COUNT(*) as `count`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('col1_name', 'ColX_name')
->havingRaw('COUNT(*) > 1')
->get();
Maybe something like that
$stock = DB::table('products')
->select('sales.*','purchases.*','products.name', DB::raw('products.name as `NbProduct`'))
->join('purchases','purchases.product_id','products.id')
->join('sales','sales.product_id','products.id')
->groupBy('sales.id', purchases.id)
->get();
I want to write the following query in Laravel 5.
SELECT *
FROM loans
WHERE loanAmount > (SELECT IFNULL(SUM(paidLoanAmount), 0)
FROM paid_loans WHERE loanId = loans.id);
I have tried the following, but subquery of PaidLoan seems not work due to loans.id or something else I don't notice.
Loan::where(
'loanAmount',
'<=',
PaidLoan::where('paid_loans.loanId', 'loans.id')
->get()
->sum('paidLoanAmount')
)->get();
You can use a leftJoin on the loan and just add a normal condition.
$payedLoans = Loan::leftJoin('paid_loans', 'paid_loans.loanId', '=', 'loans.id')
->where('loanAmount', '<=', \DB::raw("IFNULL(sum('paid_loans.paidLoanAmount'), 0)"))
->groupBy('loans.id')
->get();
I have a customers table and Customer as Model. I am trying to get all the customers except customer category with id 1. The customer_category_id can be nullable.
I have following query.
Customer::where('customer_category_id', '!=', 1)->with('customerMeta')->get();
Above query doesn't give the customers with null customer category id. What would be the reason ?
I need all the customers with category null or category not equals to 1
Any kind of suggestions are appreciated.
This should work:
Customer::where(function ($query) {
$query->where('customer_category_id', '!=', 1)
->orWhereNull('customer_category_id')
})
->with('customerMeta')
->get();
Good luck!
Because null is also not equal to 1. You should probably add ->whereNotNull('customer_category_id') to your query:
Customer::where('customer_category_id', '!=', 1)
->whereNotNull('customer_category_id')
->with('customerMeta')
->get();
That query should work.
You could try and run it in artisan tinker. Before you run the query activate query logging:
DB::connection()->enableQueryLog();
Then run the query.
Afterwards, run:
DB::getQueryLog();
You'll now see the query that was created. Copy this query into a SQL Client and tweak it.
Hope this helps.
Change your query like this:
Customer::where('customer_category_id', '!=', '1')->with('customerMeta')->get();
Because the 1 also means true, so you have to use a string .
If you want to return the null category as well, you can use whereNull
Customer::where('customer_category_id', '!=', 1)
->whereNull('customer_category_id')
->with('customerMeta')
->get();
or you can use multiple where clauses like this:
$query->where([
['customer_category_id', '!=', '1'],
['customer_category_id', '=', null],
...
])
I want to set all records order by FIELD_NAME but only 1 record with specific id need to be remain at first.
How can i achieve this using laravel query builder
My current query is like,
$orders =Order::orderBy('status', 'desc')
->where('status','<>',5)
->paginate(10);
And i want id=123 stay at first.
Use orderByRaw and simple sql condition:
$orders = Order::orderByRaw('IF(id = 123, 0,1)')->orderBy('status', 'desc')
->where('status', '<>', 5)
->paginate(10);
I'm new to laravel and I have some issues with the query builder.
The query I would like to build is this one:
SELECT SUM(transactions.amount)
FROM transactions
JOIN categories
ON transactions.category_id == categories.id
WHERE categories.kind == "1"
I tried building this but it isn't working and I can't figure out where I am wrong.
$purchases = DB::table('transactions')->sum('transactions.amount')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->select('transactions.amount')
->get();
I would like to get all the transactions that have the attribute "kind" equal to 1 and save it in a variable.
Here's the db structure:
transactions(id, name, amount, category_id)
categories(id, name, kind)
You don't need to use select() or get() when using the aggregate method as sum:
$purchases = DB::table('transactions')
->join('categories', 'transactions.category_id', '=', 'categories.id')
->where('categories.kind', '=', 1)
->sum('transactions.amount');
Read more: http://laravel.com/docs/5.0/queries#aggregates
If one needs to select SUM of a column along with a normal selection of other columns, you can sum select that column using DB::raw method:
DB::table('table_name')
->select('column_str_1', 'column_str_2', DB::raw('SUM(column_int_1) AS sum_of_1'))
->get();
You can get some of any column in Laravel query builder/Eloquent as below.
$data=Model::where('user_id','=',$id)->sum('movement');
return $data;
You may add any condition to your record.
Thanks
MyModel::where('user_id', $_some_id)->sum('amount')