I'm having this error: Creating default object from empty value in the line with the code:
if ( !$clients->isEmpty() ){ $giftcard->clients_id = $clients[0]->id; }
The whole function:
public function handle($request, Closure $next)
{
if ( empty($request->session()->get('fixedGiftcard')) )
{
$sales = $this->sales->with(['giftcards'])->all();
if ( $sales->isNotEmpty() ){
$found=[];
foreach ($sales as $sale) {
if ( !empty($sale->giftcard_id) ){
$giftcard = $this->giftcards->findBy('id', $sale->giftcard_id);
if ( empty($giftcard->sales_id) ){
$clients = $this->clients->findWhere(['ic' => $sale->ic]);
if ( !$clients->isEmpty() ){ $giftcard->clients_id = $clients[0]->id; }
$giftcard->sales_id = $sale->id;
$giftcard->save();
$found[]=$sale->id;
}else if ( $giftcard->sales_id != $sale->id ){
$sale->giftcard_id = NULL;
$sale->save();
$found[]=$sale->id;
}
}
}
}
$giftcards = $this->giftcards->findWhere([['sales_id','!=', null]]);
if ( $giftcards->isNotEmpty() ){
$found=[];
foreach ($giftcards as $giftcard) {
$sale = $this->sales->findBy('id', $giftcard->sales_id);
// continues