Laravel displaying elements distinctly - laravel

public function employeebanvisitor(Request $request) {
$vis = bookingmodel::distinct('visitorname')
->select(
'id',
'visitorname',
'visitorphonenumber',
'compname',
'designation'
)
->where('empmail',Auth::user()->email)
->get();
return view('employee.employeebanvisitor',compact('vis'));
}
In the above query i need to display the list distinctly where the the distinct element is visitorname.But this command does not return elements distinctly.Any Help or suggestion is most welcomed.

try this :
$vis = bookingmodel::select(
'id',
'visitorname',
'visitorphonenumber',
'compname',
'designation'
)
->where('empmail',Auth::user()->email)
->distinct('visitorname')
->get();

Distinct does not take any argument in laravel. You can refer to the laravel api. But what you can do is you can set the column that you want to set as distinct to be the first column in your select statement for it to work.
$vis = bookingmodel::select(
'visitorname',
'id',
'visitorphonenumber',
'compname',
'designation'
)
->distinct()
->where('empmail',Auth::user()->email)
->get();

Related

Laravel Eloquent - Filter Relationship of Relationship

This is the query I tried to do:
$products = Product::with(['cross_selling.product_related' =>
fn($query) => $query->where('status_id', '=', '2')])
->where('status_id',2)
->get();
I get this results:
In cross_selling I still get id:12 that doesn't has any product_related. I need some help for making that the query doesn't give me that item if product_related is null.
Tks.
First get only those cross selling which has any related products after that get related products. Use the whereHas condition on cross_selling.
$products = Product::with(['cross_selling' => function($qry){
$qry->whereHas('product_related', function ($q) {
$q->where('status_id', 2);
});
}, 'cross_selling.product_related' =>
fn($query) => $query->where('status_id', '=', '2')])
->where('status_id',2)
->get();
Try this
$products = Product::with(['cross_selling.product_related' =>
fn($query) => $query->where('status_id', '=', '2')])
->has('cross_selling.product_related')
->where('status_id',2)
->get();
If you don't want cross selling products with no related products you can add
->whereHas('product_related')
Which basically says: Get me only cross selling which have 'product_related'.
In order to do it properly, you can:
$callback = fn($query) => $query->where('status_id', 2);
$products = Product::whereHas('cross_selling.product_related', $callback)
->with(['cross_selling.product_related' => $callback])
->where('status_id',2)->get();
Now the callback is applied to the whereHas() and the with() plus you can pass additional constraints using $callback.

Filter record through dates not working laravel

I have a expiry_date (type=date) column in my table
$currentDate = date('Y-m-d');
$Data = Post::whereDate('expiry_date','<=',$currentDate)->where(['status' => 'active'])->orWhere(['p_id' => 3])->select('id','title','status','p_id','expiry_date')->orderBy('id', 'DESC')->get();
i want to filter data if current date is greater than expiry date then those record should not be shown but in my scenario i'm still getting record.
Any solution Thanks.
You must group orWhere clause in closure. Grouping in closure is like () in real query.
$Data = Post::whereDate('expiry_date','<=',$currentDate)
->where(function($query){
return $query
->where(['status' => 'active'])
->orWhere(['p_id' => 3]);
})
->select('id','title','status','p_id','expiry_date')
->orderBy('id', 'DESC')
->get();
But, because I don't know your project - i may wrong with grouping.

How to fix Laravel query builder where clause integer variable translated to string

I have a function to get a pass a language number to get language categories record for API purpose. I use a database query statement to select categories table and join the category language table to get category id, parent_id and name (specified language). When execute return error and select the underlying SQL converted the language value to string (e.g. languages_id = 1). I google a lot and no ideas what's wrong. Can anyone advise how to resolve. Thanks a lot.
I tried to copy the underlying SQL to MySQL Workbench and remove the languages_id = 1 --> languages_id = 1 can working properly. I guess the 1 caused error.
Code Sample:
private function getCategories($language) {
$categories = DB::table('categories')
->select(DB::raw('categories.id, categories.parent_id, categories_translation.name'))
->join('categories_translation', function($join) use ($language) {
$join->on('categories_translation.categories_id', '=', 'categories.id');
$join->on('categories_translation.languages_id', '=', $language);
})
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id]
])
->get();
return $categories;
}
Error return the converted SQL:
"SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on
clause' (SQL: select categories.id, categories.parent_id,
categories_translation.name from categories inner join
categories_translation on categories_translation.categories_id =
categories.id and categories_translation.languages_id = 1
where (parent_id = 0 and categories.id = 1))"
You are trying to join using a comparison to an scalar value, instead of a column. I think you actually want to put that comparison as a "where" condition, rather than a "join on"
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id],
['categories_translation.languages_id', '=', $language]
])
there is another thing i just discover with your code. when joining table, you are suppose to be joining 'categories_translation.languages_id' with another table id field. in your case, it is not so. you are not joining 'categories_translation.languages_id' with any table field. so ideally, what you are going to do is this
private function getCategories($language) {
$categories = DB::table('categories')
->select(DB::raw('categories.id, categories.parent_id, categories_translation.name'))
->join('categories_translation', function($join) use ($language) {
$join->on('categories_translation.categories_id', '=', 'categories.id');
})
->where([
['parent_id' ,'=', '0'],
['categories.id', '=', $id]
['categories_translation.languages_id', '=', $language]
])
->get();
return $categories;
}
hope this helps

