Laravel query builder subquery separate tables - laravel-5

How do I write this in Laravel Query Builder:
I have a users table that has one Customer with column 'phone'.
a customers table that belongs to User on 'user_id' with column 'mobile'.
I would like to find any customer id that with a phone number what either 'phone' or 'mobile'. Something like this:
select id
from customers
where mobile = '5555555555' or
user_id = (select id
from users
where phone = '5555555555')

I think this is what I need:
$customers = DB::table('customers')
->whereIn('user_id', function ($query) use ($phoneNumber) {
$query->select('id')
->from('users')
->where('users.phone', $phoneNumber);
})
->orWhere('mobile', $phoneNumber)
->pluck('id');

Related

Laravel count roles of user based on another table

I have a table of Employee which has organisation column with different names, each organization has employee and employer role, i want to fetch organization name and count total number of employee and employer
like that with keys
['organisation' => 'UAE Hospital'
'Employee' => '10'
'Employer' => '5']
i have try this so far but it shows N+1 Query.
$data = Employee::where("organisation", 'UAE Hospital')
->select('organisation')
->whereHas("user.roles", function ($query) {
$query->where("id", '2');
})
->OrWhereHas("user.roles", function ($query) {
$query->where("id", '3');
})
->get()->toArray();
For roles i am using spatie package.

Laravel query builder where condition from relationship table

I have following tables
Messages:
user_id
group_id
text
created_at
And
group_users:
user_id
left_at
group_id
These tables have relationship via "group_id".
I need "Messages" that created before "group_user" left group.
In other word, i need a query like this:
$messages = Message::where('created_at','<',group_user.left_at)->get();
How can i achieve this?
You can simply do it like this assuming you have the user ID at hand:
Message::join('group_users', 'messages.group_id', '=', 'group_users.group_id')
->where('group_users.user_id', $id)
->where('group_users.left_at', '>', 'messages.created_at')
->get();
it can be like the code below(untested):
in the Message model
public function group(){
return $this->belongsTo(Group::class);
}
in the controller
$messages = Message::whereHas('group', function($q){
$q->where('left_at','>','messages.created_at');
})->get();

Laravel Lumen eloquent left join returns joined table data rather than primary able data

I have two tables company and courses table
The table has the following fields
companies table
id, full_name, email, deleted_at
and courses table
courses table
id, company_id, course_name,deleted_at
Now i would like to retrieve all courses which company is not deleted. So in my controller i have added
public function index(Request $request){
$query = Courses::query();
$query = $query->leftJoin('companies','companies.id','=','courses.company_id');
$query->whereNull('companies.deleted_at');
if($request->get('filter_name')){
$query = $query->where('courses.name', 'like', '%' . $request->get('filter_name') . '%');
}
return response()->json($query->paginate($request->get("perPage")));
}
When i run the above it returns companies data rather than courses. Where am i going wrong or what am i missing out?
If you have use eager loading in both of your model, you can use this kind of approach.
$all_course = Courses::with(['company', function($query) {
return $query->whereNotNull('deleted_at');
}])->get();
As you can see, I query in the Courses model to return all the courses but I added some filter in the company relationship. where in I used whereNotNull to get only the company that's not deleted.

Laravel Eloquent with() selecting specific column doesn't return results

Say I have 2 models, Category and POI where 1 Category can have many POIs.
$categoryDetails = Category::with([
'pois' => function ($query) {
$query->where('is_poi_enabled', true);
},
])->findOrFail($id);
The above query returns results from the specific Category as well as its POIs.
However, with the query below:
$query->select('id', 'name')->where('is_poi_enabled', true);
The POIs become empty in the collection.
Any idea why this is happening? When added a select clause to the Eloquent ORM?
While doing a select it's required to fetch the Relationship local or Primary key.
For an example POIs table contains category_id then it's required to select it
Try this:
$categoryDetails = Category::with([
'pois' => function ($query) {
$query->select(['id', 'category_id', 'is_poi_enabled'])
->where('is_poi_enabled', true);
},
])->findOrFail($id);
Good luck!

How can we alias the name of a child table column without using select in Laravel Eloquent

Category Table Field:'id', 'title', 'slug'
Blog Table Field:'id','title', 'slug', 'short_desc', 'detail_desc', 'publish_date', 'seo_title', 'seo_keywords', 'seo_description', 'image', 'view_count', 'status'
My query is fine with this syntax :
$data['rows'] = Blog::select('blogs.id','blogs.title', 'blogs.slug', 'blogs.short_desc', 'blogs.detail_desc', 'blogs.publish_date','blogs.seo_title', 'blogs.seo_keywords', 'blogs.seo_description', 'blogs.image', 'blogs.view_count', 'blogs.status', 'cat.id as cat_id')
->join('categories as cat', 'blogs.category_id', '=', 'cat.id')
->paginate($this->pagination_limit);
i want to create this type of syntax with alise category id:
$data['rows'] = Blog::join('categories', 'blogs.category_id', '=', 'categories.id')
->paginate($this->pagination_limit);
So, how can i change categories table id as cat_id without using select. Because when i use without select and use blog id, it relapsed with category id.
Use addSelect like this:
$data['rows'] = Blog::join('categories', 'blogs.category_id', '=', 'categories.id')
->select('*')
->addSelect('categories.id AS cat_id', 'blogs.id AS id') // alias categories id and rewrite the id for blogs
->paginate($this->pagination_limit);

Resources