Below is my query which one i used and working fine for me.
if($t_requested['category_id'])
{
$t_query_condition['p.category_id'] = $t_requested['category_id'];
}
if($t_requested['brand_id'])
{
$t_query_condition['p.brand_id'] = $t_requested['brand_id'];
}
$browseData = DB::table('products as p')
->leftjoin('categories as cp', 'p.category_id', '=', 'cp.id')
->leftjoin('brands as bp', 'p.brand_id', '=', 'bp.id')
->select('p.id as product_id','p.name as product_name','cp.title as category_name','bp.name as brand_name','p.product_weight',
'p.short_description','p.product_price','p.special_price','p.stock_quantity')
->orderBy('p.created_by', "desc")
->offset(0)
->limit(10)
->where($t_query_condition)
->get();
but Now i have getting multiple id in "category_id" and "brand_id", i want to used whereIn but it used with of condition. if i get category_id or brand_id null then it's skip.
thanks in advance.
Try this query hope you will get result
$browseData = DB::table('products as p')
->select('p.id as product_id','p.name as product_name','cp.title as category_name','bp.name as brand_name','p.product_weight',
'p.short_description','p.product_price','p.special_price','p.stock_quantity')
->leftjoin('categories as cp', 'p.category_id', '=', 'cp.id')
->leftjoin('brands as bp', 'p.brand_id', '=', 'bp.id')
->orderBy('p.created_by', "desc");
if($t_requested['category_id'])
{
$browseData->whereIn('p.category_id',$t_requested['category_id']);
}
if($t_requested['brand_id'])
{
$browseData->whereIn('p.brand_id',$t_requested['brand_id']);
}
$result=browseData->offset(0)->limit(10)->get();
I think it is very simple, you have to pass array of values instead of one value
I have tried to write options for achieve this, you have to follow which is suitable for you
if($t_requested['category_id'])
{
$t_query_condition['p.category_id'] = $t_requested['category_id']; // it should be array OR
$t_query_condition['p.category_id'] = explode(",",$t_requested['category_id']); // if you get value comma saperated
$t_query_condition['p.category_id'] = explode(",",$t_requested['category_id']); // if you get value comma saperated
$t_query_condition['p.category_id'] = [$t_requested['category_id']]; // if you want to convert one value into array
}
Related
I want to show unique location names and ids and also i want it as array, if i didint use groupBy it will works and showing data as object. My requiremnt is to retrive it as unique array and its not working.
I dont know what is the error in code
public function showClinicLocations($id)
{
$clinic = Clinic::find($id);
$locations = Location::where('clinicID', $id)->get();
$locationservices = Service::select('services.serviceName as servicename','locations.locationID as locid','locations.locationName as locname')
->groupBy('locid')
->join('location_services', 'location_services.serviceID', '=', 'services.serviceID')
->join('locations', 'locations.locationID', '=', 'location_services.locationID')
->join('clinics', 'clinics.clinicID', '=', 'locations.clinicID')
->where('clinics.clinicID','=',$id)
->get();
echo $locationservices;
die();
// $locationservices = json_encode(array_unique($locationservices));
return view('clinic.locations')->with(['locations' => $locations ,'clinic'=>$clinic , 'services'=> $locationservices]);
}
Can you please help me
I have uploaded output
If there is multiple same loc id only one loc id should be shown
Hello community in Laravel I am doing a search, an array listing returns me, it's perfect. I want to return a subarray with a list, how is it done with select? using db :: table in laravel
DB::table("campania_post AS pt")
->join("campania AS c","c.id","=","pt.campania_id")
->where("c.manager_id", "=", $user->id)
->leftJoin("files AS f","f.id","=","c.avatar")
->where("pt.status", "=", 0)
->select("c.id", "c.nombre", "c.avatar", "f.name", "c.created_at", 'f.id as file_picture')
->groupby("c.id")
->get();
$data->map(function ($singleData) {
$singleData['img'] = ['Add your sub array'];
return $singleData; });
That's all.
For Query Builder:
You can use GROUP_CONCAT to a string. And then use map on the result to convert it to array.
$data = DB::table("campania_post AS pt")
->join("campania AS c","c.id","=","pt.campania_id")
->where("c.manager_id", "=", $user->id)
->leftJoin("files AS f","f.id","=","c.avatar")
->where("pt.status", "=", 0)
->select("c.id", "c.nombre", "c.avatar", "f.name", "c.created_at", DB::raw("GROUP_CONCAT(f.id, ',') AS file_picture"))
->groupby("c.id")
->distinct()
->get();
$data->map(function($q) {
$q->file_picture = explode(',', $q->file_picture);
return $q;
});
I am trying to get a result set from a laravel eloquent query whereby I match a column against a list of values in an array.
$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)
->where('entity_type_id', '=', $operation_entity_id)
->pluck('entity_access_id')->toArray();
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids);
return view('page.index')->withOperations($authenticated_operations);
You can try it as:
$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)->where('entity_type_id', '=', $operation_entity_id)->pluck('entity_access_id')->toArray();
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)->get();
return view('page.index')->withOperations($authenticated_operations);
Add get() at the end of the query.
1) pluck returns a single value from a single row. You want lists to get a single column from multiple rows. toArray may not be needed, as it returns an array of values
$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)
->where('entity_type_id', '=', $operation_entity_id)
->lists('entity_access_id');
2) You're forgetting to actually retrieve the rows in your second line:
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)
->get();
You have to call get() function on the result set to obtain results. The modified code will be like
$authenticated_operation_ids = AccessControl::where('user_id', '=', $user_id)->where('entity_type_id', '=', $operation_entity_id)->get()->pluck('entity_access_id');
$authenticated_operations = Operation::whereIn('id', $authenticated_operation_ids)->get();
return view('page.index')->withOperations($authenticated_operations);
or you can use a cursor to process it.
I have two queries running in my controller. I need a value from the first query to be passed into the second. I want the result of both these queries sent to my view.
public function jobs()
{
$query = DB::table("dbQuotes")
->leftJoin("dbACT", "dbQuotes.act_id", "=", "dbACT.ID")
->leftJoin("dbOpps", "dbQuotes.act_id", "=", "dbOpps.contactID")
->leftjoin('dbBids', 'dbQuotes.act_id','=',
DB::raw('dbBids.quote_id AND dbBids.user_id = '. Auth::user()->id))
->where("dbQuotes.active", "=", "1")
->select("dbQuotes.*", "dbACT.*", "dbBids.*",
(DB::raw('date_format(dbQuotes.posted_date, "%d/%m/%Y %H:%i") as posted_date')),
(DB::raw('date_format(dbOpps.expected_date, "%d/%m/%Y") as expected_date')))
->groupBy("dbQuotes.id")
->orderBy("posted_date", "desc")
->get();
$passinvaluehere = $query->dbQuotes.act_id
$bids = DB::table("dbBids")
->where("quote_id", "=", $passinvaluehere)
->get();
return view('jobs', ['query' => $query,'bids' => $bids]);
}
My query works and the view is established in the correct way if I replace the passed value with a number, i.e "8763". My question is how, within this function, can I pass the value/s of dbQuotes.act_id into this second query?
***UPDATED Code from answer: [error Call to a member function lists() on a non-object]
public function jobs()
{
$query = DB::table("dbQuotes")
->leftJoin("dbACT", "dbQuotes.act_id", "=", "dbACT.ID")
->leftJoin("dbOpps", "dbQuotes.act_id", "=", "dbOpps.contactID")
->leftJoin('dbBids', 'dbQuotes.act_id','=',
DB::raw('dbBids.quote_id AND dbBids.user_id = '. Auth::user()->id))
->where("dbQuotes.active", "=", "1")
->select("dbQuotes.*", "dbACT.*", "dbBids.*",
(DB::raw('date_format(dbQuotes.posted_date, "%d/%m/%Y %H:%i") as posted_date')),
(DB::raw('date_format(dbOpps.expected_date, "%d/%m/%Y") as expected_date')))
->groupBy("dbQuotes.id")
->orderBy("posted_date", "desc")
->get();
$act_id = $query->lists('act_id');
$bids = DB::table("dbBids")
->whereIn("quote_id", $act_id)
->get();
return view('jobs', ['query' => $query,'bids' => $bids]);
}
If you have multiple records (as per the ->get() method) you have two ways: either you loop over the Collection and make a query each iteration (bad) or you create an array of ids and use a whereIn in the second query (better):
$passinvaluehere = $query->lists('act_id');
// https://laravel.com/docs/5.2/queries#retrieving-results
// this creates and array of `act_id` s
$bids = DB::table("dbBids")
->whereIn("quote_id", $passinvaluehere)
->get();
// you now have a Collection of multiple $bids
If you expect only a single records from your first query, you need to change the fetcher method, using first() instead, or else take only the first element of your actual collection, something like first($query) or $query[0]
$query = DB::table("dbQuotes")
....
->first();
$passedvaluehere = $query->act_id;
I'm trying to return a query that has an arbitrary amount of where clauses based on the number of tags a user submits.
//unknown number of ids to query on
$tag_ids = array(1,5);
//multiple joins with closure
$items = DB::table('archives_items_metadata')->join('tags', 'archives_items_metadata.tag_id', '=', 'tags.id')->join('archives_items', 'archives_items_metadata.archive_item_id', '=', 'archives_items.id')->join('items', 'archives_items.item_id', '=', 'items.id')
->where(function ($query) use ($tag_ids) {
foreach ($tag_ids as $tag_id)
{
$query->where('archives_items_metadata.tag_id', $tag_id);
}
})->get();
The result I get is an empty array even though when I try array(1) or array(5) by themselves, they both return the same item. What am I missing?
EDIT::
I'm looking to return items that have each of the tag ids specified. The reference of items to tags is stored on the archives_items_metadata table. How can I get the result I'm expecting, and what's the most efficient way to accomplish this?
You are looking to do a WHERE tag_id IN (1, 2, 3) style clause laravel has the whereIn($col, $vals) builder function.
->whereIn('archives_items_metadata.tag_id', $tag_ids)
search for "whereIn" in the official docs
$tag_count = count($tag_ids);
$items = DB::table('archives_items')->join('archives_items_metadata', 'archives_items_metadata.archive_item_id', '=', 'archives_items.id')->join('tags', 'archives_items_metadata.tag_id', '=', 'tags.id')
->whereIn('archives_items_metadata.tag_id', $tag_ids)->whereNull('archives_items_metadata.deleted_at')
->groupBy('archives_items_metadata.archive_item_id')->havingRaw('count(*)='.$tag_count->get();