我正在尝试创建一个帖子表单,用户可以在其中上传图像,但图像不会被保存,并且
asset
模型不会创建新条目。我正在使用 Livewire 和 Laravel 的文件系统。我在 Laravel 文档中阅读了使用 hashName()
的信息,因为它更安全。我不确定为什么该方法不存储文件。
<form wire:submit.prevent="store_post" enctype="multipart/form-data">
<div class="mb-4">
<textarea wire:model="content" id="content" name="content" rows="4"
class="block mt-1 w-full border-gray-300 focus:border-accent focus:ring-0 rounded-md shadow-sm"></textarea>
</div>
<div class="mb-4">
<input wire:model="post_img" name="post_img" type="file" class="block w-full text-sm text-slate-500
file:mr-4 file:py-2 file:px-4
file:rounded-full file:border-0
file:text-sm file:font-semibold
file:bg-black-50 file:text-black-700
hover:file:bg-black-100
" />
@if ($post_img)
<img src="{{ $post_img->temporaryUrl() }}">
@endif
</div>
<x-primary-button>Post</x-primary-button>
</form>
Livewire 组件
namespace App\Livewire\Pages;
// ...Other use namespace calls.
use Livewire\WithFileUploads;
class GroupPage extends Component
{
use WithFileUploads;
public $group_id = '';
public $content = '';
public $post_img;
public function store_post()
{
$validated = $this->validate([
'content' => 'required|string',
'post_img' => 'nullable|image|max:2048', // Add validation rule here
]);
$user_id = Auth::id();
$post = Post::create([
'content' => $validated['content'],
'group_id' => $this->group_id,
'user_id' => $user_id,
]);
if ($this->post_img) {
$filename = $this->post_img->hashName();
$path = $this->post_img->storeAs('uploads/posts');
Asset::create([
'path' => $path,
'type' => 'image',
'user_id' => $user_id,
'post_id' => $post->id(),
]);
}
$this->reset('content');
$this->post_img = null;
session()->flash('success', 'Post created!');
}
// ...
}
资产模型:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Asset extends Model
{
protected $fillable = ['path', 'type', 'user_id', 'post_id'];
public function user()
{
return $this->belongsTo(User::class);
}
public function post()
{
return $this->belongsTo(Post::class);
}
}
资产迁移:
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('assets', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('path');
$table->enum('type', ['image', 'video', 'file']);
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->foreignId('post_id')->constrained('posts')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('assets', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['post_id']);
});
Schema::dropIfExists('assets');
}
};
当您保存帖子时,屏幕上或 laravel.log 中是否有任何错误?也许,在这部分代码中
Asset::create([
'path' => $path,
'type' => 'image',
'user_id' => $user_id,
'post_id' => $post->id(),
]);
而不是
'post_id' => $post->id(),
你应该使用
'post_id' => $post->id,
当您保存图像而不是
时$path = $this->post_img->storeAs('uploads/posts');
你应该使用
$path = $this->post_img->storeAs('uploads/posts/', $filename);