【发布时间】:2022-01-16 08:53:18
【问题描述】:
我正在使用 livewire,我正在尝试为一个帖子上传多张图片。但是,我无法让它工作。当我创建带有图像的帖子时,没有图像保存到数据库中
class Add extends Component
{
use WithFileUploads;
public $post;
public $users;
public $body;
public $image = [];
public $title;
public $category = 1;
protected $rules = [
'category' => 'required|integer|exists:categories,id',
'title' => 'required|min:4',
'body' => 'required|min:4',
];
public function createPost()
{
if (auth()->check()) {
$this->validate();
$post = Post::create([
'user_id' => auth()->user()->id,
'title' => $this->title,
'category_id' => $this->category,
'status_id' => $this->status,
'body' => $this->body,
]);
foreach ($this->image as $photo) {
$photo->storeAs('posts', str::random(6));
}
$post->save();
session()->flash("message", "Featured image successfully uploaded");
【问题讨论】:
标签: php laravel laravel-livewire