Putting order by inside with(), in Laravel - php

I have a controller that filters the columns in it's table based on the variable received in the get query, then it returns it with another table's value based on the id of the variable gotten, But now for the $filter_by_name condition I want to filter by the first_name column in the users table, please how can i do that, i.e I want to return the users table ordered by their first_name column
DB-STRUCTURE
COMPANY-USERS TABLE
id company_id user_id role created modified active department_level
USERS TABLE
id
first_name
last_name
email
password
active_company
profile_photo_id
verified
active
remember_token
created
modified
Company-users Controller
public function getCompanyUsers($companyId)
{
$filter = strtolower(Input::get('filter'));
if($filter && $filter === 'on' ){
$filter_by_date = strtolower(Input::get('filter_by_date'));
$filter_by_name = strtolower(Input::get('filter_by_name'));
$filter_by_role = strtolower(Input::get('filter_by_role'));
if($filter_by_date){
if($filter_by_date == 'oldest'){
$users = CompanyUser::where('company_id', $companyId)->orderBy('created', 'DESC')
->with(['user','user.userDepartments','user.userDepartments.department'])->get();
return $users;
}else{
$users = CompanyUser::where('company_id', $companyId)->orderBy('created', 'ASC')
->with(['user','user.userDepartments','user.userDepartments.department'])->get();
return $users;
}
}elseif ($filter_by_name){
if($filter_by_name == 'ascending'){
$users = CompanyUser::where('company_id', $companyId)->orderBy('first_name', 'ASC')
->with(['user','user.userDepartments','user.userDepartments.department'])->get();
return $users;
}else{
$users = CompanyUser::where('company_id', $companyId)->orderBy('first_name', 'DESC')
->with(['user','user.userDepartments','user.userDepartments.department'])->get();
return $users;
}
}elseif($filter_by_role){
if($filter_by_role == 'member'){
$users = CompanyUser::where(['company_id' => $companyId,'role'=>'Member'])->with(['user','user.userDepartments','user.userDepartments.department'])->get();
// dd($users);
return $users;
}elseif($filter_by_role == 'manager'){
$users = CompanyUser::where(['company_id' => $companyId,'role'=>'Manager'])->with(['user','user.userDepartments','user.userDepartments.department'])->get();
return $users;
}else
$users = CompanyUser::where(['company_id' => $companyId,'role'=>'Admin'])->with(['user','user.userDepartments','user.userDepartments.department'])->get();
return $users;
}
}
$users = CompanyUser::where('company_id', $companyId)->
with(['user','user.userDepartments','user.userDepartments.department'])->get();
//dd($users);
return $users;
}

you can pass closer functions when eager loading to add constrains like that, see more on laravel docs:
$users = CompanyUser::with(['user'=> function ($query) {
$query->orderBy('first_name', 'desc');
}
])
->where('company_id', $companyId)
->get();

You have to do orderBy on a relational model like below:
$users = CompanyUser::where('company_id', $companyId)
->with(['user' => function($subQuery){
$subQuery->orderBy('first_name', 'ASC');
}])
->with(['user.userDepartments','user.userDepartments.department'])
->get();
return $users;

One way is to add column 'first_name' in the 'COMPANY-USERS' TABLE
And now you can do order by ('first_name','ASC').
I think its not the best way but no other idea comes in my head.

Related

How Can I get Latest 5 Commentors user information By Laravel Relational Eloquent eager loading

