Laravel cannot get duplicate value datas using join query - laravel

I using laravel 5.3 in my project.
I want to get result for duplicate value (i.e.,)If the column has same id it should be displayed.
public function getSendedRequests($broker_id) {
$broker = User::where('broker_id', $broker_id)->lists('id');
//my customer sended request
$getUser = ProfileRequest::whereIn('requested_id', $broker)->lists('requested_id');
$getReq = ProfileRequest::whereIn('requested_id', $broker)->lists('user_id');
$sendUsers = $this->commonRequestQuery($getUser);
$receivedUsers = $this->commonRequestQuery($getReq);
return view('profilerequest')->with(['sendUsers' => $sendUsers->paginate(10), 'receivedUsers' => $receivedUsers->get(), 'title' => trans('messages.customer-sended-requests')]);
}
public function commonRequestQuery($values) {
return DB::table('users')
->leftJoin('basic_informations', 'users.id', '=', 'basic_informations.user_id')
->leftJoin('physical_attributes', 'users.id', '=', 'physical_attributes.user_id')
->leftJoin('religion_and_ethnicity', 'users.id', '=', 'religion_and_ethnicity.user_id')
->leftJoin('occupational_attributes', 'users.id', '=', 'occupational_attributes.user_id')
->leftJoin('personal_interests', 'users.id', '=', 'personal_interests.user_id')
->select('users.id', 'users.user_name', 'users.email', 'users.gender', 'basic_informations.age', 'basic_informations.unique_id', 'basic_informations.mother_tongue', 'religion_and_ethnicity.religion_id', 'religion_and_ethnicity.caste_id', 'occupational_attributes.education_id', 'occupational_attributes.occupation_id')
->whereNotNull('basic_informations.user_id')
->whereNotNull('physical_attributes.user_id')
->whereNotNull('religion_and_ethnicity.user_id')
->whereNotNull('occupational_attributes.user_id')
->whereIn('users.id', $values);
}
I get users id from user table for same broker and check whether they have requested some one in another table.
My problem is if the same user requested for two another users then i cannnot get result twice instead of that i can get only one time but another users(request received users is displayed propery).
Profile request table looks like image
In requested_id table im having same id two times i want to display parallely like Who requested for whom

Related

add new element in laravel collection for friendship in laravel

I am trying to display all users weather I send him friend request or not with friendship status for that. First of all I load all friend requests and received request in that manner.
$users2 = User::with(['sentRequests' => function ($query) {
...
}])->with(['recievedRequests' => function ($query) {
...
}])->where('users.id', Auth::id())->get();
But then I also want to load other users so for that I create some query like that
$sent = Friendship::where('user_id', Auth::id())
->pluck('friend_id');
$received = Friendship::where('friend_id', Auth::id())
->pluck('user_id');
$users3 = User::whereNotIn('id', $sent)
->whereNotIn('id', $received)
->where('id', '!=', Auth::id())
->get();
Now I want to append $users3 to $users2 but when I done like that
$users2->put('otherUsers',$users3);
It appended but not in the right manner , I want to append it in same manner as 'sentRequests' and 'recievedRequests' are.

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.

Is it possible to group where clauses in Laravel?

I want to group item based on ORDER NO Where sales made by login User
$sales = KotOrder::orderBy('id','DESC')
->where('prepared_by', Auth::user()->id)
->get();
return view('settups.restaurant.pos.report', \compact('sales'));
And I tried this
$sales = KotOrder::orderBy('id','DESC')
->groupBy('qrtxt')
->where('prepared_by', Auth::user()->id)
->get();
return view('settups.restaurant.pos.report', \compact('sales'));
It gives out error

SQL query similar to the concept of set (complement or except)

I need your help. I just want to display the name of all users (from table users) whose are not in table documents but in today's date
I've tried but I failed
Here is my code
public function activeUser(){
$documents = DB::table('users')
->leftJoin('documents', 'users.id', '=', 'documents.user_id')
->select('users.fname','users.mname','users.sname','documents.created_at')
->where('documents.user_id',null)
->where('documents.created_at',null)
->get();
return view('activity.activity')->with('documents', $documents);
}
I don't know to add current date in the where clause.
The above code gave me the all users who are note in table documents but if user exist in documents once will always considered as is in that documents table.
You can use "whereBetween" (https://laravel.com/docs/6.x/queries#where-clauses).
public function activeUser(){
$yesterday = Carbon::yesterday();
$today = Carbon::today();
$documents = DB::table('users')
->leftJoin('documents', 'users.id', '=', 'documents.user_id')
->select('users.fname','users.mname','users.sname','documents.created_at')
->where('documents.user_id',null)
->whereBetween('documents.created_at', [$yesterday, $today])
->get();
return view('activity.activity')->with('documents', $documents);
}
Remember, you can also use "whereNotInBetween" if that better matches your business logic case.

How to fetch data from multiple table according to id by using query builder in laravel

How to fetch data from multiple table according to id by using query builder in Laravel?
example:
I have three tables such as student,parent and teacher.Now I want to fetch data from this three tables(student,parent and teacher) according to id for editing.So how can I do that?
see bellow codes please
public function edit($id)
{
$values=DB::table("student")->find($id);
$values=DB::table("parent")->find($id);
$values=DB::table("teacher")->find($id);
return view("info", compact("values"));
}
You probably want to fetch data according to id that you have. Of course that other tables need to have foreign keys of that table>
Here is laravel documentation for joins:
https://laravel.com/docs/5.8/queries#joins
and here is example code from docs:
$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();

Resources