0

I am using laravel 5.4 and using nWidart/laravel-modules to implement the module concept.

->Created a controller ,via that controller i called a job using jobs concept in laravel i wrote a post method , first i capture that in controller then sent the request array to job for inserting inside the table the process is working fine and if i print the result in jobs the object is printing successfully but when i return that to the controller its showing as "0"

-->then i tried to return a normal string still its showing 0 as responce in controller

my Controller

public function store(Request $request)
    {
        $user = dispatch(new CheckJob($request));
        return $user;
    }

My Job

public function __construct($requestParams)
    {

        $this->id = isset($requestParams['id']) ? $requestParams['id'] : null;
        $this->firstName = isset($requestParams['firstName']) ? $requestParams['firstName'] : null;
        $this->lastName = isset($requestParams['lastName']) ? $requestParams['lastName'] : null;
        $this->email = isset($requestParams['emailId']) ? $requestParams['emailId'] : null;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $user = User::firstOrNew(array('id' => $this->id));
        $user->first_name = $this->firstName;
        $user->last_name = $this->lastName;
        $user->email = $this->email;
        $user->save();
        return $user;
    }

When i print $user in controller i get "0" as output

krishna
  • 31
  • 5

2 Answers2

1

When i create a job via command line using php artisan module:make-job user

the created file was

class CheckJob implements ShouldQueue
{

}

Since it was implementing ShouldQueue the function was acting as async but when i removed implement ShouldQueue it act as sync and returning the id as expected

krishna
  • 31
  • 5
  • Yup, and if I'm not mistaken, you can pass the --sync option to php artisan when creating the job to avoid having to make this change manually afterwards. Glad you managed to get this resolved. – Sheraz Mar 18 '17 at 12:25
0

A big part of this will come down to the fact that Jobs in Laravel have the potential to be asynchronous, and are usually used in that way. If you intend to use ShouldQueue with your Job, it's simply not possible to return a value to your controller (at least not without an extremely hacky solution). If you're triggering the job and returning instantly, i.e. synchronously, remove the ShouldQueue implementation. From there, it'll be simpler to return data from the job, take a look at the suggestion in this answer for one possible approach:

https://stackoverflow.com/a/37335658/7086017

Community
  • 1
  • 1
Sheraz
  • 160
  • 8
  • thank you Sheraz, so job is asyn function so is there a possible way to make that sync so that i can get the return and move to next line in controller with the user object; in the link thay you told i tried but still getting 0 as response from function response – krishna Mar 17 '17 at 10:27