I'm in a situation where I need to display the last 5 unique commenters information at the top of the comment list as follows screenshot.
comment image
To do this. I did as follows:
Post Model
public function comments()
{
return $this->hasMany(Comment::class);
}
public function commenter_avatars(){
return $this->comments()->distinct('user_id')
->select('id','post_id','user_id','parent_id')
->whereNull('parent_id')
->with('user')->limit(5);
}
My Controller method as follows
public function index() {
$feeds = auth()->user()
->posts()
->with(['user:id,first_name,last_name,username,avatar', 'media', 'commenter_avatars'])
->orderBy('id', 'desc')
->paginate(10);
return PostResource::collection($feeds);
}
I tried to use groupBy and Distinct.. But did't work as expected.
Did I miss something? or Have there any more best way to solve this?
Thank you in advance!
Noted: I am using latest Laravel (8.48ˆ)
I don't know about your joining of post, user and comments table. But i guess, you can do something similar to following.
At first get latest 5 unique user id of one post:
$userIds = Comments::where("post_id", $post_id)->distinct("user_id")->orderBy("id")
->limit(5)->pluck('user_id');
Then, fetch those user information
$users = Users::whereIn("id", $userIds )->get();
Then, you can return those users
UPDATE
You may use map() to fetch and reorder output. Following is an idea for you:
In Controller:
public function index(Request $request) {
$skipNumber = $request->input("skip"); // this is need for offsetting purpose
$userIds = [];
$feeds = Posts::with("comments")->where("comments.user_id", Auth::id())
->skip($skipNumber)->take(10)->orderBy('comments.id', 'desc')
->map(function ($item) use($userIds){
$users = [];
$count = 0;
foreach($item["comments"] as $comment) {
if(!in_array($comment["user_id"], $userIds) && $count < 5){
$count++;
$userIds.push($comment["user_id"])
$user = User::where("id", $comment["user_id"])->first();
$users.push($user);
}
if($count == 5) break;
}
$data = [
"post" => $item,
"latest_users" => $users
];
return $data;
})->get();
return PostResource::collection($feeds);
}
My code syntax may be slightly wrong. Hopefully you will get the idea.
I have solved this issue by using eloquent-eager-limit
https://github.com/staudenmeir/eloquent-eager-limit

Laravel | How to perform search with multiple attributes

I am creating property website and i am doing search with number of attributes, but the problem is that in search controller i have very large code and its very difficult to handle, is there any other solution exist in laravel?
$list_property = Listing_property::where([
['property_type', $request['property_type']],
['city', $request['city']],
['location', $request['location']],
['property_area_type', $request['property_area_type']],
['property_size', $request['property_size']],
['price', $min],
['price', $max]
])
->orderBy('updated_at', 'DESC')
->paginate(21);
The easiest way to search multiple column :
public function search(Request $request){
$query = $request->search;
$users = DB::table('users');
if($query){
$users = $users->where('name', 'LIKE', "%$query%");
}
if($request->city){
$users = $users->where('city',$request->city);
}
if($request->town){
$users = $users->where('town', $request->town);
}
if($request->unit){
$users = $users->where('unit', $request->unit);
}
$users->get();
return view('users.index')->with('users', $users);
}

How to filter data in laravel by relationship of parent object

