Laravel Eloquent WHEREIN with string - mysql

I have a string inside
$loc_name1='Kochi','Hydarabad','Buwaneswar';
and i have a laravel query
$employees = DB::table('audit_employee_basics')
->select('audit_employee_basics.id as empid','emp_name','emp_code','designation_name','emp_company_email_id','emp_contact_number','emp_gender','emp_location'
,'department_name','emp_joining_date','fk_emp_previous_exp','image')
->join('audit_department', 'audit_employee_basics.emp_fk_dep', '=', 'audit_department.id')
->join('audit_employee_skillset', 'audit_employee_skillset.fk_emp_id', '=', 'audit_employee_basics.id')
->join('audit_designation', 'audit_designation.id', '=', 'audit_employee_basics.emp_fk_des_id')
->whereIn('audit_employee_basics.emp_location', [$loc_name1])
-> distinct()
->get();
which is not working for me.and if i change ->whereIn('audit_employee_basics.emp_location', ['Kochi','Hydarabad','Buwaneswar']) is working for me.Any help would be highly appreciated

whereIn expects the second argument to be an array. You can change your script as :
->whereIn('audit_employee_basics.emp_location', explode(',', $loc_name1))

Related

Laravel 8: Call to database function

I'm using a Laravel 8 for website developement, and on the next part of code, I execute a query:
$assig = DB::table('table')
->where('table.column', '=', "oneValue")
->select('anfunctionOnDatabase(2,table.columDesc)')
->get();
but it no works.
How I need call to database function on laravel?
Thanks
Use selectRaw()
like this;
$assig = DB::table('table')
->where('table.column', '=', "oneValue")
->selectRaw('anfunctionOnDatabase(2,table.columDesc)')
->get();
Use : DB::raw :
$assig = DB::table('table')
->where('table.column', '=', "oneValue")
->select(DB::raw("anfunctionOnDatabase(2,table.columDesc)"))
->get();

SQL Laravel Eloquent - Where field can be uppercase or lowercase

$pname can be "Airbus", "airbus" or "AIRBUS"
$info = Plane::where('plane_name', '=', $pname)
->where('uid', '=', $uid)
->get();
Is there any way I can update this query to check the database for the plane name without case sensitivity?
Use LOWER MYsql function and strtolower php function
$info = Plane::whereRaw('LOWER(plane_name) = (?)', [strtolower($pname)])
->where('uid', '=', $uid)
->get();
OR
Plane::where('plane_name', 'ilike', $pname)->where('uid', '=', $uid)
->get();
just make sure result where clause to lowercase and query clause to lowercase too.
where(DB::raw('lower(column_name)'), '=', Str::lower($query))
dont forget to use:
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
Add -> use Illuminate\Support\Facades\DB; in Controller file.
-----------Example---------------
UserInfo::where(DB::raw('upper(username)'), strtoupper($Request->username))
It's work for me, hope to help you.
To solve my issue, I have to do that on my Model.
simple copy and paste this function
public function __get($key)
{
if (is_null($this->getAttribute($key))) {
return $this->getAttribute(strtoupper($key));
} else {
return $this->getAttribute($key);
}
}

calculating with values from two different mysql tables

