YuMp's avatar
Level 2

Upload Image return null livewire

Hello, I'm trying to upload a file it is generated in the livewire-tmp directory but using dd at the end of the comment process, the comment_image field is null, does anyone have any idea how to get around this?

thanks in advance.

dd returns --> https://i.ibb.co/BKc7xTX/dd.png AddComment code -->

class AddComment extends Component { use WithAuthRedirects; use WithFileUploads;

public $post;
public $comment;
public $comment_image;
protected $rules = [
    'comment' => 'required|min:4',
    'comment_image' => 'nullable|image|mimes:jpeg,jpg,png|max:18048',
];

public function mount(Post $post)
{
    $this->post = $post;
}

public function addComment()
{
    if (auth()->guest()) {
        abort(Response::HTTP_FORBIDDEN);
    }

    $this->validate();

    $comment = Comment::create([
        'user_id' => auth()->id(),
        'post_id' => $this->post->id,
        'status_id' => 1,
        'comment_image' => $this->comment_image ? $this->comment_image->store('comment_images') : null,
        'body' => $this->comment,
        
    ]);
    $parser = new MentionParser($comment);
    $content = $parser->parse($comment->content);
    $parser = new \App\Parser\CustomParser($comment);
    
    $content = $parser->parse($comment->body);
    $comment->body = $content;
  
  

   
    dd($comment);

    $comment->save();

    $this->reset('comment');


    $this->post->user->notify(new CommentAdded($comment));

    $this->emit('commentWasAdded', 'Comment was posted!');
}

add-comment.blade.php ....

                <label for='image' class="flex items-center justify-center w-1/2 h-11 text-xs bg-gray-200 font-semibold rounded-xl border border-gray-200 hover:border-gray-400 transition duration-150 ease-in px-6 py-3">
                    <svg  ..../>
                      </svg>
                      
                    <span class="ml-2">Anexar IMG</span>
                    
                </label>
               
                <input wire:model.defer="comment_image" id='comment_image' name="comment_image" type='file' value='' />
                <button
                    type="submit"
                    class="flex items-center justify-center h-11 w-full md:w-1/2 text-sm bg-blue text-white font-semibold rounded-xl border border-blue hover:bg-blue-hover transition duration-150 ease-in px-6 py-3"
                >
                    Post Comment
                </button>
          
            </div>

        </form>
0 likes
4 replies
Nakov's avatar
Nakov
Best Answer
Level 73

Maybe you forgot to add comment_image in your $fillable array of the Comment model?

You can always dd($this->comment_image->store('comment_images')) the content of this and see what it returns.

YuMp's avatar
Level 2

@Nakov Thanks for your comment 'image' => dd($this->comment_image ? $this->comment_image->store('comment_images') : null) returns null

Comment model

protected $fillable = ['user_id','post_id','status_id','comment_image', 'body'];

Nakov's avatar

@YuMp I didn't say to add the dd() on the line where you store it, but just before you create it.

So:

dd($this->comment_image); 

$coment = Comment::create...

this one also returns null?

Why do you have value='' on your input?

YuMp's avatar
Level 2

@Nakov thanks really was missing some things in the model, all ok now.

Please or to participate in this conversation.