SQL Laravel Eloquent - Where field can be uppercase or lowercase - mysql

$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);
}
}

Related

Laravel Eloquent WHEREIN with string

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))

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();

orWhere with multiple conditions in Eloquent

I am looking to write the following query in eloquent:
select * from stocks where (symbol like '%$str%' AND symbol != '$str' ) OR name like '$str%'
Without the last condition, it's simple:
$stocks = Stock::live()
->where('symbol','like','%'.$str.'%')
->where('symbol','!=',$str)
->get();
But adding orWhere('name','like',$str.'%') after the two wheres returns incorrect results. Basically I am wondering how to emulate what I accomplished by using the (condition1 AND condition2) OR condition3 syntax in the raw query above.
Try
$stocks = Stock::live()
->where('name', 'like' , '%'.$str.'%')
->orWhere(function($query) use($str) {
$query->where('symbol','like','%'.$str.'%')
->where('symbol','!=',$str); // corrected syntax
})->get();
Try
$stocks = Stock::live()->where('name', 'like' , '%'.$str.'%')
->orWhere(function($query) use($str) {
$query->where('symbol','like','%'.$str.'%')
->where('symbol','!=',$str)
})->get();
I didn't test this, so sorry if it doesn't work. But I think one of these solutions could work.
$stocks = Stock::live()
->where([
['symbol','like','%'.$str.'%'],
['symbol', '!=', $str],
])
->orWhere('name','like', $str.'%')
->get();
and
->where(function ($query) use ($str) {
$query->where([
['symbol','like','%'.$str.'%'],
['symbol', '!=', $str],
]);
})
->orWhere(function ($query) use ($str) {
$query->where('name','like', $str.'%');
});

How to use OR inside WHERE in laravel Query

I'm new to laravel and I need help for query in laravel
My Custom Query
$sql1="SELECT * FROM blogs
WHERE
('$title'='' OR title='$title')
AND
('$body'='' OR body='$body')";
and i create a laravel build query but don't know how to put OR inside WHERE and Put Brackets
$posts = Blog::where('title','LIKE',"%{$title}%")
->Where('body', 'LIKE',"%{$body}%")
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
I think orWhere is what you're looking for and based off custom query I think you would need = instead of like unless thats what youre going for, I think it would be something like:
->where('title', '=', $title)
->orWhere('title', '=', '');
$posts = Blog::where(function($q) use($title){
$q->where('title','')->orWhere('title',$title);
})->where(function($q) use($body){
$q->where('body','')->orWhere('body',$body);
})->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
Just use orWhere()
$posts = Blog::where('title', $title)
->orWhere('title', $otherTitle)
->orderBy($order, $dir)
->get();
Your query should be like :
$result = Blog::where(function($query) use ($title, $body){
$query->where(
["title" => $title, "body"=>$body]
)->orWhere(
["title"=>"", "body"=>""]
);
})
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
Hope this give you an idea :)
$posts = Blog::where(function ($query) use($title) {
$query->where('title', 'LIKE', "%{$title}%")
->orWhere('title', '=', '$title');
})
->orWhere(function ($query) use($body) {
$query->where('body', 'LIKE', "%{$body}%")
->orWhere('body', '=', '$body');
})
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
Not tested...
Use the ->orWhere() code.
Note: You can also make use of magic methods
->whereAgeOrPhone(18, 123456789);
use orWhere()
$blogs= DB::table('Blog')
->where('title', 'LIKE', '%{$title}%')
->orWhere('body','LIKE', '%{$body}%')
->offset($start)
->limit($limit)
->orderBy($order,$dir)
->get();
For More Query, You can visit here

Laravel SUBSTRING on Eloquent query

Question
How do I put a limit on one of the rows in an Eloquent result?
Scenario
I need to retrieve only around 100 characters from one of the fields in my result. Currently I'm using a join statement so multiple results are being returned. I basically need only the first 100 characters from post.content
Code
public function getAll()
{
return Post::select('posts.id', 'posts.title', 'posts.content', 'posts.views', 'posts.comments', 'posts.tags', 'posts.date_created', 'users.image_url', 'users.username', 'users.toxick')
->join('users', 'posts.user_id', '=', 'users.id')
->get();
}
I'm not sure how to go about putting a filter on the query to only return 100 characters. I've looked around briefly but I've not found anything useful, not to my specific scenario at least.
Cant test this at the moment (sorry) but what about:
public function getAll(){
$query = Post::select('posts.id', 'posts.title', 'posts.content','posts.views', 'posts.comments', 'posts.tags', 'posts.date_created', 'users.image_url', 'users.username', 'users.toxick')
->join('users', 'posts.user_id', '=', 'users.id')
->get();
foreach($query as $entries){
$entries->content = substr($entries->content, 1, 100);
}
return $query;
}