I want to apply search on the item members which is associated with the item.
I've members relation on my Item model like this
public function members()
{
return $this->hasManyDeep(
'App\Models\Users\TeamMember',
['App\Models\Users\Team'],
[['teamable_type', 'teamable_id'], 'team_id'],
['id', 'id']
);
}
I've done this code for apply search on items on the bases of item's members
$items = Item::query();
->where('title', 'Like', '%' . $keyword . '%')
->with('members')
->orWhereHas('members', function ($query) use($keyword) {
$query->from('users')->where('name', 'Like', '%' . $keyword . '%');
})->get();
this relation will be return an array of members. Now It's giving me error called -
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'team_members.team_id' in 'on clause' (SQL: select * from `items` and `title` Like %test% or exists
Related
I have assigned search method for my application and i have made relationship between two tables "users " table and "posts" table both of them have same field name "created_at" and when i want to fetch data that when this post has been created it will bring the date in users table created_at field not in posts table created_at field.
All i want is how to tell the system make difference between which table field name i mean .
This date comes from created_at field in users table
I wand to come date in this created_at field in posts table
ListingController.php
public function search(Request $request){
$posts = Post::with(['comments','category','creator'])
->join('users', 'posts.created_by', '=', 'users.id')
->where('slug', 'LIKE', '%'.$request->search. '%')
->orWhere('users.name', 'LIKE', '%'.$request->search. '%')
->where('status',1)->orderBy('posts.id','DESC')->paginate(5);
return view('front.listing',compact('posts'));
}
listing.blade.php
<div class="article_date">
by: {{ $post->creator->name }} , {{$post->created_at->diffForHumans()}}
</div>
You are calling your relationship improperly. You should not use a join there. Use constrained eager loading instead
There will be no conflict in the created_at date since there will be no join.
While still keeping your search query inside the closure of the relationship.
$posts = Post::with(['comments', 'category'])
->with(['creator' => function ($query) use ($request) {
$query->where('name', 'LIKE', '%' . $request->search . '%');
}])
->where('slug', 'LIKE', '%' . $request->search . '%')
->where('status', 1)
->orderBy('id', 'DESC')
->paginate(5);
I'm trying to implement search functionality within my blade for my Script model. It performs fine for everything related to searching directly within the Script collection/table. However, my users also will need to be able to enter in a Patient first_name or last_name and search for the script records within the Script table that belong to the Patient being searched for. These are connected with a hasMany/belongsTo relationship. I'm trying to reference data from the Patient table in the Script results.
I've tried (with help from SO already) to implement orWhereHas and reference the patients table, but something with my syntax must be off.
The error messages I'm getting from trying to reference 'patients' with orWhereHas returns:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'scripts.patients_id' in 'where clause' (SQL: select count(*) as aggregate from `scripts` where exists (select * from `patients` where `scripts`.`patients_id` = `patients`.`id` and (`first_name` like %% or `last_name` like %%)))
This probably would work if it referenced patient_id, which is the column name for the patients foreign key. I don't know if there is a way to do that?
When I change 'patients' to 'patient', I receive the error:
Call to undefined method App\Script::patient()
Models
Patient hasMany Script
Script belongsTo Patient (patient_id)
Script Blade
{{ Form::text('search', $search, ['class' => 'form-control form-control-sm', 'placeholder' => 'Search Scripts...']) }}
{{Form::submit('Search', ['class' => 'btn btn-primary btn-sm'])}}
ScriptController
$search = $request->search;
$patients = Patient::all();
$scripts = Script::
when($search, function ($query) use ($search) {
$query->where(function ($query) use ($search) {
$query
->where('prescribe_date', 'LIKE', '%' . $search . '%')
->orWhere('status', 'LIKE', '%' . $search . '%')
->orWhere('efax_reference', 'LIKE', '%' . $search . '%')
->orWhere('efax_confirmation', 'LIKE', '%' . $search . '%');
});
})
->orWhereHas('patients', function ($query) use ($search) {
$query->where('first_name', 'like', '%' . $search . '%')
->orWhere('last_name', 'like', '%' . $search . '%');
})
->paginate(25);
I expect that if someone searches for a patient name within the search input on the Scripts table/model, they are able to cross-reference the Patients table/model and display the Scripts records that are foreign_key'ed to those Patients.
EDIT:
Patient model
// One Patient has Many Scripts relationship
public function scripts() {
return $this->hasMany('App\Script');
}
Script model
// Many Scripts to One Patient relationship
public function patients() {
return $this->belongsTo('App\Patient');
}
I've 3 tables on my project:
Mall
Shop
Product
I've a page to search for products in the whole database. I need an option where they can search the product by the mall name. So I built my code like this:
$query = Product::with('shop', 'shop.mall');
if (!empty($data["keyword"])) {
$query = $query->where(function($q) use($data) {
$q->where('shop.mall.name', 'LIKE', '%' . $data["keyword"] . '%')->orWhere('shop.mall.keyword', 'LIKE', '%' . $data["keyword"] . '%');
});
}
But it is showing this error:
Column not found: 1054 Unknown column 'shop.mall.name' in 'where clause'
Is there any problem with my query? Thank you
To search within relation use whereHas(), it creates subquery and returns data filtered in shop.mall.
$query = Product::with('shop', 'shop.mall');
if (!empty($data["keyword"])) {
$query = $query->whereHas('shop.mall',function($q) uses ($data){
$q->where('name', 'LIKE', '%' . $data["keyword"] . '%')->
orWhere('keyword', 'LIKE', '%' . $data["keyword"] . '%');
});
}
I have a table with input fields that act as filters. There are values from Orders table and Users table which has a relationship.
e.g.:
Order Id | User Full Name | Order Price | Date |
[order filter] [user name filter] [order date filter]
How can I filter a value from a users table via relationship?
This returns error of course:
public function name($name)
{
return $this->builder->where('name', 'LIKE', '%$name%')->orWhere('surname', 'LIKE', '%$name%');
}
Error:
QueryException in Connection.php line 770:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name' in 'where clause' (SQL: select count(*) as aggregate from `orders` where (`name` LIKE %$name% or `surname` LIKE %$name%))
You can add the condition directly to your query builder or create a scope.
Add this to your Order model.
public function scopeFilterUser($query, $name)
{
return $query->whereHas('user', function ($q) use ($name) {
$q->where('name', 'like', "%{$name}%")
->orWhere('surname', 'like', "%{$name}%");
});
}
Use it like so
$orders = Order::filterUser($name)->get();
Apply the condition directly.
$orders = Order::whereHas('user', function ($query) use ($name) {
$query->where('name', 'like', "%{$name}%")
->orWhere('surname', 'like', "%{$name}%");
})->get();
Edit : Based on your filter methods.
public function name($name)
{
return $this->builder->whereHas('user', function ($q) use ($name) {
$q->where('name', 'like', "%{$name}%")
->orWhere('surname', 'like', "%{$name}%");
});
}
I have 2 tables, projects and jobs. jobs has a column called project_id. In Laravel 5.2 I want to run a search that will return all jobs which belong to a project of a given search term. This SQL works:
SELECT jobs.*, projects.name FROM jobs INNER JOIN projects ON jobs.project_id = projects.id WHERE projects.name LIKE "%$keyword%"
In my Job.php model I have created a scope method, which errors:
public function scopeSearch($query, $keyword)
{
if ($keyword != '') {
$query->where(function($query) use ($keyword) {
$query->where('projects.name', 'LIKE', '%' . $keyword . '%')->join('projects', 'jobs.project_id', '=', 'projects.id');
});
}
return $query;
}
This produces the error:
Column not found: 1054 Unknown column 'projects.name' in 'where
clause' (SQL: select * from jobs where (projects.name LIKE
%test%))
In my JobsController.php I have:
$searchResults = Job::Search($searchTerm)->get();
The parameter $query in where(function($query) is not the $query that you passed in public function scopeSearch($query, $keyword)
You can either remove it with just the query like below (as #Rob mentioned)
public function scopeSearch($query, $keyword)
{
if ($keyword != '') {
$query->where('projects.name', 'LIKE', '%' . $keyword . '%')->join('projects', 'jobs.project_id', '=', 'projects.id');
}
return $query;
}
or you need to include the $query in use()
$query->where(function() use ($keyword, $query)