How to pass data from background schedule job to global scope?

I have a background schedule job that doing some code and executing quires that have global scope append to their model and i’m trying to pass data from the job to the global scope like this

job.php

class Job
{

public static function execute() {

//Im trying here to find a cleaner and working way better than app()->user_id
//even it's a bad way, it's also not working
//because when trying to get the value in UserScope it's always null
app()->user_id = 1;

app(CreateObjectAction::class)->execute($data);
}
}

CreateObjectAction.php

class CreateObjectAction
{

public function execute(Data $data)
{
try {

DB::beginTransaction();

if (app()->runningInConsole()) {

//here app()->user_id always is null when the global scope `UserScope` is applied to the query
//so $exist will be also always empty
$exist = Object::query()->first();

} else {
//....
}

if(empty($exist)) {

//here is trying to create an existed object and it gives error
$object = Object::create(data)->toArray());

}

DB::commit();

return $object;

} catch (Throwable $e) {
DB::rollBack();
Log::error($e->getTraceAsString());
return null;
} catch (Exception $e) {
DB::rollBack();
Log::error($e->getTraceAsString());
return null;
}
}
}

Object.php

class Object extends Model
{
use BelongsToUser;
}

BelongsToUser.php

trait BelongsToUser
{
protected static function BelongsToUser()
{

static::addGlobalScope(new UserScope);

}
}

UserScope.php

class UserScope implements Scope
{
public function apply(Builder $builder, Model $model)
{
if (app()->runningInConsole()) {

//here app()->user_id always null as well.
$builder->where("{$model->getTable()}.user_id", "=", app()->user_id);

} else {
//....
}
}
}

so please what is the best way that i can pass data from a job running by background worker in console to global scope may thanks in advance for any help

from Newest questions tagged laravel-5 – Stack Overflow https://ift.tt/3AxD7dx
via IFTTT

Related Posts

Codeigniter : Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE) in Laravel project

I’m getting following error: **Parse error: syntax error, unexpected ‘const’ (T_CONST), expecting variable (T_VARIABLE)** Note : It’s working in local but facing issue in production server. private…

Firebase receive notification while tab is active or on focus

What i want is to be able to perform an action when a user receives a notification while the browser is open and tab is active or…

Laravel’s alias loader does not find class

We have a legacy project that we cannot update and we need to make some changes in symfony’s Response.php in vendor. We have solved this by copying…

Laravel 5 – generic document management

I have a system where you can create different types of unique documents. For instance, one document is called Project Identified and this expects certain inputs. Originally,…

Laravel Nova limit the results in indexQuery

I ran intro a situation where I need to limit the results of a resource to only 3 results. To be more specific, based on the logged…

Auditoria en laravel 5.8 [closed]

Cómo puedo automatizar el registro de actividades de un usuario en laravel? Si un usuario ingresa a un app de laravel, debo guardar toda su actividas, a…

Leave a Reply

Your email address will not be published.