Laravel. I can't read the data from the column - php

I have a problem.
Needs to get the first date of creation of data in the table by the currently logged in user.
Working example:
$howMuchIPlay = Game::all('user_id')->where('user_id', '=', Auth::user()->id)->count();
Not working example:
$firstVoteInGame = Game::all('created_at')->first()->where('user_id','=', Auth::user()->id);
When I use count() function I get value 38. This is all the rows for the user, but I want to get only the earliest (first). How can I do this?
Sorry for my english and thank you in advance for help.

$firstVoteInGame = Game::where('user_id', '=', Auth::user()->id)->oldest()->first();
This will get the earliest (oldest) record from the current authenticated user.

You got the order wrong.
You should first limit by user Id.
Then order by created at in descending order so oldest is on top. Or if you want the newest, order by 'desc' instead of 'asc'
Then get the first.
Game::where('user_id','=', Auth::user()->id)->orderBy('created_at','asc')->first();
What you did was:
Get all records from database in a collection.
get first possible record from collection at index 0
limit a model object by a where query(it would fail here since you were in a model object, not a collection or query builder instance);

Your not working example, $firstVoteInGame = Game::all('created_at')->first()->where('user_id','=', Auth::user()->id);, starts off with Game::all('created_at').
What this means is that all Games are returned, but these are limited to the 'created_at' column only (see the documentation here).
Since you're limiting your result to the 'created_at' column, Eloquent will now not be able to compare the 'user_id' columns to the authenticated user id.
In addition, even if you were to select all columns (by performing Game::all()), you are calling ->first() after which you only have the first row remaining.
You say you want to
get the first date of creation of data in the table by the currently logged in user.
This would thus by done by:
$firstVoteInGame = Game::where('user_id', Auth::user()->id)->orderBy('created_at','asc')->first('created_at');

Why you use 'created_at' for ordering as easy/correct way is use primary key, Because you can just do:
$firstVoteInGame = Game::where('user_id','=', Auth::user()->id)->orderBy('id', 'desc')->first();
You can try, if you still want 'created_at':
$firstVoteInGame = Game::where('user_id','=', Auth::user()->id)->orderBy('created_at', 'desc')->first();
TIP
- The Eloquent all method will return all of the results in the model's table so avoid to use it with first method

Related

How to get latest record in Laravel 9 [duplicate]

