Get data from two different database columns - laravel

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

Related

get Field name from the query builder result

I need to get field names from the query builder result.
For a single table
I could use
DB::getSchemaBuilder()->getColumnListing('table_name');
but what i need is from the query builder result.
DB::table('users as a')
->leftJoin('userwork AS uk','uk.WORK_ID', '=','a.WORK_ID')
->selectRaw("a. name ,uk.work_name as work ,concat('+',uk.work_phone) as phone")
->get();
I want to extract the attribute name to get the result like below
['name','work','phone'];
I end up using array_keys method after converting the first result of the query builder to array.
$db_fields=array_keys((array)$items->first());
Do you need like this?
DB::table('users as a')
->leftJoin('userwork AS uk','uk.WORK_ID', '=','a.WORK_ID')
->select("a. name ,uk.work_name as work ,concat('+',uk.work_phone) as phone")
->get();
just using select. You could write on your controller.

Laravel query builder: reverse of "LIKE"?

I need the reverse of a LIKE comparison. For example, if my database contains a person with firstName "Daniel" and I search for first name "dan" then I want to get a hit. The query works when I use
->where('firstName', 'like', '%'.$searchTerm.'%')
What would I use if my database contains "Dan" and I want to get a hit when I search for "Daniel", but not when I search for "Steve"? Basically, I want the effect of putting the wildcard % around the field instead of around the search term.
I am using the query builder in Laravel 5.6
[edited for clarity]
You can swap column and search term:
->whereRaw("? LIKE CONCAT('%', `firstName`, '%')", [$searchTerm]);

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

How to return columns and joined columns in a certain order (Eloquent)

I have the following code:
Incident::where('id','<','35')->with('priority')->paginate(15);
Right now this returns the following:
ID, title, description, priority_id and the priority object.
I would like to retrieve only ID, title and the priority object, without description nor priority_id but I want them in a specific order, like this:
ID, priority and title.
But when I do the following:
Incident::where('id','<','5')->with('priority')->paginate(15, array('id', 'priority', 'title'));
I get an error saying column incidents.priority not found.
Is there any way to select only the columns I want and in the order I want them when one of them is referenced through a FK?
Thank you!
You don't need to include priority in the list:
Incident::where('id','<','5')->with('priority')->paginate(15, array('id', 'title'));
If you pass a callback to the with method you can specify the order like so:
Incident::where('id','<','35')
->with(['priority' => function ($query) {
$query->select('id', 'priority', 'title');
}])
->paginate(15);
Your query is not a join. The with('priority') is actually a 2nd separate query that is executed after the Incident query and then attached to the Incident models. If you want to reference columns and use a join you would do it like:
Incident::select('id', 'priorities.*', 'incidents.title')
->leftJoin('priorities', 'priorities.id', '=', 'incidents.priority_id')
->where('incidents.id', '>', '5')
->paginate(15);
If you don't want to use the above then #tam answer would be best but make sure with his solution to include the id in the sub query callback because that is how the relation attaches itself to the parent model after the query is ran.

Laravel Order by in one to many relation with second table column

Hi i have tables with one to many relation
sectors
id
name
position
seat_plans
id
name
sector_id
I just want to select all seat plans order by sectors.position. I tried
$seat_plans = SeatPlan::with(['sector' => function($q){
$q->orderBy('position');
}
])->get();
but it is not working. when i check The SQL it is generating query like
select * from seat_plans
can anybody please tell me how to do this?
I don't think you need a custom function for your use case. Instead try this:
$users = DB::table('seat_plans')
->join('sectors', 'seat_plans.sector_id, '=', 'sectors.id')
->select('seat_plans.*')
->orderBy('sectors.position')
->get();

Resources