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();
Related
I have just created some payment system for a school and I need to filter my data to Paid or Not Paid category.
$queryBuilder = Student::query()
->leftJoin('fee_scheem_maps', 'students.students_id', '=', 'fee_scheem_maps.sfc_student_id')
->leftJoin('fee_scheems', 'fee_scheem_maps.sfc_feescheem_id', '=', 'fee_scheems.fee_scheems_id')
->leftJoin('individual_fee_scheems', 'fee_scheem_maps.sifc_feescheem_id', '=', 'individual_fee_scheems.ifs_id')
->leftJoin('fee_groups', 'fee_scheems.fee_scheems_id', '=', 'fee_groups.fg_fee_scheem_id')
->leftJoin('school_fee_collectors', 'students.students_id', '=', 'school_fee_collectors.fc_student_id')
// ->Where('school_fee_collectors.fc_fee_group_id', $fromDate)
// ->orWhereNull('school_fee_collectors.fc_fee_group_id', $pstatus)
// ->orWhereNull('school_fee_collectors.fc_fee_group_id', $fromDate)
->when($fromDate, function ($query) use ($fromDate) {
return $query->where('school_fee_collectors.fc_fee_group_id', $fromDate);
})
->select('*', DB::raw('count(students_id) as total'));
It is working fine with one clause: I need to randomly choose where or notWhere in my case...
$queryBuilder = Student::query()
->leftJoin('fee_scheem_maps', 'students.students_id', '=', 'fee_scheem_maps.sfc_student_id')
->leftJoin('fee_scheems', 'fee_scheem_maps.sfc_feescheem_id', '=', 'fee_scheems.fee_scheems_id')
->leftJoin('individual_fee_scheems', 'fee_scheem_maps.sifc_feescheem_id', '=', 'individual_fee_scheems.ifs_id')
->leftJoin('fee_groups', 'fee_scheems.fee_scheems_id', '=', 'fee_groups.fg_fee_scheem_id')
->leftJoin('school_fee_collectors', 'students.students_id', '=', 'school_fee_collectors.fc_student_id')
->when($fromDate, function ($query) use ($fromDate) {
return $query->where('school_fee_collectors.fc_fee_group_id', $fromDate);
})
->where(function($query) use ($request){
if($request->paid_status){
$query->where('table_name.columName',$request->paid_status)
}
})->select('*', DB::raw('count(students_id) as total'));
you can use where like this , when your $request->paid_status is null means have no filter about paid not paid thats time query is return everything but when is have filter thats time only this where is working
->where(function($query) use ($request){
if($request->paid_status){
$query->where('table_name.columName',$request->paid_status)
}
})
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.
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
looking for some help, I'm fetching a survey and questions, from method, when select some survey by id, from a route. But when I delete a survey, and when writing in url like http://localhost/survey_details/27 ( 27 is survey Id , which not exist anymore), I'm getting not some expection or redirection or Page not found info, but getting error where undefined variable $dat. But I need some how to show 404 page not found, or just redirect to other page if id not exist. Can some one help me?
here is my route:
Route::get('survey_draft/{id}', 'SurveyController#editSurveyDraft');
Here is my controller:
public function viewSurvey($id)
{
$object = DB::table('question')
->where('survey_id', '=', $id)
->where('name', '!=', NULL)
->get();
$teamName = DB::table('survey')->where('surveyId', '=', $id)
->join('team', 'survey.teamId', '=', 'team.teamId')
->join('company', 'company.id', '=', 'team.companyID')
->get();
$company = Auth::user()->company;
$data = Survey::where('surveyId', '=', $id)->get();
$teams = Auth::user()->teams;
$checkUserTeam = DB::table('teammembersall')
->where('UserId', '=', Auth::user()->id)
->get();
$members = Survey::where('surveyId', '=', $id)
->join('team', 'team.teamId', '=', 'survey.teamId')
->join('teammembersall', 'teammembersall.TeamId', '=', 'team.TeamId')
->join('users', 'users.id', '=', 'teammembersall.UserId')
->select('users.*')
->whereNotExists(function ($query) use ($id) {
$query->select(DB::raw(1))
->from('answer')
->whereRaw('answer.answerAboutUserId = users.id')
->where('answer.surveyId', '=', $id)
->where('answer.member_id', '=', Auth::user()->id);
})
->get();
$questions = DB::table('answer')->get();
return view('survey_details', ['object' => $object, 'data' => $data, 'teams' => $teams, 'members' => $members, 'questions' => $questions, 'checkUserTeam' => $checkUserTeam, 'teamName' => $teamName, 'company' => $company]);
}
To solve your immediate issue, you could add one of these this after $object ... get() depending on your result. One of them should work.
if(empty($object)){ abort(404); }
or
if(!$object->count()){ abort(404); }
However, your code would be much simpler if you used 2 basic laravel technologies.
ORM instead of DB::...
Route model binding. (which would handle your 404 for you)
https://laravel.com/docs/5.6/eloquent
https://laravel.com/docs/5.6/routing#route-model-binding
I have two table that I join in my controller.
Here is my controller :
$data = DB::table('classrooms')
->select('classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
->join('users', 'classrooms.user_id', '=', 'users.id')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')->get();
return View::make('std_classes')->with('data',$data);
In my joinclass table I have user_id. Now I want to view user name and that has in my users table. How can I select in this controller and view my blade page. I try this many time but I can't.
Here is my view:
Teacher: {{$data->name}}
Here, I added few modifications, You should try this in your controller
$data = DB::table('classrooms')
->join('users', 'classrooms.user_id', '=', 'users.id')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')
->select('users.name as name', 'classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
->get();
return view('std_classes',compact('data'));
And use following code on view to display records
#foreach($data as $stud)
{{$stud->name}}
#endforeach
You have to select it first then you could use it as $data->name, so try to add users.name to select :
$data = DB::table('classrooms')
->select('users.name as name', 'classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
->join('users', 'classrooms.user_id', '=', 'users.id')
->join('joinclass', 'classrooms.class_code', '=', 'joinclass.class_code')
->get();
return View::make('std_classes')->with('data',$data);
You could get the image also using :
->select('users.name as name','users.image as image', 'classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
Or get all users attributes in same time using users.* :
->select('users.*', 'classrooms.id as classrooms_id', 'classrooms.*', 'joinclass.*')
Hope this helps.