ene's avatar
Level 2

Call to a member function clearMediaCollection() on null

am using the spatie media libray package trying to upload image for a thread getting this error please assist

<?php

namespace App\Http\Livewire\Thread;

use App\Models\Tag;
use App\Models\User;
use App\Models\Space;
use App\Models\Thread;
use Livewire\Component;
use Illuminate\Http\Response;
use App\Events\ThreadWasCreated;
use Livewire\WithFileUploads;
use Spatie\MediaLibraryPro\Http\Livewire\Concerns\WithMedia;

class Create extends Component
{
    use WithFileUploads;

    public $thread;
    public $users;
    public $body;
    public $image;

    public $photo;
    public $title;
    public $space = 1;
    protected $rules = [
        'space' => 'required|integer|exists:spaces,id',
        'title' => 'required|min:4',
        'body' => 'required|min:4',
        'image' => ['required', 'image'],
    ];

    public function createThread()
    {
        if (auth()->check()) {
            $this->validate();
            $thread = Thread::create([
                'user_id' => auth()->user()->id,
                'title' => $this->title,
                'space_id' => $this->space,
                'body' => $this->body,
            ]);
            event(new ThreadWasCreated($thread));
            // SendTo::Twitter('Hello, I\'m testing Laravel social auto posting');
            $this->thread->clearMediaCollection('thread_image');
            $this->thread->addMedia($this->image->getRealPath())->toMediaCollection('thread_image');

            session()->flash("message", "Featured image successfully uploaded");

            preg_match_all('/(?<=#)(\w+)/mi', $this->body, $matchedTags, PREG_SET_ORDER, 0);
            foreach ($matchedTags as $matchedTag) {
                if (!$tag = Tag::where('name', $matchedTag[0])->first()) {
                    $tag = Tag::create(['name' => $matchedTag[0]]);
                }
                $thread->tags()->attach($tag->id);
                $tag->addEnergy(1);
            }

            preg_match_all('/(?<=@)(\w+)/mi', $this->body, $matchedMentions, PREG_SET_ORDER, 0);
            foreach ($matchedMentions as $matchedMention) {
                optional(User::where('username', $matchedMention[0])->first(), function ($user) {
                    // $user->notify(new MentionsNotify($user));
                });
            }

            session()->flash('success', 'Post was added successfully!');
            return redirect(route('index'));
            $this->reset();
        }

        abort(Response::HTTP_FORBIDDEN);
    }
    public function render()
    {
        $spaces = Space::get();
        return view('livewire.thread.create',[
            'spaces' => $spaces,
        ]);
    }
}
0 likes
1 reply
Braunson's avatar

Looks like you are calling clearMediaCollection on the $this->thread variable, based on your error, the $this->thread is returning null. So you can't call a method on null, check why $this->thread isn't returning something and add in an IF check to make sure it's not null.

Please or to participate in this conversation.