簡體   English   中英

laravel 8 API:上傳多張圖片並用 postman 存儲

[英]laravel 8 API : upload multiple images and store them with postman

我正在構建一個帶有 laravel 8 的 API。我想為我的帖子存儲圖像或圖像,帖子和圖像之間具有多態關系(因為我也有用戶和分析表,我需要他們的圖像),但我無法上傳並存儲圖像,當我在 postman 中發送值時,如下所示:

在此處輸入圖像描述

如您所見,圖像未存儲在圖像表中,因此我看不到有關圖像的任何信息

我是初學者,不太了解多態性,所以我認為我的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();
        });

和 model 后:

.
.
.
.
 public function image(){
        return $this->morphOne(Image::class , 'imageable');
    }

和我的圖像 model:

 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);

    }

同樣在表格數據中的 postman 中,我輸入圖像作為文件類型

在標題部分我輸入帶有multipart/form-data值的Content-Type

我不知道如何保存圖像,謝謝你幫助我。

編輯:

我將 controller 更改為:

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);
    }

在 postman 中,我在正文(表單數據)中輸入帶有url字段的圖像: 在此處輸入圖像描述

所以我的foreach($files as $file)有一個錯誤有什么問題?

郵遞員演示

嘗試發送多個圖像,如屏幕截圖所示,您正在發送沒有名稱的圖像,因此請求無法獲取哪個鍵保存圖像,發送多個圖像使用數組(圖像 []),如屏幕截圖中和發送單個圖像,您可以使用單個字段名稱與任何其他普通字段名稱一樣

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM