how to select particular user id in laravel? - laravel-5

i have tried the below code to calculate the attendance of a particular user but calculations is happening for all the users available in the table. I'm new to laravel so i need help.
in the controller:
$TController =DB::table('tbl_attendancetime')
->select('username','statusid','status_description','user_id')
->groupby('username')
->get();
$TController1 =DB::table('tbl_attendancetime')//->distinct()
->select('statusid')
->where('statusid','=', 0)//->groupBy('user_id')
->get();
$TController2 =DB::table('tbl_attendancetime')
->select('statusid')//->groupby('user_id')
->where('statusid','=', 1)//->distinct()->get(array('user_id'));
//->groupBy('user_id')
->get();
$TController3 =DB::table('tbl_attendancetime')
->select('status_description')//->groupby('user_id')
->where('status_description','=',3)
->groupBy('user_id')
->get();
$TController4 =DB::table('tbl_attendancetime')
->select(
'status_description')//->groupby('user_id')
->where('status_description','4')
->groupBy('user_id')
->get();
$TController5 =DB::table('tbl_attendancetime')
->select(
'status_description')//->groupby('user_id')
->where('status_description','5')
->groupBy('user_id')
->get();
foreach($TController as $row)
{
$a=count($TController1);
$p=count($TController2);
$e=count($TController3);
$s=count($TController4);
$su=count($TController5);
}

In your first query , in where condition you used this line
->where('tbl_attendancetime.user_id', 'create_register.user_id')
for this reason, you get all information for the user id which are presented on both tbl_attendancetime & create_register.
If you want particular user information, use specific ID.
For example
->where('tbl_attendancetime.user_id', 1)

Related

Product filtering problems with Laravel Query Builder

I try to implement ajax based filtering in my ecommerce project homepage for searching a product. I am using Laravel Query Builder. My Query for filtering products is given below-
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->leftjoin('products_to_categories','products.products_id','products_to_categories.products_id')
->leftjoin('categories','products_to_categories.categories_id','categories.categories_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->take(5)
->get();
I get every product twice in search result. don't know why. A sample response is given in this picture.
Can anyone help me to optimize my query for getting the desired result?
join with categories table only when you have to.
and select your columns strictly. then group by selected columns.
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->leftjoin('products_to_categories','products.products_id','products_to_categories.products_id')
->leftjoin('categories','products_to_categories.categories_id','categories.categories_id')
->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->select(['products_name','products.id','image_path'])
->groupBy(['products_name','products.id','image_path'])
->take(5)
->get();
you can use from groupBy to avoid comming repeated data.
try this one:
$result= DB::table('products')
->leftjoin('products_description','products.products_id','products_description.products_id')
->leftjoin('image_categories', 'products.products_image', '=', 'image_categories.image_id')
->when($category_slug, function($q) use ($category_slug) {
return $q->where('categories.categories_slug', $category_slug);
})
->where('products_name','like',"%{$querval}%")
->where('image_categories.image_type','=','ACTUAL')
->orderby('products.products_id','DESC')
->groupBy('products_name','products.products_id')
->take(5)
->get();

How create a subquery with eloquent and laravel with softdeletes

I am using Laravel 7 and Vue.js 2.
I want to retrieve the tasks that are not assigned to a specific user.
I made the following code that works correctly.
$tasks_user = TaskUser::select('task_id')
->where('user_id', $id)
->get();
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', $tasks_user)
->distinct()
->get();
By the way to be more elegant I decided to transform the above code into a single query as follows:
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', function($q) use ($id)
{
$q->select('task_id')
->from('task_user')
->where('user_id', $id)
->get();
})
->distinct()
->get();
Unfortunately I discovered that the above query didn't work because it doesn't considers softdeletes.
For example, if the user with id 3 was related with the task 7 but now that row has been deleted with softdeletes in the table task_user, the first code returns also the task with id 7 (correctly) and the second one not (uncorrectly).
So finally, I must do a single query that works as the first code.
Can help?
You can actually combine both approaches. whereNotIn accepts also an Eloquent Query, it doesnt need to be a callback. Try this:
$userRelatedTasksQuery = TaskUser::select('task_id')
->where('user_id', $id);
$tasks = Task::select('tasks.id as id', 'tasks.name as name', 'tasks.description as description')
->join('task_user', 'tasks.id', '=', 'task_user.task_id')
->whereNotIn('task_user.task_id', $userRelatedTasksQuery)
->distinct()
->get();
Be sure to not use get() at the end of the $userReleatedTasksQuery, as you want the eloquent query instance, not the result.