I'm building a sort of human resources application where I have to reduce a variable of a mysql table with a variable of another mysql table.
The CMV I'm using is Laravel so I'm trying to use the DB class as much as possible.
This is the first table, users:
users_table
And the second table, aanvraags:
aanvraags_table
I would like reduce the variable vakantie from the first table with the variable duur from the second table. This would happen right after the vacation request is accepted. There is already a function in my controller to change the variable goedgekeurd from 0 to 1. The function looks like this:
public function updateNieuw($id){
//update aanvraag
$update_aanvraag = DB::table('aanvraags')
->where('id', $id)
->update([
'goedgekeurd' => 1 //approved by the boss
]);
Within that same function I would like to reduce the variable vakantie with the variable duur.
At the moment I already tried a few things like:
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->get();
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->update([
'users.vakantie' => $update_vakantiedag
]);
All of this doesn't get me anywhere. Most of the time I get errors or the value from the variable vakantie from the first table.
It is quite important for the application that the reduction takes place. The variable vakantie displays the remaining vacation days per user after accepting a new request and is displayed in the app.
Any tips or solutions are welcome !
Thank you all in advance !
Use this:
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->first();
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->decrement('users.vakantie', $update_vakantiedag->duur);
#JonasStaudenmeir Thanks a lot ! I changed the function and it works !
$update_vakantiedag = DB::table('aanvraags')
->select('duur')
->where('id', $id)
->first();
$verlof = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->first();`
$vakantie = DB::table('users')
->join('aanvraags', 'users.id', '=', 'aanvraags.user_id')
->where('aanvraags.id', '=', $id)
->update([
'users.vakantie' => $verlof->vakantie - $update_vakantiedag->duur
]);
Thank you so much !

Want to get result between time range in Laravel

I have some records, i want to get records from them where item is not between given time range, here is my code that i already tried but no success
$other_hour_trade = DB::table('finaltrade')
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->select('finaltrade.*')
->where('finaltrade.user_id', $user_id)
->whereNotBetween('finaltrade.created_at', [DB::raw('exchanges.start_time'), DB::raw("ADDTIME(exchanges.start_time, '01:00:00')")])
->whereNotBetween('finaltrade.created_at', [DB::raw('exchanges.close_time'), DB::raw("SUBTIME(exchanges.close_time, '01:00:00')")])
->get();
'finaltrade.created_at' is a datetime field and
'exchanges.start_time' and 'exchanges.close_time' is a only time field
I have join two tables to get results
I don't know how to make this work directly using whereNotBetween, but since you're already trying to resort to some raw SQL in your current code, why not just use whereRaw instead:
$other_hour_trade = DB::table('finaltrade')
->join('exchanges', 'finaltrade.exchange_id', '=', 'exchanges.id')
->select('finaltrade.*')
->where('finaltrade.user_id', $user_id)
->whereRaw("finaltrade.created_at NOT BETWEEN exchanges.start_time AND ADDTIME(exchanges.start_time, '01:00:00')")
->whereRaw("finaltrade.created_at NOT BETWEEN exchanges.close_time AND SUBTIME(exchanges.close_time, '01:00:00')")
->get();

Translate Laravel function to SQL request to test

I would like to test the output of Laravel function in MySQL, for this I need to translate the code to SQL request.
Function :
return DB::table('products')
->leftJoin('product_locations', 'products.id', '=', 'product_locations.productId')
->select(DB::raw('IF(product_locations.id is not null,concat(product_locations.id,"-","M"),concat(products.id,"-","N")) as id'), DB::raw('IF(product_locations.id is not null,1,0) as multilocation'), 'products.productName', 'products.productName as text', 'product_locations.productLocationName', 'product_locations.binLocationName', 'product_locations.unitCost', 'product_locations.amount')
->orderBy('products.productName')
->distinct()
->get();
Use query logging:
DB::connection()->enableQueryLog();
DB::table('products')
->leftJoin('product_locations', 'products.id', '=', 'product_locations.productId')
->select(DB::raw('IF(product_locations.id is not null,concat(product_locations.id,"-","M"),concat(products.id,"-","N")) as id'), DB::raw('IF(product_locations.id is not null,1,0) as multilocation'), 'products.productName', 'products.productName as text', 'product_locations.productLocationName', 'product_locations.binLocationName', 'product_locations.unitCost', 'product_locations.amount')
->orderBy('products.productName')
->distinct()
->get();
dd(DB::getQueryLog())
It is not the way, but still you can get the same result with less effort.
Make a small mistake in you query, for example use remove last character of a table name or a column name. Then it raise an error that shows the query. Then you can easily get your query on the screen.
In your query, use product instead of products. This will cause an error in query and display it.