@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.
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
@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?
@Randy_Johnson Again, show some example code. It’s impossible to advise one way or the other without seeing some actual code.
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?
@Randy_Johnson you could just make that a trait
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...
@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;
}
not what i would do
@Snapey What would you do? Can you show an example?
What would you do? Can you show an example?
@Randy_Johnson Read the Livewire docs. It has a section on file uploads: https://laravel-livewire.com/docs/2.x/file-uploads
@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.
@Randy_Johnson So extract the logic to a class. Stop over-complicating things.
@martinbean Yeah, I stuck it in a model and made it static.
in a model? Yeah that makes load more sense
@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.
@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.
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'
];
}
}
//
<?php
namespace App\Livewire\Admin;
use Livewire\Component;
use App\Models\Countries;
use App\Livewire\Forms\DeleteRecords;
use Livewire\Attributes\On;
class CountryIndex extends Component
{
public DeleteRecords $deletePrompt;
public function render()
{
return view('admin.country-index',
[
'countries' => Countries::latest()->get(),
]
);
}
protected $listeners = [
'swal' => '$refresh'
];
#[On('Confirm-Delete')]
public function DeleteRecord($id)
{
$this->deletePrompt->DeleteRecord('App\Models\Countries', $id);
$this->dispatch(
'swal',
$this->deletePrompt->Swal()
);
}
}
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.