【发布时间】:2021-06-29 23:00:58
【问题描述】:
我正在使用 laravel 8 构建一个 API。我想为帖子和图像之间的多态关系存储图像或图像(因为我也有用户和分析表,我需要它们的图像),但是当我在邮递员中发送值时,我无法上传和存储图像,像这样:
如您所见,图像不存储在图像表中,结果我看不到有关图像的任何内容
我是初学者,不太了解多态性,所以我认为我的store() 方法不正确。
这是我的帖子表:
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('category_id');
$table->unsignedBigInteger('user_id');
$table->string('title');
$table->longText('body');
$table->string('video')->nullable();
$table->string('study_time');
$table->integer('likes')->nullable();
$table->tinyInteger('status')->nullable()->comment('status is 1 when a post is active and it is 0 otherwise.')->nullable();
$table->text('tags')->nullable();
$table->foreign('category_id')->references('id')->on('categories');
$table->foreign('user_id')->references('id')->on('users');
});
还有我的图片表:
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->integer('imageable_id');
$table->string('imageable_type');
$table->string('url');
$table->timestamps();
});
和后期模型:
.
.
.
.
public function image(){
return $this->morphOne(Image::class , 'imageable');
}
还有我的图像模型:
protected $fillable = [
'url'
];
public function imageable(){
return $this->morphTo();
}
还有我在 postController 中的 store() 方法:
public function store(Request $request )
{
$post = new Post;
$post->category_id = $request->get('category_id');
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->study_time = $request->get('study_time');
$post->tags = $request->get('tags');
$post->user_id = JWTAuth::user()->id;
$tags = explode(",", $request->tags);
$post->tag($tags);
if($request->hasfile('url'))
{
foreach($request->get('url') as $file)
{
$image = new Image;
$name = $file->getClientOriginalName();
$image->move(public_path().'/images/',$name);
$post->photos()->save($image);
}
}
$post->save();
return response()->json($post , 201);
}
在邮递员的表单数据中,我输入图像作为文件类型
在标题部分我输入Content-Type 和multipart/form-data 值和
我不知道如何保存图片,谢谢你的帮助。
编辑:
我把控制器改成这个:
public function store(Request $request )
{
$post = new Post;
$post->category_id = $request->get('category_id');
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->study_time = $request->get('study_time');
$post->tags = $request->get('tags');
$post->user_id = JWTAuth::user()->id;
$tags = explode(",", $request->tags);
$post->tag($tags);
$allowedfileExtension=['pdf','jpg','png'];
$files = $request->file('fileName');
foreach ($files as $file) {
$extension = $file->getClientOriginalExtension();
$check = in_array($extension, $allowedfileExtension);
if($check) {
foreach($request->fileName as $mediaFiles) {
$url = $mediaFiles->store('public/images');
//store image file into directory and db
$image = new Image();
$image->url = $url;
}
}
else {
return response()->json(['invalid_file_format'], 422);
}
}
$post->image()->save($image);
$post->save();
return response()->json($post , 201);
}
在邮递员中,我在 body(form-data) 中输入带有 url 字段的图像:
所以我的foreach($files as $file) 出错了,有什么问题?
【问题讨论】:
标签: php laravel api polymorphism