I have custom query where ignored current row id:
$relatedVacancies = \App\Vacancy::whereHas('skills', function ($q) use ($vacancy) {
return $q->whereIn('skill_id', $vacancy->skills->pluck('skill_id'));
})->where('id', '!=', $vacancy->id)
->orWhere('employment', $vacancy->employment)->where('id', '!=', $vacancy->id)
->orWhere('title', 'like', '%'.$vacancy->title.'%')->where('id', '!=', $vacancy->id)
->get();
How I can use this condition one time only:
->where('id', '!=', $vacancy->id)
If you are trying to select based on three conditions:
->where([
('id', '!=', $vacancy->id),
('employment', '=', $vacancy->employment),
('title', 'like', '%'.$vacancy->title.'%'),
])->get();
Try this (based on https://laravel.com/docs/5.7/queries#parameter-grouping):
$relatedVacancies = \App\Vacancy::where('id', '!=', $vacancy->id)
->whereHas('skills', function ($q) use ($vacancy) {
return $q->whereIn('skill_id', $vacancy->skills->pluck('skill_id'));
})
->where(function ($query) {
$query->where('employment', $vacancy->employment)
->orWhere('title', 'like', '%'.$vacancy->title.'%');
})
->get();
Also check for this answer: https://stackoverflow.com/a/23009807/5458355
Related
This is my original code where i display all data for team users.
$data = Teamuser::join('teams', 'teams.id', '=', 'team_user.team_id')
->join('users', 'users.id', '=', 'team_user.user_id')
->get(['users.name as username','teams.name','users.email','team_user.role','team_user.id','team_user.user_id','team_user.team_id']);
However, since im developing a search function for the table. I try adding orwhere to the function.
$data = Teamuser::join('teams', 'teams.id', '=', 'team_user.team_id')
->join('users', 'users.id', '=', 'team_user.user_id')
->get(['users.name as username','teams.name','users.email','team_user.role','team_user.id','team_user.user_id','team_user.team_id'])
->where('users.name', 'like', '%'.$request->search2.'%')
->orWhere('teams.name', 'like', '%'.$request->search2.'%')
->orWhere('team_user.role', 'like', '%'.$request->search2.'%')
->orWhere('users.email', 'like', '%'.$request->search2.'%')->paginate(5);
But the search function doesnt work, how do i format the syntax in a correct way?
I'm not sure you are using get() correctly, but i purpose you an alternative syntax
$data = Teamuser::select(['users.name as username','teams.name','users.email','team_user.role','team_user.id','team_user.user_id','team_user.team_id'])
->join('teams', 'teams.id', '=', 'team_user.team_id')
->join('users', 'users.id', '=', 'team_user.user_id')
->where('users.name', 'like', '%'.$request->search2.'%')
->orWhere('teams.name', 'like', '%'.$request->search2.'%')
->orWhere('team_user.role', 'like', '%'.$request->search2.'%')
->orWhere('users.email', 'like', '%'.$request->search2.'%')
->paginate(5);
Check if your join is correct, maybe you could use leftJoin() instead of join()
class TeamUserController extends Controller
{
public function index()
{
$teamUsers = Teamuser::query()
->with('team', 'user')
->search(request('search2'))
->paginate(5);
...
// App/Models/TeamUser
public function team()
{
return $this->belongsTo(Team::class);
}
public function user()
{
return $this->belongsTo(User::class);
}
public function scopeSearch($query, string $terms = null)
{
collect(explode(' ', $terms))->filter()->each(function ($term) use ($query) {
$term = '%'.$term.'%';
$query->where(function ($query) use ($term) {
$query->where('role', 'like', $term)
->orWhereHas('team', function ($query) use ($term) {
$query->where('name', 'like', $term);
})
->orWhereHas('user', function ($query) use ($term) {
$query->where('name', 'like', $term)
->where('email', 'like', $term);
});
});
});
}
I have this search query
$products = Product::query()
->where('name', 'LIKE', "%{$search}%")
->orWhere('slug', 'LIKE', "%{$search_any}%")
->where('status', 'Active')
->get();
i have user table which stores the user who listed the product and its relation is as follows
Product Model:
public function user()
{
return $this->belongsTo(User::class);
}
I have one field type in user table, i want if user enters search string matches type also then also all the products added by them should be returned i was trying this code but how can i combine both the queries
$products = Product::query()
->where('name', 'LIKE', "%{$search}%")
->orWhere('slug', 'LIKE', "%{$search_any}%")
->where('status', 'Active')
->get();
$productsAddedByUser = User::where('type', 'LIKE', "%{$search_any}%")->get();
// saved $productsAddedByUser result in this array $userIdarray
$productnew = Product::whereIn('user_id', $userIdarray)->get();
I want to combine both the results of $productnew and $products
At first save result in $userIdarray, then in products query:
$userIdarray = User::where('type', 'LIKE', "%{$search_any}%")->pluck('id');
$products = Product::query()
->where(
fn($query) => $query->where('name', 'LIKE', "%{$search}%")
->orWhere('slug', 'LIKE', "%{$search_any}%")
)
->where('status', 'Active')
->when(count($userIdarray), fn($query) => $query->whereIn('user_id', $userIdarray))
->get();
I have a WorkPackage model with below relationship.
public function work_items()
{
return $this->hasMany(WorkItem::class);
}
And WorkPackage has 'title','project_id' columns and work_items has 'title','work_package_id' columns. Now I want to search user typed keyword is matching with title in both table.
Here is my tried function
public function getWorkPackageProposals($projectId, $title)
{
$result['data'] = $this->workPackage->with(['work_items' => function ($query) {
$query->where('title', 'LIKE', "%{$title}%");
}])
->where('project_id', $projectId)
->where('title', 'LIKE', "%{$title}%")
->get();
return $result;
}
but its not working.
Please find the below code for I used to create object of WorkPackage.
public function __construct(WorkPackage $workPackage)
{
$this->workPackage = $workPackage;
$this->setModel(WorkPackage::class);
$this->workItemRepository = app(WorkItemRepository::class);
}
whereHas() works to specify additional filters for the related model to check :
$result['data'] = $this->workPackage->whereHas('work_items', function ($query) use ($title) {
$query->where('title', 'LIKE', "%{$title}%");
})
->where('project_id', $projectId)
->where('title', 'LIKE', "%{$title}%")
->get();
return $result;
Below code works for me. Thanks to everyone for help to get right answer.
$result['data'] = $this->workPackage->with(['work_items' => function ($q) use ($title) {
$q->where('title', 'LIKE', "%{$title}%");
}])
->where('project_id', $projectId)
->where('title', 'LIKE', "%{$title}%")
->get();
return $result;
I think your problem will be solved with join method:
$result = $this->workPackage
->join('work_items', 'work_packages.id', '=', 'work_items.package_id')
->where('work_items.title', 'LIKE', '%{$term}%')
->orWhere('work_packages.title', 'LIKE', '%{$term}%')
->get();
I'm trying to do a simple search function. For that, I implemented the following query:
Profile::whereHas('accounts', function ($query) use ($id) {
$query->where('user_id', $id);
})
->where('username', 'LIKE', "%{$search}%")
->paginate(20);
The whereHas selects the profiles that the user with $id has.
Then, with the where we figure out if the search match with some username in the database.
Finally, we paginate the results.
All right so far.
The problem comes when I add a orWhere like that:
Profile::whereHas('accounts', function ($query) use ($id) {
$query->where('user_id', $id);
})
->where('username', 'LIKE', "%{$search}%")
->orWhere('fullname', 'LIKE', "%{$search}%")
->paginate(20);
I want that the query analyses if the search matchs with the username and/or the fullname. When I add the orWhere works, but the whereHas doesn't do its work and the results are from all the users.
You need to do it like this:
Profile::whereHas('accounts', function ($query) use ($id) {
$query->where('user_id', $id);
})
->where(function($query) use ($search) {
$query->where('username', 'LIKE', "%{$search}%")
->orWhere('fullname', 'LIKE', "%{$search}%");
})
->paginate(20);
I do request using query:
return Baber::where(function ($query) use ($request) {
// HERE CONDITION WHERE FOR USER TABLE
})->with("user")->orderBy('id', 'desc')->get();
How can I use where() inside query, that it will be comparabled with user table in with("user")?
I mean the following:
return Baber::where(function ($query) use ($request) {
$query->where('user.created_at', $request->date);
})->with("user")->orderBy('id', 'desc')->get();
It's known as constraining eager loads.
Baber::with(['user' => function ($query) use ($request) {
$query->where('user.created_at', '=', $request->date);
}])
->orderBy('id', 'desc')
->get();