I am try to do is check if the booking is b/n request check_in and check_out date. if so exclude the booking with booked status =1.
$room->bookings()->where(function ($query) use ($check_in, $check_out, $statusCollect) {
$query->where('check_in','<=',$check_out)
->where('check_out','>=',$check_in)
->whereNotIn('book_status_id', $statusCollect)
});
but the problem is this query filter all inside table. how I can I use if statement in whereNotIn() or is there is other method.
what I need is if the check_in<=$check_in and $check_out >=check_out then give me booking table row then check the bookstatus, if it pass get me the row.
I hope I don't make it complicated:).
thanks
//controller
//first get data if data get then update the status
$data=$room->bookings()->where(function ($query) use ($check_in, $check_out) {
$query->where('check_in','<=',$check_out)
->where('check_out','>=',$check_in)
});
if(!empty($data) && $data!=''){
//you can use your table primary id to update the status
$modelObj=new model_name();
$updateArray=$modelObj->updatedById($data->id[
'book_status_id'=>statusCollect
]);
}
//model
function updatedById($id,$statusCollect){
return $this->where('id',$id)->update($statusCollect);
}
Related
I am using Laravel 9, I stuck into the Relationship, I have a table called test_series_question another is test_series_responses. Every test_series_question has one row in test_series_responses. I need to select the responses available in the table. Something like WHERE question_id = '$id' AND test_id = $test_id
I have tried to to get the responses from Laravel Model
function response()
{
return $this->hasOne(TestSeriesResponse::class, 'question_id', 'id');
}
It's working fine, But There is a one column named test_id and if I dont compare test_id It'll return previously submitted responses which match the question_id id not the test_id
I have two questions
Can I put a parameter into function response() in Model from controller?
How can I achive the thing, Please help me out.
Here is the Controller code:
function testOngoing($test_id, $history_id)
{
$test = TestSeries::find($test_id);
$history = $history_id;
$questions = TestSeriesQuestion::where('test_id', $test_id)->with('response')->paginate(1);
$data = compact('test', 'questions', 'history');
return view('test-series/test-question')->with($data);
}
Maybe something like
$questions = TestSeriesQuestion::with(['response' => function ($query) use ($history_id) {
$query->where('history_id', $history_id);
}])->where('test_id', $test_id)->paginate(1)
I have found this: Get Specific Columns Using “With()” Function in Laravel Eloquent
but nothing from there did not help.
I have users table, columns: id , name , supplier_id. Table suppliers with columns: id, name.
When I call relation from Model or use eager constraints, relation is empty. When I comment(remove) constraint select(['id']) - results are present, but with all users fields.
$query = Supplier::with(['test_staff_id_only' => function ($query) {
//$query->where('id',8); // works only for testing https://laravel.com/docs/6.x/eloquent-relationships#constraining-eager-loads
// option 1
$query->select(['id']); // not working , no results in // "test_staff_id_only": []
// option 2
//$query->raw('select id from users'); // results with all fields from users table
}])->first();
return $query;
In Supplier model:
public function test_staff_id_only(){
return $this->hasMany(User::class,'supplier_id','id')
//option 3 - if enabled, no results in this relation
->select(['id']);// also tried: ->selectRaw('users.id as uid from users') and ->select('users.id')
}
How can I select only id from users?
in you relation remove select(['id'])
public function test_staff_id_only(){
return $this->hasMany(User::class,'supplier_id','id');
}
now in your code:
$query = Supplier::with(['test_staff_id_only:id,supplier_id'])->first();
There's a pretty simple answer actually. Define your relationship as:
public function users(){
return $this->hasMany(User::class, 'supplier_id', 'id');
}
Now, if you call Supplier::with('users')->get(), you'll get a list of all suppliers with their users, which is close, but a bit bloated. To limit the columns returned in the relationship, use the : modifier:
$suppliersWithUserIds = Supplier::with('users:id')->get();
Now, you will have a list of Supplier models, and each $supplier->users value will only contain the ID.
How can I retrieve all records of my model based on certain ID's in my pivot table?
I have the following 3 tables
users;
id,
name
stats;
id,
name
stats_selected;
user_id,
stats_id
Model
User.php
public function stats()
{
return $this->belongsToMany('App\stats', 'stats_selected', 'user_id', 'stats_id')->withTimestamps();
}
Controller
// Get all users with example of stats ID's
$aFilterWithStatsIDs = [1,10,13];
$oUser = User::with(['stats' => function ($query) use($aFilterWithStatsIDs ) {
$query->whereIn('stats_id', $aFilterWithStatsIDs);
}])
->orderBy('name', 'desc')
->get()
This outputs just all the users. Btw, fetching users with there stats and saving those selected stats into the DB is not a problem. That works fine with the above lines.
But how do I retrieve only the users which has certain stats_id's within them?
But how do I retrieve only the users which has certain stats_id's within them?
Use a whereHas conditional.
User::whereHas('stats', function ($stats) use ($aFilterWithStatsIDs) {
$stats->whereIn('id', $aFilterWithStatsIDs);
});
I have a eloquent model called product. I need to use where condition depends on the status of column in same table. This is my code
public function product_list(Request $request,Datatables $datatables){
$product = Product::where('supplier_id',$request->supplier_id)->where('product','!=',3);
return $datatables->eloquent($product)->filter(function ($query) use ($request) {
if($query->is_from_portalsite==1){
$query->where('status','>',1);
}
})->make(true);
}
I need to check the where condition if the column is_from_portalsite equals to 1. how can I check that in laravel.
Try this (assuming is_from_portalsite is boolean):
$query->where('is_from_portalsite',0)->orWhere('status','>',1);
I have a model class ServiceAgentRel. Where agent_id belongs to user table. I am fetching all column with service_id 3.
$agent_data = App\ServiceAgentRel::where(['service_id'=>3])->with('agent')->get();
Using this code I can get all services with service_id 3 But I need to set a condition on user table if user status (status corresponding to that agent_id) is inactive no row will be fetched.
My main requirement is, I have to set a condition on a relation function agent(). Also is there any way to check it from controller?
class ServiceAgentRel extends Model {
public function agent(){
return $this->belongsTo('App\User', 'agent_id', 'id');
}
}
I know it is possible to do this task by a simple join, but I want to know if it is possible by eloquent model function.
Thanks
enter image description here
for that you need to use whereHas,like that
$agent_data = App\ServiceAgentRel::where(['service_id'=>3])->with('agent')
->whereHas('agent',function($query) {
$query->where('status', 'active');
})->get();
Hi you can do like this
$agent_data = App\ServiceAgentRel::where(['service_id'=>3])->first();
echo $agent_data->agent->name;
You can use this statement:
$agent_data = App\ServiceAgentRel::where(['service_id'=>3])
->with(['agent' => function($query) {
$query->where('agent.status', 'active');
}])->get();