Cant redirect from Livewire controller

When I return a redirect from my livewire component I get an error
Trying to get property ‘name’ of non-object

The non object/property in question is the auth()->user()->name
It is the logged in user displayed on the dashboard.

I have a function done() which redirects to a jobs.show route. This route then returns a view with the auth()->user()->name.

If i redirect from any other controller (non-livewire) all works fine. Its as if the session data is not available when you redirect from a livewire component.

Here is my Livewire Controller

<?php

namespace App\Http\Livewire;

use App\Hazard;
use App\Job;
use App\Task;
use Illuminate\Support\Facades\Redirect;
use Livewire\Component;

class AddHazards extends Component
{
    public $job;
    public $task;
    public $generic_hazards = [];
    public $general_hazards = [];
    public $selected_generic_hazards = [];
    public $selected_generic_hazard = null;
    public $selected_general_hazards = [];
    public $selected_general_hazard = null;
    public $hazards_in_task;
    public $selected_task;

    public function mount($task, $job)
    {
        $this->generic_hazards = Hazard::select('id', 'title')->where('generic', true)->orderBy('title', 'asc')->get()->toArray();
        $this->general_hazards = Hazard::select('id', 'title')->where('generic', false)->orderBy('title', 'asc')->get()->toArray();
        $this->hazards_in_task = array_column( $task->hazards()->get()->toArray(), 'title', 'id');

        $this->task = $task;
        $this->job = $job;
    }

    public function addSelectedGenericHazard()
    {
        if($this->selected_generic_hazard != null){
            $added_hazard = Hazard::find($this->selected_generic_hazard);
            $this->attachHazard($added_hazard->id);
            $hazard = ['id' => $added_hazard->id, 'title' => $added_hazard->title];
            if(!in_array($hazard, $this->selected_generic_hazards))
            {
                $this->selected_generic_hazards[] = $hazard;
            }
        }
        $this->refreshGeneric();
    }

    public function removeSelectedGenericHazard($index)
    {
        $this->detachHazard($this->selected_generic_hazards[$index]['id']);
        unset($this->selected_generic_hazards[$index]);
        $this->selected_generic_hazards = array_values($this->selected_generic_hazards);
    }

    public function addSelectedGeneralHazard()
    {
        if($this->selected_general_hazard != null){
            $added_hazard = Hazard::find($this->selected_general_hazard);
            $this->attachHazard($added_hazard->id);
            $hazard = ['id' => $added_hazard->id, 'title' => $added_hazard->title];
            if(!in_array($hazard, $this->selected_general_hazards))
            {
                $this->selected_general_hazards[] = $hazard;
            }
        }
        $this->refreshGeneral();
    }

    public function removeSelectedGeneralHazard($index)
    {
        $this->detachHazard($this->selected_general_hazards[$index]['id']);
        unset($this->selected_general_hazards[$index]);
        $this->selected_general_hazards = array_values($this->selected_general_hazards);
    }


    public function attachHazard($id)
    {
        $this->task->hazards()->attach($id);
    }

    public function detachHazard($id)
    {
        $this->task->hazards()->detach($id);

    }

    public function refreshGeneral()
    {
        $this->selected_general_hazards = [];
        $this->selected_general_hazard = null;
        $this->task->refresh();
        $this->render();
    }

    public function refreshGeneric()
    {
        $this->selected_generic_hazards = [];
        $this->selected_generic_hazard = null;
        $this->task->refresh();
        $this->render();
    }

    public function done()
    {
        return redirect()->route('jobs.show', [$this->job->id]);
    }

    public function render()
    {
        return view('livewire.add-hazards');
    }
}

Anyone had this issue before.
The only packages I have installed is Laravel Debugbar, laravel/ui and livewire.
My laravel version is 7.16.1 and my livewire is 1.2.

1 Like