How to get links from a collection of users - php

I have a collection of users
$department = Department::find(1)->users;
I need to get Journal relations to them, the code below is working, but here the conditions are used directly from the Users table, and I specifically need from the $department collection
User::where('name', '<>', 'Manager')
->with(['journal' => function($query) use ($request){
$query->where('schedule', 'like', "%" . $request->date . "%");
}])->get();

Try this :
$department = Department::with('users.journal')
->whereHas('users', function ($query) use ($request) {
$query->where('name', '<>', 'Manager')->whereHas('journal', function ($q) use ($request) {
$q->where('schedule', 'like', "%" . $request->date . "%");
});
})->find(1);

Related

Using whereHas and with laravel

Can this code be written using WhereHas and with only and not using Join? am filtering few data from multiple tables
public function filterByOffer(Request $request, User $user, Offer $offer)
{
$query = TradeRequest::query()
->select(
'trade_requests.uuid as trade_request_uuid',
'trade_requests.card_images_url',
'trade_requests.status',
'trade_requests.card_value',
'trade_requests.amount_payable',
'offers.*',
'cards.name',
'card_currencies.currency'
)
->join('offers', 'trade_requests.offer_id', 'offers.id')->where('offers.uuid', $offer->uuid)
->join('card_currencies', 'offers.card_currency_id', 'card_currencies.id')
->join('cards', 'offers.card_id', 'cards.id')->where('offers.user_id', $user->id)
->orderBy('trade_requests.created_at', 'DESC');
$request->whenHas('card', function ($value) use ($query) {
return $query->where('cards.name', 'like', '%' . $value . '%');
});
$request->whenHas('currency', function ($value) use ($query) {
return $query->where('card_currencies.currency', 'like', '%' . $value . '%');
});
$request->whenHas('status', function ($value) use ($query) {
return $query->where('trade_requests.status', 'like', '%' . $value . '%');
});
return $query->paginate();
}
Can this code be written using WhereHas and with only and not using Join?

Laravel Search Query which meets conditions