I would like to retrieve the last file inserted into my table. I know that the method first() exists and provides you with the first file in the table but I don't know how to get the last insert.
You'll need to order by the same field you're ordering by now, but descending.
As an example, if you have a time stamp when the upload was done called upload_time, you'd do something like this;
For Pre-Laravel 4
return DB::table('files')->order_by('upload_time', 'desc')->first();
For Laravel 4 and onwards
return DB::table('files')->orderBy('upload_time', 'desc')->first();
For Laravel 5.7 and onwards
return DB::table('files')->latest('upload_time')->first();
This will order the rows in the files table by upload time, descending order, and take the first one. This will be the latest uploaded file.
Use the latest scope provided by Laravel out of the box.
Model::latest()->first();
That way you're not retrieving all the records. A nicer shortcut to orderBy.
You never mentioned whether you are using Eloquent, Laravel's default ORM or not. In case you are, let's say you want to get the latest entry of a User table, by created_at, you probably could do as follow:
User::orderBy('created_at', 'desc')->first();
First it orders users by created_at field, descendingly, and then it takes the first record of the result.
That will return you an instance of the User object, not a collection. Of course, to make use of this alternative, you got to have a User model, extending Eloquent class. This may sound a bit confusing, but it's really easy to get started and ORM can be really helpful.
For more information, check out the official documentation which is pretty rich and well detailed.
To get last record details
Model::all()->last(); or
Model::orderBy('id', 'desc')->first();
To get last record id
Model::all()->last()->id; or
Model::orderBy('id', 'desc')->first()->id;
Many answers and some where I don't quite agree. So I will summarise again with my comments.
In case you have just created a new object.
By default, when you create a new object, Laravel returns the new object.
$lastCreatedModel = $model->create($dataArray);
dd($lastCreatedModel); // will output the new output
echo $lastCreatedModel->key; // will output the value from the last created Object
Then there is the approach to combine the methods all() with (last()and first()) without a condition.
Very bad! Don't do that!
Model::get()->last();` // the most recent entry
Model::all()->last();` // the most recent entry
Model::get()->first();` // the oldest entry
Model::all()->first();` // the oldest entry
Which is basically the wrong approach! You get() all() the records, and in some cases that can be 200,000 or more, and then pick out just one row. Not good! Imagine your site is getting traffic from Facebook and then a query like that. In one month that would probably mean the CO² emissions of a city like Paris in a year. Because the servers have to work unnecessarily hard. So forget this approach and if you find it in your code, replace it/rewrite it. Maybe you don't notice it with 100 data sets but with 1000 and more it can be noticeable.
Very good would be:
Model::orderBy('id', 'desc')->last(); // the most recent record
Model::latest('id')->first(); // the most recent record
Model::latest('id')->limit(1)->get(); // the most recent record
Model::orderBy('id', 'desc')->limit(1)->get(); // the most recent entry
Model::orderBy('id', 'desc')->first(); // the most recent entry
Model::orderBy('id', 'asc')->first(); // the oldest entry
Model::orderBy('id', 'asc')->limit(1)->get(); // the oldest entry
Model::orderBy('id', 'asc')->first(); // the oldest entry
If orderBy is used in this context, the primarykey should always be used as a basis and not create_at.
Laravel collections has method last
Model::all() -> last(); // last element
Model::all() -> last() -> pluck('name'); // extract value from name field.
This is the best way to do it.
You can use the latest scope provided by Laravel with the field you would like to filter, let's say it'll be ordered by ID, then:
Model::latest('id')->first();
So in this way, you can avoid ordering by created_at field by default at Laravel.
Try this :
Model::latest()->get();
Don't use Model::latest()->first(); because if your collection has multiple rows created at the same timestamp (this will happen when you use database transaction DB::beginTransaction(); and DB::commit()) then the first row of the collection will be returned and obviously this will not be the last row.
Suppose row with id 11, 12, 13 are created using transaction then all of them will have the same timestamp so what you will get by Model::latest()->first(); is the row with id: 11.
To get the last record details, use the code below:
Model::where('field', 'value')->get()->last()
Another fancy way to do it in Laravel 6.x (Unsure but must work for 5.x aswell) :
DB::table('your_table')->get()->last();
You can access fields too :
DB::table('your_table')->get()->last()->id;
Honestly this was SO frustrating I almost had to go through the entire collection of answers here to find out that most of them weren't doing what I wanted. In fact I only wanted to display to the browser the following:
The last row ever created on my table
Just 1 resource
I wasn't looking to ordering a set of resources and order that list through in a descending fashion, the below line of code was what worked for me on a Laravel 8 project.
Model::latest()->limit(1)->get();
Use Model::where('user_id', $user_id)->latest()->get()->first();
it will return only one record, if not find, it will return null.
Hope this will help.
Model($where)->get()->last()->id
For laravel 8:
Model::orderBy('id', 'desc')->withTrashed()->take(1)->first()->id
The resulting sql query:
Model::orderBy('id', 'desc')->withTrashed()->take(1)->toSql()
select * from "timetables" order by "id" desc limit 1
If you are looking for the actual row that you just inserted with Laravel 3 and 4 when you perform a save or create action on a new model like:
$user->save();
-or-
$user = User::create(array('email' => 'example#gmail.com'));
then the inserted model instance will be returned and can be used for further action such as redirecting to the profile page of the user just created.
Looking for the last inserted record works on low volume system will work almost all of the time but if you ever have to inserts go in at the same time you can end up querying to find the wrong record. This can really become a problem in a transactional system where multiple tables need updated.
Somehow all the above doesn't seem to work for me in laravel 5.3,
so i solved my own problem using:
Model::where('user_id', '=', $user_id)->orderBy('created_at', 'desc')->get();
hope am able to bail someone out.
be aware that last(), latest() are not deterministic if you are looking for a sequential or event/ordered record. The last/recent records can have the exact same created_at timestamp, and which you get back is not deterministic. So do orderBy(id|foo)->first(). Other ideas/suggestions on how to be deterministic are welcome.
You just need to retrive data and reverse them you will get your desire record let i explain code for laravel 9
return DB::table('files')->orderBy('upload_time', 'desc')->first();
and if you want no. of x last result
return DB::table('files')->orderBy('upload_time', 'desc')->limit(x)->get();
If the table has date field, this(User::orderBy('created_at', 'desc')->first();) is the best solution, I think.
But there is no date field, Model ::orderBy('id', 'desc')->first()->id; is the best solution, I am sure.
you can use this functions using eloquent :
Model::latest()->take(1)->get();
With pdo we can get the last inserted id in the docs
PDO lastInserted
Process
use Illuminate\Support\Facades\DB;
// ...
$pdo = DB::getPdo();
$id = $pdo->lastInsertId();
echo $id;

