Eloquent + Object loading

0

I have always used Eloquent very well with static methods. Yesterday, when creating a new session log concept for a new project, I came across a question I could not clarify.

I have a "Session" template that points to a table with the same name in the database. It happens that the moment the user passes the token, an object of class "Session" is created and its constructor tries to look for the record related to that token in the database. Usually I would use:

$sessao = Sessao::where('token',Input::get('token'))->get();

Or something like this:

$this->where('token',Input::get('token'))->get();

But in this case, I need to load the result into the object that made 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. Perhaps I have not understood very well how Eloquent works internally in the object. Is it possible to perform this loading dynamically?

    
asked by anonymous 20.08.2014 / 15:40

1 answer

1

Do you want to return the object that has the query result after the query? if this is because you do not create a method within the model:

public function SessionByToken($token){
 return Sessao::where('token',$token)->first();
}
    
20.08.2014 / 15:50