Laravel 5.1, Eloquent where zero value - laravel

$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...

Related

Laravel Offset not offsetting and/or Skip not skipping

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).

Remove Limit From Eloquent Query

How can I remove the limit/offset from the below query?
$query = TestModel::where('a', 'b')->limit(100);
$query->removeLimit();
I'm using a query from another module and I don't want to change the code.
You can reset the $limit property:
$query = TestModel::where('a', 'b')->limit(100);
$query->limit = null;
$unlimited = $query->get();
$query->getQuery()->limit = null;
One can reset the limit by passing the null value to the limit method.
$query->limit(null);
Works both with Eloquent\Builder and Query\Builder.
The simple answer to your question is - you cannot. Because you have already filtered the result set to a limit of 100 tuples.
What is the reason for you to avoid the change in code in the Model? Because what #Dhruv has suggested is the correct way to achieve what you want to.
In fact, if you still want to keep the code intact. You can rather define another function in your model this way and use it internally in your old function:
public function newFunction(){
return TestModel::where('a', 'b')->get();
}
public function oldFunction(){
return $this->newFunction()->limit(100);
}
Keeping your code consistent, then use newFunction() in your Controller to do whatever you want to.
get(): To get all record from table use get():
$query = TestModel::where('a', 'b')->get();
limit(): To limit the number of results returned from the query
$query = TestModel::where('a', 'b')->limit(10)->get();

Where with a greater than option in Laravel

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();

Laravel where statement

I am new in Laravel. I want to create a search filter using eloquent model.
I want to check some columns are equal to some enteries and return the results, but the problem is that if any of the enteries is empty it returns nothing.
How to make it to search if other enteries matche and return values even one of the entery is empty.
$result = person::Where('age','>=',$age)
->Where('hairColor','=',$hairColor)
->Where('height','>=',$height)
->get();
Just use orWhere
$result = person::Where('age','>=',$age)
->orWhere('hairColor','=',$hairColor)
->orWhere('height','>=',$height)
->get();
Also, check this documentation
Laravel where clauses

select_max not working in CI

This is not working in my CI app:
$query = $this->db->select_max('order')->get('posts');
print_r($query);
Why is that?
I have a column in my DB called order (int, where the highest value is currently 6) and the table is called posts
Why nothing is outputted instead a number 6 ?
This just runs the query, you need to use ->row() to get the result from it.
$this->db->select_max('order', 'max_order');
$query = $this->db->get('posts');
echo $query->row()->max_order;
DOCS:
https://www.codeigniter.com/userguide2/database/active_record.html
https://www.codeigniter.com/userguide2/database/results.html

Resources