Convert mysql query into query builder

Hello Guys I am new with the query builder of laravel.
I want to convert this query into laravel query builder. Thank you in advance.
$bo_facilities = ' SELECT
a.bo_facility_code,
a.bo_facility_groupcode,
a.bo_number,
a.bo_indYesOrNo,
a.bo_indLogic,
b.bo_content_facility_description
FROM
bo_facilities AS a
RIGHT JOIN bo_content_facilities AS b
ON b.bo_content_facility_code = a.bo_facility_code AND b.bo_content_facility_facilityGroupCode = a.bo_facility_groupcode
WHERE a.bo_hotel_code = "1" GROUP BY b.bo_content_facility_description';
My groupBy method didnt work..
$join->groupBy('b.bo_content_facility_description');
this is my whole code
I didn't know the best way.
DB::table('bo_facilities AS a')
->select("a.bo_facility_code", "a.bo_facility_groupcode", "a.bo_number", "a.bo_indYesOrNo", "a.bo_indLogic", "b.bo_content_facility_description")
->join('bo_content_facilities AS b', function ($join) {
$join->on('b.bo_content_facility_code', '=', 'a.bo_facility_code');
$join->on('b.bo_content_facility_facilityGroupCode', '=', 'a.bo_facility_groupcode');
$join->groupBy('b.bo_content_facility_description');
})
->where('a.bo_hotel_code', $hotelCode)
->get();
Finally I already fixed my problem.
$bo_facilities_raw = DB::table('bo_facilities AS a')
->join('bo_content_facilities AS b', function ($join) {
$join->on([['b.bo_content_facility_code', '=', 'a.bo_facility_code'], ['b.bo_content_facility_facilityGroupCode', '=', 'a.bo_facility_groupcode']]);
})
->select("a.bo_hotel_code","a.bo_facility_code", "a.bo_facility_groupcode", "a.bo_number", "a.bo_indYesOrNo", "a.bo_indLogic", "b.bo_content_facility_description", "b.bo_content_facility_code", "b.bo_content_facility_facilityGroupCode")
->where('a.bo_hotel_code', 1)
->get();
$bo_facilities_decode = json_decode($bo_facilities_raw, true);
$remove_dupli = array_unique(array_column($bo_facilities_decode, 'bo_content_facility_description'));
$bo_facilities = array_intersect_key($bo_facilities_decode, $remove_dupli);
the duplication of records was inside the multidimensional array thats why i use array unique and array intersect key for removing the duplicate descriptions.

Eloquent / Laravel - Putting a WHERE Clause on a Reference Table With Chained Relationships

I have the following relationship functions in my Job model:
public function resourceTypes(){
return $this->belongsToMany('ResourceType', 'job_requests');
}
public function resources(){
return $this->belongsToMany('Resource', 'jobs_resources')->withPivot('flow_type', 'resource_type_id');
}
I am able to get an object with data from both of the above relationships using:
$job = Job::findorfail($projectId);
$result = $job->with('resources.resourceTypes')->get();
I would like to put a where clause on the jobs_resources pivot table - specifically on the column flow_type.
How would I do this?
Try something like this:
$job = Job::with('resources' => function($q) {
$q->with('resourceTypes')->where('flow_type',2);
})->findorfail($projectId);
In above you will get only those resources with flow_type = 2
I ended up using the following statement:
Job::with(['resources' => function ($query){
$query->wherePivot('flow_type', '=', '1' );
}, 'resources.resourceTypes'])->where('id', $projectId)->firstOrFail();
$result = DB::table('job')
->join('job_resources', 'job.id', '=', 'job_resources.job_id')
->join('job_requests', 'job_resources.request_id', '=', 'job_requests.id')
->where('job_resources.flow_type', '=', CONDITION)
->get();
Your table data is not clear from your input, but this method (query builder) should work

Resources