When you add jobs to a queue, you must pass all required data. For example, if it is a job to resize a user's avatar, the required information is the user's id so that he can load his model. Like when you are viewing a user's profile page in the browser, it passes the required information to the URI request (for example, usuários/ profile/{id}
).
Sessions will not work for queue jobs, because sessions are used to define states from browser requests, and queue jobs are run by the system.
So, continuing with the user's avatar example. You could pass the user id to the job, or pass the entire user model, however, if job is delayed the state of this user may have changed in the meantime, then you would be working with inaccurate data.
In this way, to add your job to the queue, you would use something like:
Queue::push('AvatarProcessor', [Session::get('id_logado')]);
When the job starts up, load the user from the database, and then pass it on to the other classes participating in the job. In this example I simulated sending the user id from the session as you wish. Imagining that the whole process starts with a browser user interface.
Following is an example of the avatar class retrieving the information passed to the job.
class AvatarProcessor {
public function fire($job, $data)
{
$user_id = $data[0]; // o id do usuário está no primeiro índice do array
$user = User::find($user_id); // peça para o modelo carregar os dados do usuário baseado em seu id
if ($user == null)
{
// caso o usuário não exista, trate corretamente com avisos, quem sabe, por email ao administrados
}
// Execute a lógica de manipulação de imagem necessária, um exemplo simples seria o código a seguir
(new ImageManipulator)->resizeAvatar($user);
$user->save(); // salve as alterações
$job->delete(); // exclua o job ao concluir
}
}
This response was based on and aided by discussion in link and by the documentation of Laravel queues at link