monaabdo88's avatar

livewire.js?id=5d3e67e0:347 Uncaught TypeError: Cannot read properties of null (reading 'name')

Hello all I am working on a new project similar to youtube (laravel v10 & livewire v3) every user has one channel the issue is I am trying to update user Channel data but no data shows in livewire components and error message show on the console tab

"livewire.js?id=5d3e67e0:347 Uncaught TypeError: Cannot read properties of null (reading 'name')"

I have two tables of users & channels

This my code Route File

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ChannelController;
Route::get('/', function () {
    return view('welcome');
});

Auth::routes();
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
Route::middleware('auth')->group(function () {

    Route::get('/channel/{channel}/edit', [ChannelController::class, 'edit'])->name('channel.edit');
});

EditChannel livewire

<?php

namespace App\Livewire\Channel;
use Livewire\Component;
use App\Models\Channel;
use SebastianBergmann\Diff\Chunk;

class EditChannel extends Component
{
    public $channel;
    public $image;

    protected function rules()
    {

        return [

            'channel.name' => 'required|max:255|unique:channels,name,' . $this->channel->id,
            'channel.slug' => 'required|max:255|unique:channels,slug,' . $this->channel->id,
            'channel.description' => 'nullable|max:1000',
            'image' => 'nullable|image|max:1024',

        ];
    }


    public function mount(Channel $channel)
    {

        $this->channel = $channel;
    }
    public function render()
    {
        return view('livewire.channel.edit-channel');
    }

    public function update()
    {

        $this->validate();

        $this->channel->update([

            'name' => $this->channel->name,
            'slug' => $this->channel->slug,
            'description' => $this->channel->description,

        ]);

        session()->flash('message', 'Channel updated');

        return redirect()->route('channel.edit', ['channel' => $this->channel->slug]);
    }
}

edit-channel form

<div>
    @if($channel->image)
    <img src="{{ asset('images' . '/' . $channel->image)}}" alt="">
    @endif
    <form wire:submit.prevent="update">


        <div class="form-group">
            <label for="name">Name</label>
            <input type="text" class="form-control" wire:model="channel.name">
        </div>

        @error('channel.name')
        <div class="alert alert-danger">
            {{ $message }}
        </div>
        @enderror

        <div class="form-group">
            <label for="slug">Slug</label>
            <input type="text" class="form-control" wire:model="channel.slug">
        </div>

        @error('channel.slug')
        <div class="alert alert-danger">
            {{ $message }}
        </div>
        @enderror

        <div class="form-group">
            <label for="description">Description</label>
            <textarea cols="30" rows="4" class="form-control" wire:model="channel.description"></textarea>
        </div>

        @error('channel.description')
        <div class="alert alert-danger">
            {{ $message }}
        </div>
        @enderror

        <div class="form-group">
            <label></label>
            <input type="file" wire:model="image" class="form-control">
        </div>

        <div class="form-group">
            @if ($image)
            Photo Preview:
            <img src="{{ $image->temporaryUrl() }}" class="img-thumbnail">
            @endif
        </div>

        @error('image')
        <div class="alert alert-danger">
            {{ $message }}
        </div>
        @enderror

        <div class="form-group">
            <br>
            <button type="submit" class="btn btn-primary">Update</button>
        </div>

        @if(session()->has('message'))
        <div class="alert alert-success">
            {{ session('message')}}
        </div>
        @endif

    </form>
</div>

Main Edit File

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Channe') }}</div>

                <div class="card-body">
                    <livewire:channel.edit-channel :channel="$channel" />
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
0 likes
7 replies
Leeajames's avatar

I seem to have the same issue driving me nuts, it's passing data and displaying data but on update it just returns the original data!! ? did you manage to solve this?

1 like
mohdm's avatar

facing this issue as well, not trying to understand why Livewire3 is throwing the error when Livewire 2 works fine

mxwebdev's avatar

I had the same issue. Turned out it was caused by the the legacy_model_binding setting in my Livewire config. After changing the setting to true the error disappeared.

'legacy_model_binding' => true,

The setting is related to the changed Eloquent model binding behaviour in Livewire v3.

4 likes
mohdm's avatar

@mxwebdev thank you, found this guide as well on the Upgrade documentation - but my problem still persist even after changing the config to True. Odd behavior, maybe a bug on my project.

1 like
aleahy's avatar

@mohdm I came across a similar issue today. My error was similar but was trying to read 'before' instead of 'name' on null. Found two separate causes: Turned out one of the wire:key attributes were producing the same values for some models inside a loop. Once they were unique it reduced some of the occurences of the bugs.

The other was related to the construction of an anonymous component being used to display the data in a loop. We had the opening tag of the component in an @if @else block depending on a passed in attribute. After pre-calculating the required attributes and using the opening tag outside of the logic, the problem went completely away.

Both these issues occurred inside a @foreach so it might be worth looking there first if it applies to you.

Please or to participate in this conversation.