i have a filter function and if i select the number 3 it should show me also the tests with the number 1 and 2
so i need a multiple "where" who shows me all tests with the number who are less than 3
i tried something like this:
$tests = DB::table('tests')->orderBy('name', 'asc')-where('number', < 3)->get();
it shows me that is not possible but is there a right laravel syntax to do something like this?
i cant find anything about it
because i always use where('number', 1) so like this
Your condition is not correct, so change it to:
where('number', '<=', 3) // '<=' for 1,2,3. If you want only 1,2 try '<'
and there is an issue here:
-where
change it to:
->where
and try again.
You need to use less than mark as a string ('<') as a 2nd param and value as a 3rd param. For example:
$tests = DB::table('tests')->orderBy('name', 'asc')->where('number', '<', 3)->get();
If you use equal to ('=') mark you can use value as a 2nd param.
You can get detail knowledge from below link with multiple examples
https://laravel.com/docs/5.7/queries#where-clauses
$selected_number = '3';
$tests = DB::table('tests')->where('number', '<=', $selected_number)->orderBy('name', 'asc')->get();
Related
I want to get data from database with range like 0 to 50 and 51 to 100, this is possible? if possible please talk me how to do that.
$users = User::all();
I am not able to get clearly what do you want. But I have provided some examples below that might work for you.
$users = DB::table('users')
->whereBetween('id', [1, 50])
->get();
Also try skip and take.
You may use the skip and take methods to limit the number of results returned from the query or to skip a given number of results in the query:
$users = DB::table('users')->skip(50)->take(50)->get();
Alternatively, you may use the limit and offset methods. These methods are functionally equivalent to the take and skip methods, respectively:
$users = DB::table('users')
->offset(50)
->limit(50)
->get();
Hope this helps!
There are a number of options available to you. If you're looking to obtain a working data set of x results at a time and not concerned about the id of the results, then one of the following might be of use:
Pagination
Chunking results
Both of the above will allow you to define the size/number of records returned for your data set.
If you're looking to return records between specific id values, then you'll want to use something like whereBetween on your database queries. The whereBetween clause can be used in conjunction with the pagination and chunking.
i have a database with data and i want to skip/offset the first 3 row.
$data = Data::orderBy('created_at','desc')->skip(3)->paginate(1);
$data = Data::orderBy('created_at','desc')->offset(3)->paginate(1);
both query is returning all result from the start. can anyone help me with this?
Thanks.
skip doesn't seem to work with paginate. What you can do is exclude the row by using whereNotIn.
$data = Data::orderBy('created_at','desc')->whereNotIn('id', [1,2,3])->paginate(1);
If you don't know the id you can query and use the result.
$id = Data::orderBy('created_at','desc')->take(3)->pluck('id');
$data = Data::orderBy('created_at','desc')->whereNotIn('id', $id)->paginate(1);
You can not use paginate() and skip() together. You can do is :
$data = Data::orderBy('created_at','desc')->skip(3)->take(10)->get(); and update these values skip and take values as per your custom implementation.
If you literally want to skip first 3 rows and never ever use them in pagination, you can do :
$dataToEliminate = Data::orderBy('created_at','desc')->take(3)->select('id')->pluck('id');
$data = Data::whereNotIn('id', $dataToEliminate)->orderBy('created_at','desc')->skip(3)->paginate(1);
See documentation for reference.
I have explored the paginate method from the code and came to know
$data = Data::orderBy('created_at','desc')->paginate(1, '*', null, 2);
The 4th parameter, you need to provide the page number (not the offset).
This has me stumped. I'm trying to filter $ages=Ages::all(); based on a productmask field using bitwise operator &
(code should be self-explanatory)
I've tried
#foreach($ages->where('productmask', '&', 2) as $option)
and
#foreach($ages->filter(function($i){return ((int)($i->productmask & 2)); }) as $option)
and
#foreach($ages->filter(function($i){return ((int)($i->productmask & 2) == 2); })->values() as $option)
and none work when productmask = 3 but do work when productmask is exactly 2.
What are my options here (no pun intended)? Why doesn't this work?
I'm pretty sure it would work if I did a \DB::whereRaw (because I can run this against the Db and it works and I get the 2 and 3 entries):
SELECT * from ages WHERE productmask&2
but here it's bypassing fluent and hitting the database inside a view?? Not good form.
Anyone using bit masks out there ever run into this?
thanks in advance.
Well, I don't know why I had to do this exactly, but here's what i did to get it to work:
#foreach($locations->filter(function($i){if (decbin($i->productmask) & 16) return $i; }) as $option)
Basically, I had to use decbin() on the collection's field value ($ages->productmask) so that the compare would resolve correctly.
Works now! Hope this helps someone.
From the source here suggests that
where() takes 3 parameters, name of the column, operator and value to
be compared against.
The operator can be one of the following: '=', '<', '>', '<=', '>=',
'<>', '!=', 'like', 'not like', 'between', 'ilike'
However I found something that you might need to look here.
This will help you to fix your issues.
For further digging see PHP documentation also read this SO post
and as far as your question is concerned
use your query like this
$ages->whereRaw('(productmask & 2)')
$sqLines = SqLines::whereDocumentId($id)->get();
$sqLines->where('item_id','=',0)->count();
item_id is an unsigned integer field
In my development server, it shows result but in my production server with the record exists.
Any reason it happen?
You can use the following
$count = SqLines::where('document_id','=',$id)
->where('item_id','=',0)
->count();
OR
$data = SqLines::where('document_id','=',$id)
->where('item_id','=',0)
->get();
$count = count($data);
With get(), you retrieve the result, so you can't limit it after. Change your code to this:
$sqLines = SqLines::whereDocumentId($id)
->where('item_id','=',0)
->get();
echo $sqlLines->count(); // wil return the number of selected records.
Maybe you will need to change your code. Use one of the following options:
Option 1. Remove the "->get()".
Option 2. Remove the "->count()" and add a loop for check manually the result with a "foreach", "while", etc...
I am trying make select in eloquent:
$query = $query->where($value);
echo $value is:
´column´, ´<´, ´3´
BUT i have error: Unknown column ''column', '=', '3'' in... (look at quotation mark). If i write directly:
$query->where( ´column´, ´<´, ´3´);
Everything is okay
where() wants at least two arguments, but for the way you're using it it will need three. where('column', '<', $value) where $value is 3.
References:
http://laravel.com/docs/queries#advanced-wheres
http://laravel.com/api/source-class-Illuminate.Database.Query.Builder.html#268-324
$user = DB::table('users')->where('name', 'John')->first();
$users = DB::table('users')->where('votes', '>', 100)->get();
Where() required at least two parameters but it will accept three parameters.
Two parameters are compulsory:
table column name
compare value
in your example, you have only passed one parameter and will be consider as a table column name. obviously it will not match with the column name.
However, if you provide 3 parameters, ensure that you place the value parameter as 3rd parameter.
More:
http://laravel.com/docs/queries#selects