codeigniter, get the maximum value in mysql table column - codeigniter

i'm using codeigniter 2.
i've a mysql table column storing the time taken for each student.
eg. 1.2327, 0.6547, 1.9876
i want to get the max. value that column.
This is my code:
$this->db->select_max('time_taken', 'time');
$result = $this->db->get('students');
echo $result->row()->time;
when i echo the result, it give me a value of 2(correct value should be 1.9876).
What is the correct way to get this value i need, thanks?

Try:
$this->db->select_max('time_taken AS time');
$result = $this->db->get('students')->row();
echo $result->time;
Edit: Make sure your database table field (i.e time_taken) is decimal, NOT integer.

$this->db->select_max('time_taken');
$this->db->from('students');
$query=$this->db->get();
return $query->result_array();

Related

get one row of dummy data with eloquent

Take this example
$result = Players::select("first_name", "last_name")->where("some_field", "some_value");
That returns a Illuminate\Database\Eloquent\Builder
Now I would like to "simulate" that same thing but with dummy data, so I did this
$result = Players::select(DB::raw("'john', 'doe'"));
That works, but when the query runs, if the the players table has 10 rows I get 10 rows filled with john doe and I only need just one.
I've tried doing
$result = Players::select(DB::raw("'john', 'doe'"))->limit(1);
but that has no effect
How can I get only 1 row of dummy data? Taking into account that $result must return a Illuminate\Database\Eloquent\Builder
if you want to do it in your suggested way i think this should work :
$result = Players::select(DB::raw("'john' as first_name , 'doe' as last_name"))->take(1);

Find max value of a column in laravel

The problem started because I have a table (Clientes), in which the primary key is not auto-incremental. I want to select the max value stored in a column database.
Like this select, but with eloquent ORM (Laravel):
SELECT MAX(Id) FROM Clientes
How can I do this?
I tried:
Cliente::with('id')->max(id);
Cliente::select('id')->max(id);
I prefer not to make a simple raw SELECT MAX(ID) FROM Clientes
I cannot make it.
Thanks all!
The correct syntax is:
Cliente::max('id')
https://laravel.com/docs/5.5/queries#aggregates
Laravel makes this very easy, in your case you would use
$maxValue = Cliente::max('id');
But you can also retrieve the newest record from the table, which will be the highest value as well
$newestCliente = Cliente::orderBy('id', 'desc')->first(); // gets the whole row
$maxValue = $newestCliente->id;
or for just the value
$maxValue = Cliente::orderBy('id', 'desc')->value('id'); // gets only the id
Or, if you have a created_at column with the date you could get the value like this
$maxValue = Cliente::latest()->value('id');
Relevant Laravel Documentation: https://laravel.com/docs/5.5/queries#aggregates
$maxValue = DB::table('Clientes')->max('id');
Cliente::where('column_name', $your_Valu)->max('id') // You get any max column
We can use the following code :
$min_id = DB::table('table_name')->max('id');
https://laravel.com/docs/8.x/queries#aggregates

Laravel Eloquent and SUM() - Do not consider the decimal

I run in a strange issue, I need to sum a column using eloquent but every function I use eloquent do not consider the decimal.
First of all I have the column set ad DECIMAL(8,2) and in my DB the data is stored in the correct way.
If I just use a simple query:
select sum(total_after_tax) from invoice_details where invoice_id = 1
I get the result: 312.93
Now if I use eloquent:
$invoice = Invoice::with(['details'])->get();
dd($invoice->first()->details->sum('total_after_tax'));
I get: 312.
How is this possibile? How can I get the correct value form DB?

How to get unique year values from created_at?

I have a collection called $products, each instance of which has a field created_at. The latter obviously has a format ('Y-m-d H:i:s') in the DB. Now, it's easy to get instances with unique created_at. However, I'd like to retrieve unique year values of created_at in one single expression (that I can write in my view). What I am looking for is:
$products->unique( year value ('Y' ONLY) of 'created_at' )
This expression should evaluate to something like this: ['2012', '2013', '2016'].
Building on the comment that gives you a collection with unique timestamps, you can map out the year and sort with the following code.
$years = $products->unique(function($item){
return $item['created_at']->year;
})->map(function($item){
return $item['created_at']->year;
})->sort()->toArray();
In addition to Tim result
$products = DB::table('products')->get();
$years = $products->unique(function($item){
return $item['created_at']->year;
})->map(function($item){
return $item['created_at']->year;
})->sort()->toArray();
You can use toArray or values() to get the result of collection

Laravel 5.1, Eloquent where zero value

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

Resources