Is it possible to group where clauses in Laravel? - 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

Related

Messages: show User Name with variable User_ID

i have 2 tables, to create messages, conversation and messages. If you create a new message with a new partner, you get a new conversation ID, so if you started the conversation, your user_id is in the messages table and in the conversation table you are user_one your writting partner is user_two. But if somewhere else is started the conversation you are user_two and in the messages table, the user_id ist the other user.
I need the name from your writting partners to show in a list.
Example:
Mario / Last Message / -> if i click so i will see the messages
Maria / Last Message / -> if i click so i will see the messages
To save and show the messages is working. I need only to read the name from your conversation partner.
With my solution i get the name of the writting partners, but only if you are User two.
The problem is the join.
I need something like JOINNOTIN
My table:
Conversation
'id'
'user_one'
'user_two'
Messages
'id'
'message'
'userid'
'conversationid'
My Query:
$messages = DB::table('conversation')
->select('messages.message', 'conversation.user_one', 'conversation.user_two', 'users.name', 'users.firstname', 'messages.id', 'messages.created_at', 'conversation.id')
->join('users', 'users.id', '=', 'conversation.user_one')
->join('messages', 'messages.conversation_id', '=', 'conversation.id')
->where('messages.userid', '!=', Auth::user()->id)
->orwhere('conversation.user_one', '=', Auth::user()->id)
->orwhere('conversation.user_two', '=', Auth::user()->id)
->groupBy('messages.conversation_id')
->latest()
->get();
You need the condition user_one == auth()->user()->id OR user_two == auth()->user()->id in the join() method.
$authId = auth()->user()->id;
$messages = DB::table('conversation')
->select('messages.message', 'conversation.user_one', 'conversation.user_two', 'users.name', 'users.firstname', 'messages.id', 'messages.created_at', 'conversation.id')
->join('users', function ($join) use ($authId) {
$join->on('id', '=', 'user_one')->orOn('id', '=', 'user_two')
->where('id', '!=', $authId);
})
->join('messages', 'messages.conversation_id', '=', 'conversation.id')
->where('messages.userid', '!=', Auth::user()->id)
->orwhere('conversation.user_one', '=', Auth::user()->id)
->orwhere('conversation.user_two', '=', Auth::user()->id)
->groupBy('messages.conversation_id')
->latest()
->get();

Laravel cannot get duplicate value datas using join query

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

how to select particular user id in laravel?

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)

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();

Foreach loop not going through correctly

I have a form to mark employees' attendance that HR fill in which is looped over every current employee. I need it to show the current values where attendance is marked in the database, otherwise it should be blank.
In my controller I query the existing results:
$results = Attendance::where('Sdate', '=', date("Y-m-d", strtotime($TheDate))) ->get();
Then loop through them to get the employee details:
foreach($results as $result)
{
$contractors = DB::table('contractors') ->where('contractors.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $employees = DB::table('current_employees') ->where('current_employees.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $showEmployees = array_merge($contractors, $employees);
}
This should exclude all employees who have a record saved in attendance for that date, however it doesn't seem to be looping correctly. It will exclude some results, but not all. If I return the results variable I get the correct list of records so I know that part is working correctly.
What I'm looking to achieve in my view is something like:
#foreach($attendance as $results)
Show form where there's an existing record for this date and department
#endforeach
#foreach($employees as $employee)
Show form for all employees in this department (but should exclude results where there is a record in attendance)
#endforeach
The problem with your code is you are saving the result in a variable and not in an array.
Your solution would be to store the data in an array
foreach($results as $result)
{
$contractors[] = DB::table('contractors') ->where('contractors.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $employees = DB::table('current_employees') ->where('current_employees.AreaPosition', '=', $department) ->where('PRN', '!=', $result->PRN) ->get(); $showEmployees = array_merge($contractors, $employees);
}
Try printing contractors array and see what happens.I hope this works
This was answered for me on Laracasts.
What I needed to do was create a list of the variable to check against (employee number)
$PRNs = Attendance::where('Sdate', '=', date("Y-m-d", strtotime($TheDate)))->lists('PRN');
And then use Laravel's whereNotIn, checking against the list.
$contractors = DB::table('contractors')
->where('contractors.AreaPosition', '=', $department)
->whereNotIn('PRN', $PRNs)
->get();

Resources