I have a Laravel job class and I'm initializing a Collection attribute with an Eloquent join query. Inside the constructor everything works fine.
The problem comes in the handle method of the class, that specific attribute losses the value initalized previously in the constructor. With a simple Eloquent query everything works well, the value persists.
Here's how the constructor looks like and the model class where the Eloquent join query is made:
class CreateResponseFile extends Job
{
/**
* @var Collection
*/
protected Collection $records;
public function __construct(ResponseWriter $parser, $body)
{
$responseID = $body["response_id"];
$this->parser = $parser;
$this->response = Response::where('id', $responseID)->firstOrFail();
if($parser instanceof WriterCsv){
$this->records = ResponseRecord::getConciliatedRecords($this->response);
}else{
$this->records = ResponseRecord::where('response_id', $responseID)->get();
}
$this->file = $body["path"];
}
}
class ResponseRecord extends Model
{
public static function getConciliatedRecords(Response $response)
{
$records = self::where("response_id", $response->id)
->join("conciliated_trxs", function($join){
$join->on("response_records.id", "=", "conciliated_trxs.response_trx_id");
})
->join("presentation_records", function($join){
$join->on("presentation_records.id", "=", "conciliated_trxs.presentation_trx_id");
})
->get();
return $records;
}
}
My Answer:
'id'];
$this->records = Response::join('users', 'responses.user_id', '=', 'users.id')
->where('responses.id', $responseID)
->get();
}
public function handle()
{
// Here $this->records is empty
}
}
class Response extends Model
{
protected $table = 'responses';
}
Rate this post
3 of 5 based on 1938 votesComments