【问题标题】:laravel 8 API : upload multiple images and store them with postmanlaravel 8 API:上传多张图片并用邮递员存储
【发布时间】: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-Typemultipart/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


    【解决方案1】:

    尝试发送多个图像,如屏幕截图所示,您正在发送没有名称的图像,因此请求无法获取哪个键保存图像,发送多个图像使用数组 (image[]) 就像在屏幕截图中一样,发送单个图像给您可以像任何其他普通字段名称一样使用单个字段名称

    【讨论】:

    • 所以我输入了一个字段 url[] 并选择 3 张图片进行上传,但上面写着:rrorException: Invalid argument supplied for foreach()
    • 而不是url[] 尝试url 并获取类似$url = $request->file('url'); foreach($url as $u){ print_r($u->getClientOriginalName()); } 的文件
    • Click Here 请参考这个 我测试了它并且它有效,在邮递员中使用url 而不是url[]
    • 我编辑了问题..请再检查一遍
    猜你喜欢
    • 2020-03-20
    • 1970-01-01
    • 2014-04-02
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多