Hi I have this code that is to search for records.
Admins are able to see all records, this is the query for admins
$trainings = Training::where('staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%")
->paginate();
Whereas Employees should only see their records, so how do I add this condition into the existing query for the search function? This is the query I have for the Employee search currently
$employees = Training::where('staffName',$username)
->orWhere('staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%")
->paginate();
I have another version for Managers, which is meant to also refer to their roles from a different table. I am trying it like this but it only is working properly when it comes to the Manager, but when the function fetches anything related to staff under them it fetches all records instead of specific records.
$managers = DB::table('trainings')
->join('users','users.id','=','trainings.staff_id')
->select('trainings.staffName','programTitle','trainer','trainingDate','hours')
->where('trainings.staffName',$username)
->orWhere('reportingTo',$username)
->where(function ($query) use ($request) {
$query->where('trainings.staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%");
})
->paginate();
Your usage of orWhere is wrong because orWhere remove all conditions if it's true try this code below
Training::where('staffName', $username)
->where(function ($query) {
$query->where('staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%");
})
->paginate();
For the last query
$managers = DB::table('trainings')
->join('users', 'users.id', '=', 'trainings.staff_id')
->select('trainings.staffName', 'programTitle', 'trainer', 'trainingDate', 'hours')
->where(function ($query) use ($username) {
$query->where('trainings.staffName', $username)
->orWhere('reportingTo', $username);
})
->where(function ($query) use ($request) {
$query->where('trainings.staffName', 'LIKE', "%{$request->search}%")
->orWhere('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%");
})
->paginate();
here's when query scopes comes to the rescue.
class Training extends Eloquent {
public function scopeFilterByRole($query, $roleId, $searchInput)
{
if ($roleId === 3) {
return $query->where('staffName', 'LIKE', "%{$searchInput}%")
}
return $query;
}
}
Then in your controller
Training::where('programTitle', 'LIKE', "%{$request->search}%")
->orWhere('trainer', 'LIKE', "%{$request->search}%")
->FilterByRole(3, $request->search)
->get();
You can write a global scope for that it will automatically apply in all existing queries:
protected static function boot()
{
parent::boot();
static::addGlobalScope(function (Builder $builder) {
if (auth()->check() and auth()->user()->role == 3) {
$builder->where('staffName', 'LIKE', "%{request()->get('search_name')}%")
}
});
}

How to filter all and specific fields using Laravel Eloquent?

I want to refactor my source code when filtering records in all and specific fields.
Currently, I'm filtering all fields by passing 'search' in my request:
GET request: /api/users?search=
public function index(Request $request) {
$search = request("search") ?? "";
$users = $users->where(function ($query) use ($search) {
$query->where('code', "like", "%" . $search . "%");
$query->orWhere("first_name", "like", "%" . $search . "%");
$query->orWhere("last_name", "like", "%" . $search . "%");
$query->orWhereHas('department', function ($q) use ($search) {
$q->where('name', "like", "%" . $search . "%");
});
});
return $users->paginate(10);
}
I tried to follow this article, and now my GET request should be: /api/users?first_name=Juan&last_name=Cruz
Should I pass all request like /api/users?all=Juan to filter all fields at once?
What is the best approach when applying this kind of functionality?
If you know any acticles/resources which can serve as my reference, it would be a great help.
If you are following the article you shared, your url should look like /users?name=John&last_name=Doe&code=123
Then, you just call User::filter($filters)->get(), and it will automatically apply your query string filters to the query.
If you dont want to implement the filters functionality, you can use eloquent when method. It only apply the filter when a given condition is true.
public function index(Request $request) {
$query = User::query();
$query = $query->when(request('code'), function($query) {
$query->where('code', "like", '%'.request('code').'%')
})
->when(request('first_name'), function($query) {
$query->where('first_name', 'like', '%'.request('first_name').'%');
})
->when(request('last_name'), function($query) {
$query->where('last_name', 'like', '%'.request('last_name').'%');
})
->when(request('department'), function($query) {
$query->where('last_name', 'like', '%'.request('last_name').'%');
})
->when(request('department'), function($query) {
$query->whereHas('department', function($query) {
$query->where('name', 'like', '%'.request('department').'%');
});
});
return $query->paginate(10);
}

Search where clause orWhere clause in Laravel

I'm having two models Company and Contact search controller, my Contact model look something like:
class Contact extends Model {
public function companies()
{
return $this->belongsToMany('App\Company', 'company_contact', 'contact_id', 'company_id');
}
}
Few of my companies are referred as client for which I have a boolean field under the name of is_client.
I want to search contacts through a param request search which compares contact's first_name and last_name also with company name. If any body wants to have contact list of all the client then they can pass a extra request of client so accordingly I made a controller something like this:
Contact::when($request->client && $request->search, function ($q) use($request) {
$q->whereHas('companies', function ($q) use ($request) {
$q->where('is_client', true)->where(function ($q) use ($request) {
$q->orWhere('name', 'like', '%'. $request->search . '%');
});
})->where(function ($q) use ($request) {
$q->orWhere('first_name', 'like', '%'.$request->search.'%')
->orWhere('last_name', 'like', '%'.$request->search.'%');
});
})->when($request->client == null && $request->search, function ($q) use($request) {
$q->where('first_name', 'like', '%'.$request->search.'%')
->orWhere('last_name', 'like', '%'.$request->search.'%')
->orWhereHas('companies', function ($q) use ($request) {
$q->where('name', 'like', '%' . $request->search . '%');
});
})->when($request->client && $request->search == null, function ($q) use ($request) {
$q->whereHas('companies', function ($q) use ($request) {
$q->where('is_client', true);
});
})
->paginate(30)
It work perfectly fine with non-client contacts and for client contact initial list appears fine but when we type something it behaves not as desired. I may be doing some logic error. Help me out in this or guide me for better approach.
Your conditions when you got client and search are not correctly grouped
Contact::when($request->client && $request->search, function ($q) use($request) {
$q->whereHas('companies', function ($q) use ($request) {
$q->where('is_client', true);
})->where(function ($q) use ($request) {
$q->where('first_name', 'like', '%'.$request->search.'%')
->orWhere('last_name', 'like', '%'.$request->search.'%')
->orWhereHas('companies', function ($q) use ($request) {
$q->where('name', 'like', '%' . $request->search . '%');
});
});
})->when($request->client == null && $request->search, function ($q) use($request) {
$q->where('first_name', 'like', '%'.$request->search.'%')
->orWhere('last_name', 'like', '%'.$request->search.'%')
->orWhereHas('companies', function ($q) use ($request) {
$q->where('name', 'like', '%' . $request->search . '%');
});
})->when($request->client && $request->search == null, function ($q) use ($request) {
$q->whereHas('companies', function ($q) use ($request) {
$q->where('is_client', true);
});
})
->paginate(30)
the condition is_client must be separated from the condition of search
You should have noticed that you can easily shrink the code here
$contactQuery = Contact::query();
if ($request->client) {
$contactQuery->whereHas('companies', function ($q) {
$q->where('is_client', true);
});
}
if ($request->search) {
$contactQuery->where(function ($q) use ($request) {
$q->where('first_name', 'like', '%'.$request->search.'%')
->orWhere('last_name', 'like', '%'.$request->search.'%')
->orWhereHas('companies', function ($q) use ($request) {
$q->where('name', 'like', '%' . $request->search . '%');
});
});
}
$contacts = $contactQuery->paginate(30);

Executing query with where, whereHas in Laravel

I'm building a small application in Laravel 5.5 where I'm querying something like this:
$models = Contact::where('first_name', 'LIKE', '%'.$request->search_input.'%')
->orWhere('last_name', 'LIKE', '%'. $request->search_input.'%')
->orWhereHas('company', function ($q) use($request) {
$q->where('name', 'LIKE', '%'.$request->search_input.'%');
})
->whereHas('company', function ($query) {
$query->where('type', '=', 'Research');
})
->orderBy('created_at', 'desc')
->take(50)
->get();
The idea behind is, I want to search the list of contact with first_name, last_name and their company name (which is a relational data) with the search_input and filter them with type = Research in the company list, I mean only those contact should appear which is having company type as research.
But currently I'm getting all the list of contacts without filtering it with type. Help me out with this. Thanks.
You need to use where closure:
Contact::where(function($q) use($request) {
$q->where('first_name', 'like', '%' . $request->search_input . '%')
->orWhere('last_name', 'like', '%' . $request->search_input . '%')
->orWhereHas('company', function ($q) use($request) {
$q->where('name', 'like', '%' . $request->search_input . '%');
});
})
->whereHas('company', function ($query) {
$query->where('type', 'Research');
})
->latest()
->take(50)
->get();

Categories