How to insert the value into query? - laravel

Do I need to insert the variable in sum(orderdets.quantity) into another variable?
$order = Order::with('customer','product')
->select(
'orders.id',
'orders.customer_id',
'orderdets.product_id',
DB::raw('SUM(orderdets.quantity)'))
->get();

Try This
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
dd($orders);

Use AS tempName to get the column data by specific name. this way it can be accessed.
$order = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) AS sumOrders'))->get();

$orders = Order::with('customer','product')
->select('orders.id', 'orders.customer_id', 'orderdets.product_id'))
->get()->map(function($order){
$order->put('sum', $order->orderdets->sum('quantity');
})->save();
This will sum all your debts and will also update orders table in one go, Although you question wasn't clear but I think you must be looking for something like this

Please try below script in your controller.
$orders = Order::with('customer','product')->select('orders.id', 'orders.customer_id', 'orderdets.product_id', DB::raw('SUM(orderdets.quantity) as sum'))->get();
return $orders;
Thanks
PHPanchal

Related

Wildcard-like syntax in an eloquent Where clause to search between two strings

I saw the answer provided in this question Wildcard-like syntax in an eloquent Where clause? now I want to know if is there a way to search between two strings?.
basicasicaly in my code I want to show requests that have a status of new or scheduled.
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->where('status', 'LIKE','%New%')
->get();
you can use whereIn ,The whereIn method verifies that a given column's value is contained within the given array:
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->whereIn('status', ['new','scheduled'])
->get();
You can use:
->where('status', 'new')
->orWhere('status', 'scheduled')
you can simply use a where with an orWhere:
$requests = DB::table('requests')
->select('requestDate','requestTime','status','requestID')
->where('requestorID', '=',$userID)
->where(function($q) {
$q->where('status', 'LIKE','%New%');
$q->orWhere('status', 'LIKE','%Scheduled%');
})->get();

How to select min and max in laravel

I want to convert this code in laravel.
SELECT MAX(date_start) AS DateStart,MIN(date_end) AS DateEnd FROM DBTest
And I try this code
$data = DB::table('DBTest')
->select(max('date_start'), min('date_end')))
->get();
Return Error: max(): When only one parameter is given, it must be an array
I am using laravel 5.2, and SQLyog as database
I am confuse in syntax please help me
You can't use functions in select statement, but you can use raw SQL :
$data = DB::table('DBTest')
->select(\DB::raw('MIN(date_start) AS DateStart, MAX(date_end) AS DateEnd'));
->get();
You can do it like this:
For the max start date:
max = DB::table('DBTest')->select('date_start')->orderBy('date_start', 'desc')->first();
For min end date:
min = DB::table('DBTest')->select('date_end')->orderBy('date_end', 'asc')->first();
You have to use something called selectRaw method in Laravel in order to achieve this result. Chaining method like ->max('columnA')->min('columnB') will not work. So, here is the solution:
$data = DB::table('DBTest')
->selectRaw('MAX(date_start) AS DateStart, MIN(date_end) AS DateEnd')->get();
Try MIN() and MAX() functions in the sql query.
$data = DB::table('DBTest')
->select(\DB::raw('MIN(date_start) AS startDate, MAX(date_end) AS endDate'));
->get();

How to get string value using eloquent query

I am trying to execute a query using eloquent, however it returns me an array. What I want is to only get the string value of my query.
This is what I get
[{"name":"hey"}] [{"name":"sdasdasd"}]
Here's my eloquent query:
$categoryName = Category::select('name')->where('id', request('category'))->get();
$subCategory = Subcategory::select('name')->where('id', request('subCat'))->get();
as per the Laravel Docs
Retrieving A Single Row / Column From A Table
If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single stdClass object:
$user = DB::table('users')->where('name', 'John')->first();
echo $user->name;
If you don't even need an entire row, you may extract a single value from a record using the value method. This method will return the value of the column directly:
$email = DB::table('users')->where('name', 'John')->value('email');
so in your example maybe you need to do something like the following :
$subCategory = Subcategory::select('name')->where('id', request('subCat'))-> first();
echo $subCategory->name;
There are so many ways to achive your goal.
Solutions
Use pluck().
$categoryName = Category::select('name')
->where('id', request('category'))
->pluck()
->first();
Use model properties.
$categoryName = Category::find(request('category'))
->name;
If you take only name then follow the below code.
$categoryName = Category::where('id', request('category'))->first()->name;
$subCategory = Subcategory::where('id', request('subCat'))->first()->name;
echo $categoryName;
echo $subCategory;
If you take all columns in a row follow the below code:
$categoryName = Category::where('id', request('category'))->first();
$subCategory = Subcategory::where('id', request('subCat'))->first();
echo $subCategory->name;
echo $categoryName->name;
I hope this i may help you.Try this.
You can get the answer using
$name = DB::table('Category')->where('id','=',request('category'))->pluck('name')->first();
echo $name;
$name = Category::select('name')->where('id', request('category'))->take(1)->orderBy('id','asc')->first()->name;
echo $name;
OR
echo Category::select('name')->where('id', request('category'))->take(1)->orderBy('id','asc')->first()->name;

How to select all column from multiple tables in codeigniter using join?

I have 4 tables like this:
Now, I want to select all columns from all tables where model.featured=1. i.e.
model.id
model.name
model_attributes.id
model_attributes.attributes_value
model_images.id
model_images.model_images
attributes.id
attributes.name
attributes.value
I can only do basic level queries and I'm not sure if I'm anywhere near to the solution but this is what I have tried (returns nothing):
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model.id = model_images.model_id','RIGHT');
$this->db->join('model_attributes','model.id = model_attributes.model_id','RIGHT');
$this->db->join('attributes','model_attributes.attributes_id = attributes.id','RIGHT');
$this->db->where('model.featured', 1);
$query = $this->db->get();
return $query->result();
How do I achieve what I want ? Or, is there any other better methods to do it ?
// try this query
$this->db->select('*');
$this->db->from('model');
$this->db->join('model_images','model_images.model_id = model.id');
$this->db->join('model_attributes','model_attributes.model_id = model.id');
$this->db->join('attributes','attributes.id = model_attributes.attributes_id');
$this->db->where('model.featured','1');
$query = $this->db->get()->result();
return $query;
Please go through below mentioned solution.
$this->db->select('model.*,model_images.*,model_attributtes.*, attributes.*'):
Let me know if it not works for you.
Please go through below solution. If multiple table has same column name then you have to give alias for each column name which have same name in both the table. because by default CI merge all column name and generate result.
$this->db->select('*,c.name as c_name,s.name as s_name');
$this->db->from('country c');
$this->db->join('state s', 'c.id = s.country_id');
$query = $this->db->get();

Order by is not working?

I have a problem with order by articles by priority. Where is working. Any suggestion?
$articles = Articles::whereHas('priority',function($query){
$query->orderBy('order','asc');
// $query->where('order','=',1);
})->limit(7)->get();
You have to use join to fetch the articles by the order of related table column as:
Articles::join('priority', 'articles.id', '=', 'priorities.article_id')
->orderBy('priorities.order','asc')
->select('articles.*')
->limit(7)
->get();
You can do the following:
$quotes=Articles::orderBy('priority', 'desc')->limit(7)->get();
Try this code
$articles = Articles::orderBy('order','desc')->limit(7)->get();
and if you want to add a condition you can use somthing like this:
$articles = Articles::join('priorities','articles.id','=','priorities.articale_id')->where('priority',1)->orderBy('order','desc')->limit(7)->get();

Resources