Laravel using pass and accept limit request
I am trying to create a query that uses the skip () and take () functions, for some reason it keeps adding offset 0
to the end but it should beLIMIT 0,0
code:
$dbSearch = new SchoolInfo;
$dbSearch = $dbSearch->where(function($query) use($search) {
$query->where('school_name', 'LIKE', '%'.$search.'%')
->orWhere('address_1', 'LIKE', '%'.$search.'%')
->orWhere('address_2', 'LIKE', '%'.$search.'%')
->orWhere('address_3', 'LIKE', '%'.$search.'%')
->orWhere('address_4', 'LIKE', '%'.$search.'%')
->orWhere('county', 'LIKE', '%'.$search.'%')
->orWhere('postcode', 'LIKE', '%'.$search.'%')
->orWhere('head_teacher_email', 'LIKE', '%'.$search.'%')
->orWhere('head_teacher_first', 'LIKE', '%'.$search.'%')
->orWhere('head_teacher_last', 'LIKE', '%'.$search.'%');
});
$results = $dbSearch
->skip($startat)
->take($startat)
->orderBy('school_name', 'ASC')
->get();
And this is the error I am getting
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'offset 0' at line 1 (SQL: select * from `school_info` where (`school_name` LIKE %ok% or `address_1` LIKE %ok% or `address_2` LIKE %ok% or `address_3` LIKE %ok% or `address_4` LIKE %ok% or `county` LIKE %ok% or `postcode` LIKE %ok% or `head_teacher_email` LIKE %ok% or `head_teacher_first` LIKE %ok% or `head_teacher_last` LIKE %ok%) order by `school_name` asc offset 0)","file":"\/var\/www\/html\/globalrecruit\/vendor\/laravel\/framework\/src\/Illuminate\/Database\/Connection.php
source to share
When you execute skip(n)
, it adds offset n
to the request as you noticed. And when you do take(n)
, it adds an n constraint to the request. The limit then overrides the offset when building the actual query.
But take(0)
, you say you want to return zero results. The eloquent thinks that you do not want to apply the limit, and no. Instead, only the offset remains and is not a valid request in and of itself.
Make sure the value is take()
greater than zero and you should be fine. :)
(Note: this is not entirely true for unified queries, but it is quite different.)
source to share