I've got 3 models: Client, Organization nad Industry. I want to filter and display clients, that are assigned to any organization in choosen Industry.
Relations:
Client N-N Organization N-N Industry
One client can have multiple organizations and one organization can have multiple industries. I use form with GET method.
My controller:
class ClientListsController extends Controller
{
public function index()
{
$clients = new Client;
$queries = array();
$columns = ['statuses'];
foreach ($columns as $column) {
if (request()->has($column)) {
if ($column == 'industries') {
// filter by industries
}
if ($column == 'statuses' && request($column) == 1 ) {
$clients = optional($clients->has('organizations'))->paginate(100, ['*'], 'client_page');
}else if($column == 'statuses' && request($column) == null ){
$clients = Client::paginate(100, ['*'], 'client_page');
}
else{
$clients = $clients->whereDoesntHave('organizations')->paginate(100, ['*'], 'client_page');
}
$queries[$column] = request($column);
}else{
$clients = Client::paginate(100, ['*'], 'client_page');
}
}
$organizations = Organization::paginate(100, ['*'], 'org_page');
return view('clientLists.index')->with(['clients' => $clients, 'organizations' => $organizations]);
}
}
I've tried multiple things, such as looping through current result with organizations, but that returned true or false whe using has().
I've also tried using $clients = Client::all() but my point is to paginate it further so I've worked with Client::where('id', '*') so I didn't get collection, but that caused, that statuses filter does not work.
I'm working with Laravel 6. Do you have any idea how to make universal filtering with pagination? Thanks a lot!
EDIT
relevant relationship in details (f.e. pivot table for Client - Organization is client_organization) with foreign keys defined.
App\Client
public function Organizations()
{
return $this->belongsToMany('App\Organization')->withTimestamps();
}
App\Organization
public function Clients()
{
return $this->belongsToMany('App\Client')->withTimestamps();
}
public function Industries()
{
return $this->belongsToMany('App\Industry')->withTimestamps();
}
App\Industry
public function Organizations()
{
return $this->belongsToMany('App\Organization')->withTimestamps();
}
EDIT 2:
I'm trying to achieve my goal through SQL query, but I'm stuck with JOINS with many-to-many relationships.
$clients = DB::table('clients')
->leftJoin('client_organization', 'client_organization.client_id', '=', 'clients.id')->leftJoin('industry_organization', 'industry_organization.organization_id', '=', 'organizations.id')
->whereIn('industry_organization.industry_id', request($column))
->paginate(100, ['*'], 'client_page');
}
select
count(*) as aggregate
from
`clients`
left join `client_organization` on `client_organization`.`client_id` = `clients`.`id`
left join `industry_organization` on `industry_organization`.`organization_id` = `organizations`.`id`
where
`industry_organization`.`industry_id` in (2)`
This query returns Column not found: 1054 Unknown column 'organizations.id' in 'on clause'
So, if you have the $industry you can find all of the Organisations within it using your relationships:
$organisations = $industry->organisations();
Then, you want to get all clients for each organisation:
$clients = [];
foreach ($organisations as $organisation) {
$clients[$organisation] = $orgination->clients()->toArray();
}
Or, alternatively, to give it a go all in one query:
$industries = request($column);
$clients = DB::table('clients')
->leftJoin('client_organization', 'client_organization.client_id', '=', 'clients.id')
->leftJoin('organizations', 'organizations.id', '=', 'client_organization.id')
->leftJoin('industry_organization', 'industry_organization.organization_id', '=', 'organizations.id')
->whereIn('organisations.industry_id', $industries)
->paginate(10);
Try this line by line to check it out and debug it easily (try ->get() after the first leftJoin and check that it works; if that's okay, try it after the second.
I managed to do it this way:
if (request()->industries[0] != "") {
$clients = $clients->with('organizations')->whereHas('organizations', function($query){
$query->with('industries')->whereHas('industries', function($query2){
$query2->whereIn('industry_id', request()->industries);
});
});
}
Thanks for your help boys :)

I cannot fetch data from using this code

public function orderView(Request $request){
$orderByID = $request->id;
$orderDetailsView = DB::table('order_details')
->where('order_id',$orderByID)
->get();
return view('admin.order.vieworder', ['orderDetailsView' => $orderDetailsView]);
}
If i return only $orderByID it shows the id. But using this id i cannot fetch data from the database table. it shows empty array.
Try this code
$orderss = DB::table('order_details')->where('order_id', '=', $orderByID)->get();

Return a specific column from a many-many relationship using php in laravel

In my database I have two models, User and Role defined as many-many relationship, I'm trying to write a code in laravel that takes the id from the roles table and gets all the user fullnames from the users table.
I have defined a route that looks like this :
Route::get('roleUser/{role}', 'RoleController#RoleNames');
in which i pass the role name with it, as you see above
In my RoleController, I defined the method roleNames to do the job
public function RoleNames($role)
{
$idrole = Role::where('name', '=', $role)->first()->id;
//$iduser = DB::table('assigned_roles')->where('role_id', '=', $idrole)->first()->id;
$iduser = DB::table('assigned_roles')->where('role_id', '=', $idrole)->get(array('id'));
$usersUnderRole = array();
foreach ($iduser as $idusers) {
$usersUnderRole = array_add($usersUnderRole, $idrole, $idusers);
$full_name = DB::table('users')->where('id', '=', $idusers)->get()->full_name;
}
return $this->respond([
'result' => $this -> roleTransformer->transform($full_name)
]);
}
This code is meant to take the role_id from the roles table and gets the appropriate user_ids by the pivot table assigned_roles, puts them in an array and fetches the correspondent full_names, but it says this error:
Object of class stdClass could not be converted to string
Any advice on how to get it to work?
This will give you all the users who belong to the given role:
$role_id = 3;
$result = DB::table('role_user')
->select('users.full_name')
->where('role_user.role_id', $role_id)
->leftJoin('users', 'users.id', '=', 'role_user.user_id')
->get();
var_dump($result);
$full_names = DB::table('users')->where('id', '=', $idusers)->lists('users.full_name');
Besides the solution #lamzazo provided, there is another way to go with the answer :
$role2 = Role::where('name', '=', $role)->first();
$idrole = Role::where('name', '=', $role)->first()->id;
//$iduser = DB::table('assigned_roles')->where('role_id', '=', $idrole);
$user = DB::table('assigned_roles')->where('role_id', '=', $idrole)->get();
// $user = DB::table('users')->where('id', '=', $iduser);
// return $this->respond($iduser[0]->user_id);
$userArray = array();
$i=0;
foreach ($user as $users) {
$iduser= $users->user_id;
$allusers = User::where('id', '=', $iduser)->get(array('id', 'full_name'));
$userArray = array_add($userArray, $i . '', $allusers);
$i++;
}
return $this->respond([
'result' => $this -> roleTransformer->testTransform($role2, $userArray)
]);

Categories