Joining tables laravel and keeping first table ids

I noticed that when you join tables in laravel using lefJoin() or Join the ids that it returns are the one of the last table. So I was wondering if I can join two tables in laravel and keepig the ids of the first table?
Here's my query
$comments = DB::table('posts')
->where('posts.parent_id',$id)
->leftJoin('profiles', 'profiles.user_id', '=', 'posts.user_id')
->leftJoin('users', 'users.id', '=', 'posts.user_id')
->get();
you should show ur code .
but try like this
$query = Users::where("users.user_id","=",$users->user_id)
if ($request->exists('product_name')) {
$query->join('products', 'users.user_id', 'products.user_id');
$query->where('products.product_name', 'like', '%' . $request->input('product_name')
. '%');
}
$test= $query->get();
Better use aliases in this case.For Example
DB::table('table_1')->select('table1.column_name as alias_name', 'table_2.column_name as alias_name2')->leftjoin('table_2', 'table_2.userId', '=', 'table_1.id')
->get();
You can select all from first table and nothing from second
DB::table('table_1')->join('table_2', 'table_1.id', '=', 'table_2.table_1_id')
->selectRaw('table_1.*')
->get();
Example DB::table('guests')->join('answers', 'guests.id', '=', 'answers.guest_id')
->selectRaw('guests.*')->get()
You also can select here anything from another table
use: ->addSelect('table_2.column')

Eloquent User Where Clause with Entrust Library

I'm trying to select all users for a company. But only users who has "admin" role status (Entrust, etc.).
User::where('company_id', Auth::user()->company_id)->hasRole('admin')->get();
The above is throwing an error. Left a bit lost on how to run such a query. Where am I going wrong with this? Very little documentation on Entrust.
You can use plain Eloquent for this. The whereHas method would generate one query:
$users = User::where('company_id', Auth::user()->company_id)
->whereHas('roles', function($query) {
$query->where('name', 'admin');
})->get();
Or, you can just get the roles and then get all of that role's users. This would generate two queries, but ultimately achieve the same thing.
$users = Role::where('name', 'admin')
->first()
->users()
->where('company_id', Auth::user()->company_id)
->get();
Think you need to get all the users having the company_id first
$users = User::where('company_id', Auth::user()->company_id)->get();
Then loop through $users and check hasRole()
foreach ($users as $user) {
if($user->hasRole('admin')){
//user is admin
}
}
UPDATE
This might be a dirty solution but you can try to do a manual query
$admin = DB::table('role_user')
->join('users', 'users.id', '=', 'role_user.user_id')
->join('roles', 'roles.id', '=', 'role_user.role_id')
->where('roles.name', 'admin')->get();

How to select instances with multiple relations?

I have some models Featured_Course_Request, Course_Request, Response and Teacher. Featured_Course_Request hasOne Course_Request and Course_Request hasMany Response by Teacher.
I want to get the only Featured_Course_Requests on which logged in teacher has not responded (have no Response by logged in teacher.) How can I do it?
I am trying to achieve it with the following code but it is not giving correct output.
$featured_course_request = Featured_Course_Resquest::whereRaw('remaining_coins >= coins_per_click')->where('status', '=', 'open')
->whereHas('courseRequest', function($q) use ($teacher){
$q->whereHas('responses', function($qe) use ($teacher){
$qe->where('teacherID', '!=', $teacher->id);
});
});
You can target nested relations with the dot syntax: 'courseRequest.responses' further more you'll need whereDoesntHave instead of whereHas:
$featured_course_request = Featured_Course_Resquest::whereRaw('remaining_coins >= coins_per_click')
->where('status', '=', 'open')
->whereDoesntHave('courseRequest.responses', function($q) use ($teacher){
$qe->where('teacherID', '=', $teacher->id);
})
->get();

Resources