Skip to content
Advertisement

Laravel 8 job Cannot pass parameter 1 by reference error

I’m working with a Laravel 8 project and have created a Job that gets processed. I’ve got several functions to extract functionality but am getting the following error:

Cannot pass parameter 1 by reference

The trace is initially saying on line 65, which would be 'title' => $page->header ?? "Page $key" but I’m not sure what I’m missing here?

My attached job is:

<?php

namespace AppJobs;

use IlluminateBusQueueable;
use IlluminateContractsQueueShouldBeUnique;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationBusDispatchable;
use IlluminateQueueInteractsWithQueue;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesHttp;
use AppModelsBeam;
use AppModelsBeamData;
use AppJobsFetchBeamData;
use CarbonCarbon;
use GuzzleHttpClient;
use IlluminateSupportFacadesLog;

class FetchBeamData implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    /*
    ** Beam whose data should be linked to
    */
    protected $beam;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($beam)
    {
        $this->beam = $beam;
    }

    /**
     * Beam content schema
     */
    protected function formatBeamContent($data)
    {
        $pages = $this->formatBeamPages($data);

        return [
            'css' => $data->css ?? null,
            'notification' => $data->notification ?? null,
            'pages' => $pages ?? []
        ];
    }

    /**
     * Beam content pages
     */
    protected function formatBeamPages($data)
    {
        $pages = [];

        if (!$data->lenders || count($data->lenders) <= 0) {
            return $pages;
        }

        foreach ($data->lenders as $key => $page) {
            array_push([
                'title' => $page->header ?? "Page $key",
                'content' => $page->names ? json_encode($page->names) : null
            ]);
        }

        return $pages;
    }

    /**
     * Get Beam
     */
    public function getBeam()
    {
        $beam = Beam::where('id', $this->beam->id)->first();
        return $beam;
    }

    /**
     * Get Beam data
     */
    public function getBeamData()
    {
        try {
            $res = Http::timeout(120)->get($this->beam->url);
            return json_decode($res->body());
        } catch (Exception $e) {
            return [];
        }
    }

    /**
     * Save Beam data
     */
    public function saveBeamData()
    {
        $fetched = $this->getBeamData();
        $fetched = $this->formatBeamContent($fetched);

        try {
            $beamData = new BeamData;
            $beamData->user_id = $this->beam->user_id;
            $beamData->beam_id = $this->beam->id;
            $beamData->notification = $fetched['notification'] ?? null;
            $beamData->beam_content = $fetched ? json_encode($fetched) : null;
            $beamData->save();
        } catch (Exception $e) { }
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        if (!$this->getBeam()) {
            return;
        }

        $this->saveBeamData();
    }
}

Advertisement

Answer

Not sure about the error but you didn’t provide your array to array_push method

it should be

foreach ($data->lenders as $key => $page) {
        array_push($pages, [
            'title' => $page->header ?? "Page $key",
            'content' => $page->names ? json_encode($page->names) : null
        ]);
    }
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement