I use slim 3 for microservice for that i install laravel eloquent and i write a laravel query like following
use Illuminate\Database\Capsule\Manager as DB;
DB::enableQueryLog();
$faqs = DB::table('rental_faq')
->where('id', '!=', '')
->where(function ($query) {
$query->where('isActive', '=', 1)
->orWhere('isDelete', '=', 'no');
})
->get();
then the problem is it shows a error like following
"
There was an error parsing JSON data
Unexpected token I in JSON at position 743
"
I want to perform and_where and or_where operation separated parenthesis
$maincat=25;
$subcat=30;
$faqs=DB::table('rental_faq')
->where('isActive',1)
->where('isDelete','no')
->where(function ($query) use($maincat,$subcat) {
$query->where('main_category',$maincat)
->whereRaw('FIND_IN_SET(?,sub_category)',[$subcat]);
})
->get();
Try with MySQL "NOT" function like so:
$faqs = DB::table('rental_faq')
->where('id', '<>', '')
->where(function ($query) {
$query->where('isActive', 1)
->orWhere('isDelete', 'no');
})
->get();
dd($faqs);
Let me know if it solves your issue.
Related
I need to lookin' for data that the where clause have 3 condition, so its need OR operator twice, here is the code in my controller, I use laravel 8
$salesTransactions = SalesTransaction::when($request->keyword, function ($query) use ($request) {
$query
->where('tanggal_transaksi', 'like', "%{$request->keyword}%")
->orWhere('name', 'like', "%{$request->keyword}%")
->orWhere('status', 'like', "%{$request->keyword}%");
})->join('distributors', 'distributors.id', '=', 'sales_transactions.distributor_id')
->join('users', 'users.id', '=', 'distributors.user_id')
->select('sales_transactions.*', 'users.name')
->orderBy('tanggal_transaksi', 'DESC')->latest('sales_transactions.id')->paginate(25);
I got the error, but dont know how to solve, the problem is I cant use OR operator twice, any idea ? Thanks
If you need to group an "or" condition within parentheses, you may pass a closure as the first argument to the orWhere method. Like:
$users = DB::table('users')
->where('votes', '>', 100)
->orWhere(function($query) {
$query->where('name', 'Abigail')
->where('votes', '>', 50);
})
->get();
I would like to add a parameter to my Eloquent relationship in the with:: static method.
I have a Dealer Model that has those relationships
public function events()
{
return $this->hasMany('App\Event', 'dealer_id', 'dealer_id');
}
public function recentEvents($interval = 14)
{
return $this->events()
->whereDate('datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('dateend', '>=', Carbon::now('America/Toronto'));
}
Everything works fine if I call it like this $dealer->recentEvents(20) but in my DealerController
I would like to do something in the line of this
Dealer::with("recentEvents")
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();
Where i would pass a paremeter to "recentEvents".
I've tried putting a closure function to with like this
Dealer::with(["recentEvents" => function ($query) use ($interval){
//The problem here is that I would need to put the recent-events code like this
$query->whereDate('cal_datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('cal_dateend', '>=', Carbon::now('America/Toronto'));
}])
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();
But it would defeat the purpose of having a recentEvents relationship.
Is it possible to just pass a parameter ?
I've tried using Dynamic Scope from Laravel doc but I figured it was not what I was looking for.
i think u should use like this
Dealer::with(["events",function($q) use($interval){
$q->whereDate('datestart', '>=', Carbon::now('America/Toronto')->subDays($interval))
->whereDate('dateend', '>=', Carbon::now('America/Toronto'));
}])
->where('dealer_flag', 1)
->orderBy('dealer_storename', 'ASC')
->get();
select properties.address, properties.unit_type, rents.date, rents.amount, lease_requests.security_deposit
from properties
left join rents on rents.property_id = properties.id
left join lease_requests on lease_requests.property_id = properties.id
where (rents.status = 'paid' and rents.date between '2019-01-07' and '2019-02-07')
or (lease_requests.security_deposit_status = 'paid' and lease_requests.move_in between '2019-01-07' and '2019-02-07')
How can I rewrite this sql query in Laravel, but not by using raw sql, but with combining ->join, ->where, ->orWhere? I know that I should use function within one of these, but I am not sure within which one would that be. Does all of those functions accept function as second parameter?
EDIT
Here is the code which I have written in order to accomplish this, but it's not retrieving exactly the same data as the sql code I have written up there, because I am not sure how to implement query with brackets using Laravel:
Property::join('rents', 'properties.id', '=', 'rents.property_id')
->leftJoin('lease_requests', 'properties.id', '=', 'lease_requests.property_id')
->where('properties.address', $property['address'])
->whereBetween('move_in', [$from,$to])
->whereBetween('date', [$from,$to])
->where('rents.status', 'paid')
->orWhere('security_deposit_status', 'paid')
->get(['properties.address', 'properties.unit_type', 'rents.date', 'rents.amount', 'lease_requests.move_in', 'lease_requests.security_deposit'])->toArray();
$status = 'paid'; $dateStart = '2019-01-07'; $dateEnd = '2019-02-07';
DB::table('properties')
->selectRaw('properties.address, properties.unit_type, rents.date, rents.amount, lease_requests.security_deposit')
->leftJoin('rents', 'rents.property_id', '=', 'properties.id')
->leftJoin('lease_requests', 'lease_requests.property_id', '=', 'properties.id')
->where(function ($query) use ($status, $dateStart, $dateEnd) {
$query->where('rents.status', $status)->whereBetween('rents.date', [$dateStart, $dateEnd]);
})
->orWhere(function ($query) use ($status, $dateStart, $dateEnd) {
$query->where('lease_requests.security_deposit_status', $status)->whereBetween('lease_requests.move_in', [$dateStart, $dateEnd]);
})->get();
you must provide a function to get the parenthesis ;)
EDIT with your code :
Property::leftJoin('rents', 'properties.id', '=', 'rents.property_id')
->leftJoin('lease_requests', 'properties.id', '=', 'lease_requests.property_id')
->where(function ($query) use ($status, $dateStart, $dateEnd) {
$query
->where('rents.status', $status)
->whereBetween('rents.date', [$dateStart, $dateEnd]);
})
->orWhere(function ($query) use ($status, $dateStart, $dateEnd) {
$query
->where('lease_requests.security_deposit_status', $status)
->whereBetween('lease_requests.move_in', [$dateStart, $dateEnd]);
})
->get([
'properties.address', 'properties.unit_type',
'rents.date', 'rents.amount', 'lease_requests.move_in',
'lease_requests.security_deposit'
])->toArray();
I want to run this query but with laravel query builder I'm not getting the exactly results using orWhere and where clause
SELECT * FROM lectures WHERE school=6 AND ( (period=3 AND class_section=3) OR (period=4 AND teacher=17) )
so how can I do it with laravel query builder can anyone help me out?
You need to use Parameter Grouping to translate your query to query builder form
DB::table('lectures)
->where('school', '=', 6)
->where(function ($query) {
$query->where(function ($query) {
$query->where('period', '=', 3)
->where('class_section', '=', 3)
})->orWhere(function ($query) {
$query->where('period', '=', 4)
->where('class_section', '=', 17)
});
})
->get();
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();