Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Randy_Johnson's avatar

Using class of my own in livewire

In Livewire, I want to use another class of my own, lets say image ?Controller?. Is the best way to do this just to create another livewire class, delete the view, and have all the image affiliated methods and functions stuck into this class to be used within a specific livewire component.

Regards, Randy

0 likes
17 replies
martinbean's avatar

@randy_johnson Show some example code. What does this image controller do, because you shouldn’t be trying to use controller classes within a Livewire component.

Randy_Johnson's avatar

@martinbean well, I want a class that I can copy and paste from one project to another, since everything with image is similar. I was just wondering do I create a livewire image, and delete the view, or should I create a model?

martinbean's avatar

@Randy_Johnson Again, show some example code. It’s impossible to advise one way or the other without seeing some actual code.

Randy_Johnson's avatar

@martinbean

    public function submit()
    {
        $post = new Post;
        $post->user_id = auth()->id();
        $post->head = $this->head;
        $post->body = $this->body;
        dd($this->image);
        $post->image = $this->upload_image($this->image); 
        $post->save();
        $this->reset('head', 'body', 'image');
    }

right now, I have upload_image in the Post (livewire). But I want it somewhere alone, so I can easily copy and paste from one project to another. Should I use Model or have it in another livewire class?

Snapey's avatar

You should use the livewire uploads store method

If you have common functionality, you could put it in a trait. But for one line ... not saving anything...

Randy_Johnson's avatar

@Snapey like this?

    public function upload_image($p_image)
    {
        $image = $p_image;
        $url = 'storage/';
        
        $this->validate([
            'image' => 'image|max:10240', // 1MB Max
        ]);

        if ($image) {
            $url .= $image->store('images/posts', 'public');
        }
        else
        {
            $url .= 'images/universal/no-image.gif';
        }

        return $url;
    }
Randy_Johnson's avatar

@martinbean Yeah, but here he has it in the component, every time I make a new project I have to rewrite it. I want something solid, that I can reuse all the time, and in the livewire docs it hasn't got logic that would delete an old picture.

Snapey's avatar

in a model? Yeah that makes load more sense

Randy_Johnson's avatar

@Snapey Sarcasm. Well the way I see it is that I could have a whole system linked this way, have a db of image - id, url, type, u_id. Then I can easily port it from one project to another and make it as universal as one possible can.

jlrdw's avatar

@Randy_Johnson I would probably put it in a special service class not a model, but if it works good for you then all should be good.

unicsoft1's avatar

I encountered this same issue In Laravel 11.x and Livewire 3 because I wanted to create classes for separate methods that i will need to include in other classes to solve the same problem.

So, after all efforts what i did was to use Livewire form object.

Form objects allow you to re-use form logic across components and provide a nice way to keep your component class cleaner by grouping all form-related code into a separate class.

You can check the full Docs for clearer understanding

You can either create a form class by hand or use the convenient artisan command:

php artisan livewire:form DeleteRecords

// this is what the form object looks like

<?php

namespace App\Livewire\Forms;

// use Livewire\Attributes\Validate;
use Livewire\Form;
use App\Models\Universities;

class DeleteRecords extends Form
{

    public function DeleteRecord($Model, $id)
    {
        $record = $Model::find($id);
        $record->delete();
    }

    public function Swal(){
        return 
            [
                  'title' => 'Great!',
                  'message' => 'Record Deleted successfully',
                  'icon' => 'success'
            ]; 
    }
}

//

You can see that i can now call and use the form object like so

 $this->deletePrompt->DeleteRecord('App\Models\Countries', $id);

Please or to participate in this conversation.