Get a user with posts

I am trying to get a user with his posts in laravel, so far I have tried to use the following line
User::findOrFail($user_id)->with('posts')->first();
But I am getting the first user on the table regardless of what the user ID specified is.
I have tried to dd the user_id and it's working fine, (getting the user_id from the route).
So far the result I am getting is if the user id is x and the first user in the table has an id of 1 I get the info of user id 1 and his posts.
Thanks in advance!
You have your methods in the wrong order.
findOrFail executes the query immediately, which returns the User record for $user_id.
Chaining that to ->with() will start a new query.
Finally, calling ->first() returns the first User from the database.
Adjust your query as such:
User::with('posts')->findOrFail($user_id);
findOrFailand first will give you the User Object but you have to decide witch function you will use.
If you use Routemodel Binding then you can use:
User::with('posts')->first(); or User::load('posts');
If you dont use routemodel Binding you can use findOrFail like that:
User::with('posts')->findOrFail($user_id);

how to perform this join in laravel

I have a table in my database called JobCardOps and a model JobCardOp.php. Fields of this table include OpNum, JobCardNum, PrevOpNum, OpStatus. An row is uniquely identified by the JobCardNum and OpNum pair. Now, I considered having another field called PreviousOpStatus which contains the status of the previous Op, by then I wondered whether I could do this with Join.
So to clarify, When I retrieve a group of JobCardOp models from the database I want to add another field on the end; 'PreviousOpStatus'.
I know ill need something like;
$Ops = JobCardOp::where('JobCardNum', '=', $someJobNum)
->join('JobCardOps as JobCardOps_1, some constraint)
->select('JobCardOps.*', 'JobCardOps_1.OpStatus as PreviousOpStatus')
->get();
Where JobCardOps_1 is somehow offset by 1 if that makes sense. Can anyone help if this can be done?
Thanks

i Want to count all my repeated value in mysql but i get this

I want to count the number of books people take from library i am working in laravel 5 with mysql and here is the code for counting book number
$books= DB::table('libraries')->where('userid', '=', $request->input('userid'))
->select(DB::raw('count(*) as number'))
->groupBy('userid')->get();
i am getting this result
[{"number":24}]
but i only need the number which 24?
i tried to get it by using associated array but wont work
$books['number'] how can i get the number and is it array
Laravel doesn't return an associative array, it returns an array of objects instead (json_decode is not needed)
So you should be able to access your value $books[0]->number as mentioned by others.
But the main problem is your query. Because the user id is in your where clause, it is redundant to group by user id, which means you can eliminate the group by and simply use laravel's count function to get the # of rows that have a specific user id:
$count = DB::table('libraries')->where('userid', '=', $request->input('userid'))->count();

Cakephp - get database data from not associated models

I wanted to ask, how can i get data from one table and use this in other find.
For example, i have films table.
I want to get highest rated 3 films. Result should return 3 ID's.
Now, i want to create other query from not associated table, and pass this 3 ID's as "conditions" to find data in other table.
I dont want to use associations, because, data is stored in many databases, and this is problematic.
Thank You.
Once you've got your film IDs you can use in to filter the results from your other Model:-
$filmIds = ['32','55','75'];
$query = TableRegistry::get('Model')->find()
->where(function ($exp, $q) use ($filmIds) {
return $exp->in('film_id', $filmIds);
});
// WHERE film_id IN ('32','55','75')
Check out the docs section on advanced conditions.
If you need to get your film IDs into the correct format (i.e. that shown in the example code) you can use Hash::extract() on the results from your previous query.
if your cakephp version 3.x you can use subqueries in fairly intuitive way
$films = TableRegistry::get('Films')->find('highestRated')
->select(['id'])
->limlt(3);
$query = $related->find()
->where(['id' => $films]);
Subqueries are accepted anywhere a query expression can be used. For example, in the select() and join() methods. http://book.cakephp.org/3.0/en/orm/query-builder.html#subqueries

Categories