I think this is an easy one, but driving me a bit crazy
I have this:
Transaction::
where('seller_id', $this->id)->
Orwhere('buyer_id', $this->id )->
whereIn('concept', ['Lemonway','Paypal'])->
where('status', '=', 'ok')
->get();
So if I'm clearly specifying status = 'ok'
Why is picking a status = Pending transaction?
You need to use the where() closure for parameter grouping, for example:
Transaction::where(function($q) {
$q->where('seller_id', $this->id)
->orwhere('buyer_id', $this->id)
->whereIn('concept', ['Lemonway', 'Paypal']);
})
->where('status', 'ok')
->get();
Related
here is what I want
check rows with ( user_id AND member_name)
I have try some other code and in this one I got this error
"message":"mb_strpos() expects parameter 1 to be string, object given"
$checkData= DB::table('member_opinions')
->where(DB::table('member_opinions')
->where('user_id',$members->user_id)
)
->orWhere( DB::table('member_opinions')
->where('member_name',$request->{'committees_name'})
)
->first();
/* $checkData= DB::table('member_opinions')
->where('user_id',$members->user_id)
->orWhere('member_name',$request->{'committees_name'})
->first();
*/
How can I do it?
$checkData= DB::table('member_opinions')
->where('user_id',$members->user_id)
->orWhere('member_name',$request->{'committees_name'})
->first();
Let me know did it works.
Update:
Try it out if you want and operation
$checkData= DB::table('member_opinions')
->where('user_id',$members->user_id)
->where('member_name',$request->{'committees_name'})
->first();
That's because of you try to provide object in where() function instead of string.
You have to extract your conditions from inner where() functions and provide them directly to outer where() functions like this:
$checkData = DB::table('member_opinions')
->where('user_id', $members->user_id)
->orWhere('member_name', $request->{'committees_name'})
->first();
After that, pay attention on what you want to get. As I can see, you want to use AND condition, but used orWhere() function, which will build your query with OR condition. So, you should use andWhere() instead:
$checkData = DB::table('member_opinions')
->where('user_id', $members->user_id)
->andWhere('member_name', $request->{'committees_name'})
->first();
And finally, let's make your code less dirty:
$userId = $members->user_id;
$commiteesName = $request->committees_name;
$checkData = DB::table('member_opinions')
->where('user_id', $userId)
->andWhere('member_name', $commiteesName)
->first();
UPD: #user3532758 is right, it is no method called andWhere() in Laravel, you should use where() instead:
$userId = $members->user_id;
$commiteesName = $request->committees_name;
$checkData = DB::table('member_opinions')
->where('user_id', $userId)
->where('member_name', $commiteesName)
->first();
My tables looks like this
area_trip
|id|dispatch_id|trip_id|status|
equipment_trip
|equipment_id|trips_id|dispatch_id|
trips
|id|dispatch_id|status
I am trying to pass collection to my resource. Can someone check my query and tell me what I am doing wrong as following query returning all the data matches dispatch_id whether it matches equipment_id or not. Btw I am new to laravel.
return
Resources::collection(
area_trip::where('dispatch_id', $request->dispatch_id)
->where('status', 1)
->orWhere('status', 9)
->whereHas('equipment_trip', function($query) use ($request) {
$query->where('equipment_trip.equipment_id', '=', $request->equipment_id);
})
->with(['equipment_trip', 'createdBy', 'updatedBy', 'area', 'trips'])
->orderBy('tripStartDate', 'ASC')
->orderBy('status', 'ASC')
->get());
Here is the relationship set up in area_trip model
public function equipment_trip()
{
return $this->belongsTo(equipment_trip::class, 'trip_id', 'trips_id');
}
I believe your whereHas sub query is incorrect also instead of where and orWhere use where in and you can define all statuses necessary, try this:
Resource::collection(area_trip::where('dispatch_id', $request>dispatch_id)
->whereIn('status', [1, 9])
->whereHas('equipment_trip', function($query) use ($request) {
return $query->where('equipment_id', '=', $request->equipment_id);
})
->with(['equipment_trip', 'createdBy', 'updatedBy', 'area', 'trips'])
->orderBy('tripStartDate', 'ASC')
->orderBy('status', 'ASC')
->get());
I'm trying to retrieve a number of results from database using eloquent.
I dont have problem with that.
Example code
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
Now lets suppose I have a variable that if it's true i want to add one more WHERE clause. For example if $domestic == true then i want something like this:
$flights = App\Flight::where('active', 1)
->where('domestic',1)
->orderBy('name', 'desc')
->take(10)
->get();
So I dont like to do, because it's not nice.
if($domestic) {
$flights = App\Flight::where('active', 1)
->where('domestic',1)
->orderBy('name', 'desc')
->take(10)
->get();
} else {
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->take(10)
->get();
}
Ideally i want to pass only the where clause eg.
if(#domestic) { $flights->where('domestic',1) }
But this is not working.
What is the best way to pass additional where clauses whenever needed?
$flights = App\Flight::where('active', 1)
->orderBy('name', 'desc')
->when($domestic, function ($query){
return $query->where('domestic',1)
})
->take(10)
->get();
The laravel query builder has some real gems like the when clause, the when clause will only execute the closure if the first parameter is true (in this case $domestic)
This specific function can be found here. On the same way more of these functions can be found.
Solution is:
$flights = App\Flight::where('active', 1);
if($domestic) {
$flights->where('domestic',1);
}
$results = $flights->orderBy('name', 'desc')
->take(10)
->get();
Use Query Scopes
Write them on your model, quick example on flight model:
public function scopeIsDomestic($query,$domestic){
if($domestic == 1){
return $query->where('domestic',1);
}else{
return $query; //you can return the inverse if you need it -> where domestic <> 1
}
}
And now use it like this
$flights = App\Flight::where('active', 1)
->isDomestic(1)
->orderBy('name', 'desc')
->take(10)
->get();
It provides a nice way to keep the code maintainable since you can update the scope on all your queries at the same time by modifying it on a single place
I have a table called instructor_class: user_id, class_id and I have another table classes: id, time, active.
I would like to show classes for a single user but only those classes that active is 0 or 1.
My current code looks like this:
return InstructorClass::with('classes.session')->where('user_id', '=', $userId)->get();
This code is displaying me everything, then I tried the following code:
$active = 1;
return InstructorClass::with(['classes' => function ($q) use ($active) {
$q->where('active', '=', $active); // '=' is optional
}])
->where('user_id', '=', $userId)
->get();
This again returns me same records, but of course the class property is null for each record, which at some point looks correct, but my point is if the 'active' field does not corresponds at the classes table do not show the record, seems like the where() stm within with() is optional..
I am kinda stuck here...
Would appreciate your help, opinions!
You can use ::has('classes') to only return the models that have related classes
return InstructorClass::has('classes')->with(['classes' => function ($q) use ($active) {
$q->where('active', $active);
}])
->where('user_id', '=', $userId)
->get();
Never thought it could be this simple:
return InstructorClass::with('classes.session')
->join('classes', 'classes.id', '=', 'instructor_class.class_id')
->where('classes.active', '=', 1)
->where('user_id', '=', $userId)
->get();
I'm trying to select specific columns from two tables however when I add the ->select() method into my query, I get an error.
If I leave out the ->select() method, I get a valid resultset and everything works, but adding the select breaks it. Sadly the error reported has nothing to do with the query and is useless to me.
Here is the code that works:
$notifications = DB::table('notifications')
->join('notifications_pivot', function($join)
{
$join->on('notifications.id', '=', 'notifications_pivot.notification_id')
->where('notifications_pivot.user_id', '=', Session::get('id'))
->where('notifications_pivot.is_read', '=', 'N');
})
->get();
Now here's the code that breaks:
$notifications = DB::table('notifications')
->join('notifications_pivot', function($join)
{
$join->on('notifications.id', '=', 'notifications_pivot.notification_id')
->where('notifications_pivot.user_id', '=', Session::get('id'))
->where('notifications_pivot.is_read', '=', 'N');
})
->select(DB::raw('notifications.id, notifications.subject, notifications.message, notifications.url,
notifications.start_date, notifications.end_date, notifications.access_role_id, notifications_pivot.id,
notifcations_pivot.notification_id, notifications_pivot.user_id, notifications_pivot.is_read'))
->get();
It's times like these when I wish I could just write straight SQL and parse the query!
Any suggestions?
Take out the DB::raw() and just pass the fields you want as parameters.
If that doesn't work, the Laravel log at app/storage/logs/laravel.log may provide more insight.
$notifications = DB::table('notifications')
->join('notifications_pivot', function($join)
{
$join->on('notifications.id', '=', 'notifications_pivot.notification_id')
->where('notifications_pivot.user_id', '=', Session::get('id'))
->where('notifications_pivot.is_read', '=', 'N');
})
->select('notifications.id', 'notifications.subject', 'notifications.message', 'notifications.url', 'notifications.start_date', 'notifications.end_date', 'notifications.access_role_id', 'notifications_pivot.id', 'notifcations_pivot.notification_id', 'notifications_pivot.user_id', 'notifications_pivot.is_read')
->get();