How to convert column values to lower case and get data? - laravel

I have user nickname in my table in case insensitive type.
I tried to get a record from the table using this query but did not succeed:
$user = User::whereIn('LCASE(nickname)', strtolower($nickname));
Error message:
Invalid argument supplied for foreach()
How I can get users by nickname in case insensitive type?

WhereIn expects an array of values, in this case I'd suggest t change it to a base where (and add a first so you get the first value or use a get to return all users that matches).
$user = User::where('nickname', 'like', '%' . strtolower($nickname) . '%')->first();

Related

Get data from two different database columns

i have tow column in my sql and use this query to search
$Person->where('name','LIKE',"%{$keyword}%")
->orWhere('family_Name','LIKE',"%{$keyword}%")
->orWhere('id',$keyword);
but if user type name and family name together then result is null
how can i get where from sum of tow column? or any other way
If I understood correctly, a raw sql command like the one below might work for you.
$person->where(DB::raw('CONCAT_WS(" ", name, family_Name)'), 'like', '%'.$keyword.'%')->get();
I assume you want search "John Doe". You put "John" at column name and "Doe" at family_name.
So, you can add CONCAT_WS() for this.
$Person
->where('name','LIKE',"%{$keyword}%")
->orWhere('family_Name','LIKE',"%{$keyword}%")
->where(DB::raw('CONCAT_WS(" ", name, family_name)'), 'like', "%{$keyword}%") // <-- here
->orWhere('id',$keyword);

Laravel - WhereExists returning "Invalid parameter number: parameter was not defined"

I'm trying to use whereExists() on an existing Eloquent query builder (called $trips):
$trips = $trips->whereExists(function ($query) use ($filterValue) {
$query->from(DB::raw("jsonb_array_elements(passengers->'adults'->'persons') as p(person)"))
->whereRaw("p.person->>'name' LIKE '?%'", $filterValue);
});
The query I'm trying to create in raw postgres format is the following (this query works fine using pgAdmin):
SELECT *
from trips
WHERE exists (select *
from jsonb_array_elements(passengers -> 'adults' -> 'persons') as p(person)
where p.person ->> 'name' LIKE 'Prof%');
And I'm receiving this error:
Invalid parameter number: parameter was not defined
I think the problem is small, but I can't see it myself.
The parameter definition in your whereRaw() statement is not quite correct. Parameterized queries are not just string replacements. Your query as written doesn't have a parameter in it, it has a string literal of '?%'. You need to change this to a query parameter, and append the % wildcard to the string you pass in.
Try this:
->whereRaw("p.person->>'name' LIKE ?", $filterValue.'%')

Invalid parameter number on Laravel subquery

When I run the below query I get the following error: Invalid parameter number: mixed named and positional parameters.
$subQuery = DB::table('earliest_count')
->select('reporting_week')
->where('vendor_name', $vendorName);
$dates = DB::table('invoice')
->select('week_beginning_date', 'week_end_date')
->whereRaw(':sql BETWEEN `week_beginning_date` AND `week_end_date`', [':sql' => DB::raw("({$subQuery->toSql()})")])
->where('week_beginning_date', '<', $date)
->orderBy('week_beginning_date')
->limit(1)
->mergeBindings($subQuery)
->get();
If I replace the whereRaw with the following it works:
->whereRaw('(SELECT reporting_week FROM earliest_count WHERE vendor_name = "My Vendor") BETWEEN `week_beginning_date` AND `week_end_date`')
How can I get the subquery to work without having to write the exact query as a string?
Edit
I did try the following, and I get no errors but I don't get any results. (When I enter the subquery as a string I do get a result):
->whereRaw('? BETWEEN `week_beginning_date` AND `week_end_date`', [DB::raw("({$subQuery->toSql()})")])
Laravel doesn't use named placeholders, you should use ? for placeholders and remove the name from the parameters array.
Like this:
->whereRaw('? BETWEEN `week_beginning_date` AND `week_end_date`', [DB::raw("({$subQuery->toSql()})"])

Search query from joined table in Laravel 5.3

I have a books table that contains many subject on my subjects table (one-to-many relationship).
I tried to join my tables like this:
$book = Book::latest()
->leftjoin('subjects', 'books.id', '=', 'subjects.book_id')
->select('books.*', 'subjects.subject')
->where('subject', 'like', '%' .$search. '%')
->paginate(20);
I want a search query that will display the books having subjects matched form the $search variable. However, it keeps displaying a book redundantly depending on how many subjects of a book that matched on the $search variable since a book has many subjects.
I only want to display a book once, regardless of how many subjects the book matched.
This image below was the output of the search query I made, the value of the $search= ""
On the second image notice that I search "a" on the search box:
The book entitled "Special Education assessment: Issues strategies affecting today's classrooms" (see it on the first image; it was being redundant 6 times since the subjects of that book was 6)
To display a book only once you have to group by book id (or any other unique column)
->groupBy('books.id');
Mind you, as mentioned in the MySQL doc here
SQL92 and earlier does not permit queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are not named in the GROUP BY clause.
Hence the error message 'bisu_ccc_library.books.ISBN' isn't in GROUP BY
To bypass this, turn off strict in Laravel and everything will work nicely.
Go to config/database.php and in the mysql configuration array, change strict => true to strict => false
I think you want to use distinct for your select
$book = Book::latest()
->leftjoin('subjects', 'books.id', '=', 'subjects.book_id')
->select('books.*', 'subjects.subject')
->distinct()
->where('subject', 'like', '%' .$search. '%')
->paginate(20);
Just like in regular SQL (which it will translate to) it will "Force the query to only return distinct results." (from laravel api docs)
The SELECT DISTINCT statement is used to return only distinct
(different) values.
Inside a table, a column often contains many duplicate values; and
sometimes you only want to list the different (distinct) values.
The SELECT DISTINCT statement is used to return only distinct
(different) values. - W3Schools

Eloquent select where($value) issues

I am trying make select in eloquent:
$query = $query->where($value);
echo $value is:
´column´, ´<´, ´3´
BUT i have error: Unknown column ''column', '=', '3'' in... (look at quotation mark). If i write directly:
$query->where( ´column´, ´<´, ´3´);
Everything is okay
where() wants at least two arguments, but for the way you're using it it will need three. where('column', '<', $value) where $value is 3.
References:
http://laravel.com/docs/queries#advanced-wheres
http://laravel.com/api/source-class-Illuminate.Database.Query.Builder.html#268-324
$user = DB::table('users')->where('name', 'John')->first();
$users = DB::table('users')->where('votes', '>', 100)->get();
Where() required at least two parameters but it will accept three parameters.
Two parameters are compulsory:
table column name
compare value
in your example, you have only passed one parameter and will be consider as a table column name. obviously it will not match with the column name.
However, if you provide 3 parameters, ensure that you place the value parameter as 3rd parameter.
More:
http://laravel.com/docs/queries#selects

Resources