I have problem with my query using laravel - laravel

I am building a simple private chat app with laravel.
I have a messages table with a column : id/sender_id/receiver_id and body
I am trying to show chat history related to user when i select to any user but unfortunately my query is not working properly all users seeing all message please help me how can i resolved that thank u.
Controller
Message::where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id)
->where('sender_id', $userId)
->orWhere('receiver_id', $userId)
->get();

make subquery where like this:
Message::where(function($q){
$q->where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id);
})->where(function($q){
$q->where('sender_id', $userId)
->orWhere('receiver_id', $userId)
})->get();

Just add ->where(function($q){}) case orWhere breakdown all other conditions
try this code
Message::where(function ($q) {
$q->where('sender_id', Auth::user()->id)
->orWhere('receiver_id', Auth::user()->id);
})->where(function ($q) {
$q->where('sender_id', $userId)
->orWhere('receiver_id', $userId);
})->get();

Related

How to Filter Paid or Not Paid Student In laravel

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)
}
})

Laravel Eloquent making select on whereHas()

How can I select on whereHas() some columns in relation.
My current query looks like as it follows
Location::select(['title'])
->withCount(['job' => function($q) {
return $q->where('language_id', 1)->whereNull('deleted_at');
}])->whereHas('country', function ($q) {
return $q->where('id', '=', 80);
})->get();
and I tried
Location::select(['title', 'id as value'])->withCount(['job' => function($q) {
return $q->where('language_id', 1)->whereNull('deleted_at');
}])->whereHas('country', function ($q) {
return $q->select('title', 'iso3')->where('id', '=', 80);
})->get();
but nothing is getting returned on country relation. How do I refactor this query to get it work?
whereHas() doesn't eager load the relation, only filters results based on it. To eager load, use with().
Location::with('country:id,title,iso3')
->select(['title', 'id as value', 'country_id'])
->withCount(['job' => function($q) {
$q->where('language_id', 1)->whereNull('deleted_at');
}])
->whereHas('country', function ($q) {
$q->where('id', '=', 80);
})->get();

How to use where conditional in with Laravel?

I do request using query:
return Baber::where(function ($query) use ($request) {
// HERE CONDITION WHERE FOR USER TABLE
})->with("user")->orderBy('id', 'desc')->get();
How can I use where() inside query, that it will be comparabled with user table in with("user")?
I mean the following:
return Baber::where(function ($query) use ($request) {
$query->where('user.created_at', $request->date);
})->with("user")->orderBy('id', 'desc')->get();
It's known as constraining eager loads.
Baber::with(['user' => function ($query) use ($request) {
$query->where('user.created_at', '=', $request->date);
}])
->orderBy('id', 'desc')
->get();

Laravel: whereHas->WhereHas->Where query. Need to add wherePivot

I am selecting all the ScheduledPrograms from a certain data range that has Attendees that belong to a certain User. I want to add a filter to select only the SchduledPrograms where the pivot field registered=1
I.E. I need to add a wherePivot('registered', 1) for the many-to-many relation scheduledPrograms->attendees. I How do I do this? My mind is scrambled from all the where clauses.
$programs = ScheduledProgram::where('registration_start_date', '<=', $today)
->where('end_date', '>=', $today)
->whereHas('attendees', function($q) use($user)
{
$q->whereHas('user', function($q) use($user){
$q->where('id', $user->id);
});
})->get();
Models
Attendee->belongsTo('User')
->belongsToMany('ScheduledPrograms')
User->hasMany('Attendee')
ScheduledProgram->belongsToMany('Attendee')
`
ScheduledProgram Model
public function attendees()
{
return $this->belongsToMany('Attendee', 'prog_bookings')->withPivot('registered','paid');
}
wherePivot() can be used only for belongsToMany instances, but whereHas() closure function receives Builder instance instead.
So you can't use wherePivot() inside whereHas().
Method wherePivot works with model instances, not with the Model Query Builder.
I think when using whereHas method, the pivot table is already joined by eloquent, so just start using it in the whereHas method:
$programs = ScheduledProgram::where('registration_start_date', '<=', $today)
->where('end_date', '>=', $today)
->whereHas('attendees', function($q) use($user)
{
$q
//========= See this: =========
->where('prog_bookings.registered', '1')
->whereHas('user', function($q) use($user){
$q->where('id', $user->id);
});
})
->wherePivot('registered', 1)
->get();
did you try anything? What error do you get?
If the 'registered' field is in the pivot table between ScheduledPrograms and Atendees relation, you can add the where clause before the get():
$programs = ScheduledProgram::where('registration_start_date', '<=', $today)
->where('end_date', '>=', $today)
->whereHas('attendees', function($q) use($user)
{
$q->whereHas('user', function($q) use($user){
$q->where('id', $user->id);
});
})
->wherePivot('registered', 1)
->get();

Laravel Clear Model Queries

I have this code but I am not sure if is possible be more compact or I´m not sure if this is the way should looks this type of queries in laravel.
public function getRoleTransactionNameByID($id)
{
$role_transaction= $this->model->select(
'roles_transactions.id',
'roles.id as role_id',
'roles.role_name',
'modules.module_name',
'transactions.transaction_name',
'transactions.transaction_description',
'roles_transactions.transaction_action_id',
'transaction_actions.transaction_action_name',
'roles_transactions.created_at',
'roles_transactions.updated_at')
->join('roles', 'roles_transactions.role_id', '=', 'roles.id')
->join('transactions', 'roles_transactions.transaction_id', '=', 'transactions.id')
->join('transaction_actions', 'roles_transactions.transaction_action_id', '=', 'transaction_actions.id' )
->leftjoin('modules','transactions.module_id', '=', 'modules.id')
->where('roles_transactions.id', '=', $id)
->where('roles_transactions.deleted_at','=', null)
->orderby('roles.role_name', 'ASC')
->orderby('modules.module_name', 'ASC')
->orderby('transactions.transaction_name', 'ASC')
->get();
return $role_transaction;
}
Thank you for the help.

Resources