I have a component Posts in my dashboard. It has to manage the posts. I can create and delete the posts perfectly.
My $post consists of some properties. All of them are passed to the component to edit the post, except one property. It’s img. I can save image in the folder storage of my app (there is a link of storage in the folder public). The path to the image is saved in the db. Images are displayed.
The problem is in edit function.
Here is my livewire component Posts:
<?php
namespace App\Http\Livewire;
use App\Models\Category;
use Livewire\Component;
use App\Models\Post;
use Cviebrock\EloquentSluggable\Services\SlugService;
use Livewire\WithPagination;
use Livewire\WithFileUploads;
class Posts extends Component
{
public $title, $categories, $category, $category_id, $body, $post_id, $search, $img;
public $isOpen = 0;
use WithFileUploads;
use WithPagination;
public function mount()
{
$this->categories = Category::all();
}
public function updatedTitle($value)
{
$this->slug = SlugService::createSlug(Post::class, 'slug', $value);
}
public function render()
{
$search = '%' . $this->search . '%';
$posts = Post::where('title', 'LIKE', $search)
->orWhere('body', 'LIKE', $search)
->latest()
->paginate(12);
return view('admin.posts.posts', ['posts' => $posts])->layout('layouts.app');
}
public function create()
{
$this->resetInputFields();
$this->openModal();
}
public function openModal()
{
$this->isOpen = true;
}
public function closeModal()
{
$this->isOpen = false;
}
private function resetInputFields()
{
$this->category_id = '';
$this->img = '';
$this->title = '';
$this->body = '';
$this->post_id = '';
}
public function store()
{
$this->validate([
'category_id' => 'required',
'title' => 'required',
'body' => 'required',
'img' => 'required|image|max:1024'
]);
Post::updateOrCreate(
['id' => $this->post_id],
['category_id' => $this->category_id,
'title' => $this->title,
'body' => $this->body,
'img' => $this->img->hashName(),
]);
if(!empty($this->img)) {
$this->img->store('public/docs');
}
session()->flash('message',
$this->post_id ? 'Пост успешно обновлен.' : 'Пост успешно создан.');
$this->closeModal();
$this->resetInputFields();
}
public function edit($id)
{
$post = Post::findOrFail($id);
$this->category_id = $post->category_id;
$this->post_id = $id;
$this->title = $post->title;
$this->body = $post->body;
$this->img = $post->img;
$this->openModal();
}
public function delete($id)
{
Post::find($id)->delete();
session()->flash('message', 'Пост успешно удален.');
}
}
Here is model Post:
<?php
namespace App\Models;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Cog\Contracts\Love\Reactable\Models\Reactable as ReactableInterface;
use Cog\Laravel\Love\Reactable\Models\Traits\Reactable;
class Post extends Model implements ReactableInterface
{
use HasFactory;
use Sluggable;
use Reactable;
protected $fillable = [
'title',
'slug',
'body',
'img',
'category_id'
];
public function comments()
{
return $this->hasMany(Comment::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function likes()
{
return $this->hasMany(Like::class);
}
public function sluggable(): array
{
return [
'slug' => [
'source' => 'title'
]
];
}
}
Please, any help is appreciated