2

I wanna try to store data into database via database queue Laravel.

But I always get this error "Undefined offset: 0"

this is my controller :

    public function store(Request $request)
    {
        $order = new Order;
        $order->code = $request->code;
        $order->created_at = $request->created_at;

        $this->dispatch(new SalesOrder($order));
    }

and this is my SalesOrder Jobs :

    protected $order;

    public function __construct(Order $order)
    {
        $this->order= $order;
    }


    public function handle()
    {
         $this->order->save();
    }

is there something wrong in my code? Please somebody help me fix this issue. Than's anyway.

2 Answers 2

2

Instead of pass Order object pass in job order data, And the in job save order.

Controller code.

public function store(Request $request)
{
    $data['code'] = $request->code;
    $data['created_at'] = $request->created_at;

    $this->dispatch(new SalesOrder($data));
}

Job code

protected $data;

public function __construct(array $data)
{
    $this->data = $data;
}


public function handle(Order $order)
{
    if (!$order->craete($this->data)) {
        // when not saved try again
        $this->release();
    }

    return true;
}
Sign up to request clarification or add additional context in comments.

12 Comments

@Almaida Jody Are you try this??
@AlmaidaJody Jody It is your expected result??
im sorry for this late reply.. i've try and it return the same error again.. "Undefined offset: 0"
can you tell me what exactly I have to do, to save data via laravel queue?
even I just do public function handle(Order $order) { dd($order);exit(); } in my handle function, it will return "Undefined Offset [0]".
|
0

Try running

$order = new Order;
$order->code = $request->code;
$order->created_at = $request->created_at;

inside your job so it looks like this

protected $order;
protected $request

public function __construct($request)
{
  $this->order = new Order;
  $this->request = $request;
}


public function handle()
{
  $this->order->code = $request->code;
  $this->order->created_at = $request->created_at;
  $this->order->save();
}

and in your controller

public function store(Request $request)
{
   $this->dispatch(new SalesOrder($request));
}

This should work. Did not test this am writing on my phone.

1 Comment

now it return an error like this "Serialization of 'Closure' is not allowed"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.