Laravel 8 Carbon MM/DD/YYYY to Age compared inside join condition - laravel

Table: Search Preferences
Column: search_min_age (two digit number)
Table: Users
Column: birthdate (mm/dd/yyyy)
Both of these are joined together, but here is my error.
Could not parse 'users.birthdate': DateTime::__construct(): Failed to parse time string (users.birthdate) at position 0 (u): The timezone could not be found in the database
Here is snippet from my code:
$users = DB::table('users')
->join('search_preferences', function($q) {
$q->on('search_preferences.search_type', '=', 'users.account_type')
->on('search_preferences.search_identity', '=', 'users.account_identity')
->on('search_preferences.search_gender', '=', 'users.gender')
->on('search_preferences.search_tribe', '=', 'users.tribe')
->on('search_preferences.search_position', '=', 'users.position')
->on('search_preferences.search_min_age', '<=', Carbon::parse('users.birthdate')->age)
->on('search_preferences.search_max_age', '>=', Carbon::parse('users.birthdate')->age);
})
->where('users.verified_id', '=', 'yes')
->where('search_preferences.user_id', '=', auth()->user()->id)
->orderBy('users.id', 'ASC')
->select('users.*')
->paginate(9);

Related

Laravel 8 - How to write statement with paginate instead with get

I use Alias name of column because I have two columns in different tables with the same name, when retrieve them one column will override another column
DB::table('trip_user')
->join('trips', 'trips.id', '=', 'trip_id')
->join('users', 'users.id', '=', 'user_id')
->where('guide_id','=',$id)
->get(['*','trips.name As tirp_name','trip_user.id As reservation_id'])
How can I write it with paginate?
Try it this way:
$result = DB::table('trip_user')
->join('trips', 'trips.id', '=', 'trip_id')
->join('users', 'users.id', '=', 'user_id')
->where('guide_id','=',$id)
->paginate(100, ['*','trips.name As tirp_name','trip_user.id As reservation_id']);

How to join two queries as one in whereIn

In my Laravel-5.8 I have these queries:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', $employee_with_goals)
->get();
$employee_with_goals = DB::table('hr_employees')
->join('appraisal_goals', 'hr_employees.employee_code', '=', 'appraisal_goals.employee_code')
->select('hr_employees.employee_code')
->where('hr_employees.company_id', $userCompany)
->where('hr_employees.hr_status',0)
->where('appraisal_goals.is_published', '=', '1')
->where('appraisal_goals.is_approved', '=', '3')
->groupBy('hr_employees.employee_code')
->get();
$manager is the main query
How do I combine these two queries as one using $employee_with_goals as the variable in whereIn()?
You can use the pluck method to retrieve all values for a given key in a collection:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', $employee_with_goals->pluck('employee_code'))
->get();
You can actually use a subquery too:
$manager = DB::table('hr_employees')
->select('line_manager_id')
->where('hr_status',0)
->whereIn('employee_code', function ($query) use ($userCompany) {
$query
->table('hr_employees')
->select('hr_employees.employee_code')
->join('appraisal_goals', 'hr_employees.employee_code', '=', 'appraisal_goals.employee_code')
->where('hr_employees.company_id', $userCompany)
->where('hr_employees.hr_status',0)
->where('appraisal_goals.is_published', '=', '1')
->where('appraisal_goals.is_approved', '=', '3')
->groupBy('hr_employees.employee_code')
})
->get();
The code above is untested.

Concatenate two field into a alias name laravel

$data = DB::table('tables')
->select('orders.order_id', 'users.*')
->leftJoin('lists', 'lists.order_id', '=', 'orders.order_id')
->leftJoin('users', 'users.user_id', '=', 'lists.user_id')
->where('users.type', '=', 'admin')
->get();
dd($data);
getting first_name and last_name how can i group those i.e alias says full_name
fullname => Max jones
Concatenate two field into a alias name laravel
used raw method laravel and mysql concat method used here
DB::raw("CONCAT(`users`.`first_name`,`users`.`last_name`) as full_name")
try
$data = DB::table('tables')
->select('orders.order_id', DB::raw("CONCAT(`users`.`first_name`,`users`.`last_name`) as full_name"))
->leftJoin('lists', 'lists.order_id', '=', 'orders.order_id')
->leftJoin('users', 'users.user_id', '=', 'lists.user_id')
->where('users.type', '=', 'admin')->get();
dd($data);
DB::raw("CONCAT('name','id') AS ID"))

laravel join query is not working

I have the next query and I want to get the price_types.name but is not returned:
$projects = Project::with('projectsTask')
->select('projects.*',
'price_types.name as name_type'
)
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get();
Here an image query is retrievng
This on picture "type_list" must be string text
Maybe somebody can help me.
Many thanks!
Try this:
$projects = Project::with('projectsTask')
->where('client_id', $client->id)
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->orderBy('id')
->get([''projects.*',
'price_types.name as name_type'']);
get method receive as parameter an array with fields that you want.
$projects = Project::join('tasks', 'projects.id', '=', 'tasks.project_id')
->select('tasks.*',
'price_types.name as name_type',
'statuses.name as name_status'
)
->where([['client_id', $client->id], ['tasks.status_type', '!=', 2]])
->join('price_types', 'tasks.type_list', '=', 'price_types.id')
->join('statuses', 'tasks.status_type', '=', 'statuses.type')
->orderBy('tasks.id', 'DESC')
->get();

Laravel - Database query on default null date

I was trying to get some records of a table, which has a column 'UpdateDate' with default value NULL. I need to check if a record exists that either 'UpdateDate' is not today or 'UpdateDate' is NULL. The query I built is as follows:
DB::table('Users')
->where('id', '=', '10')
->where(function($query) {
$query->where('UpdateDate','<>',date("Y-m-d"))->orWhere('UpdateDate','is','NULL'); })
->exists()
It didn't work as expected. How should I modify it? Thanks.
Change you query to this
DB::table('Users')
->where('id', '=', '10')
->where(function($query) {
$query->where('UpdateDate','<>', date("Y-m-d"))
->orWhereNull('UpdateDate');
})
->exists();
If you have UpdateDate date as datatype.
If you have timestamp or datetime as datatype then change to this
DB::table('Users')
->where('id', '=', '10')
->where(function($query) {
$query->whereBetween('UpdateDate',[date("Y-m-d 00:00:00"), date("Y-m-d 23:59:59")])
->orWhereNull('UpdateDate');
})
->exists();

Resources