0
I’ve always used Eloquent very well with static methods. Yesterday, when creating a new concept of session registration for a new project, I came across a question that I could not clarify.
I have a "Session" template that points to a table with the same name in the database. It turns out that the moment the user passes the token, an object of the class "Sessao" is created and its constructor tries to fetch the record related to that token in the database. Usually I would use:
$sessao = Sessao::where('token',Input::get('token'))->get();
Or something like that:
$this->where('token',Input::get('token'))->get();
But in this case, I need to load the result into the object that performed the query itself. Something like this:
$this = $sessao[0];
Of course this is out of the question. : ) Another option would be:
$this->id = $sessao[0]->id;
$this->token = $sessao[0]->token;
And so on. Maybe I didn’t quite understand how Eloquent works internally on the object. It is possible to perform this loading dynamically?
@Actually it is not necessary to return the object. When making the query can happen two things: the session can be found (then the data is loaded into the same object ), or the session can not be found (and the object remains without loading). To know if the session is valid or not, I created a public method within the Sessao class that checks whether or not the upload occurred and answers a boolean value.
– nefariun
Every query you make using Eloquent returns either a Collection or an instance of the model itself. I believe that the most appropriate solution is to create a static method that returns the first occurrence with this token.
– Vagner do Carmo
Right @Vagnerdocarmo. The way you suggested was what I used to use. If there is no solution I will go back to doing so.
– nefariun