0

i'm building an API with laravel8 and want to upload image for posts , and when i don't send values for posts fields in postman , gives me this error :

call to a member function extension() on null

and when i send values for fields , gives this error :

 "error": {
        "images": [
            "validation.uploaded"
        ]
    },

i changed the size of uploaded files in php.ini but it wasn't fixed.

so as you can see , my validation doesn't work when i don't enter values for the fields

my codes :

post table :

public function up()
    {
        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->string('images');
            $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');

         });
    }

and store() method in PostController :

public function store(Request $request )
    {
        $data = $request->all();

        $validator = Validator::make($data, [
            'user_id'=>'required',
            'category_id'=>'required',
            'title' => 'required|max:100|unique:categories',
            'body' => 'required|max:500',
            'study_time'=>'required',
            'images' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);

        $imageName = time().'.'.$request->images->extension();
        $request->images->move(public_path('images'), $imageName);

        if ($validator->fails()) {
            return response()->json(['error' => $validator->errors(), 'Validation Error']);
        }

        $post = Post::create($data);

        return response()->json([
        "success" => true,
        "message" => "successfully",
        "data" => $post
        ]);
    }

thank you for you help .

calisa
  • 175
  • 1
  • 4
  • 17

1 Answers1

1

It seems that your image is not being uploaded to Laravel, How do you sent your image file to Laravel server? If you are using VueJS remember to use Formdata more info here.

If a natural form make sure that your image is being sent through by inspecting dev tools on your browser.

  • i send image with postman , in body part in form-data , i choose file type for images and uploaded it , and in headers part i enter `Content-Type` with `multipart/form-data` value – calisa Apr 18 '21 at 11:40
  • How do you sent your image data on postman, should be like this https://stackoverflow.com/a/39663104/7006983 adding it by choosing the file on postman – John Paul Grefaldo Apr 18 '21 at 11:49
  • Do I have to send data for the image? `images` is just a column for `posts` table – calisa Apr 18 '21 at 11:52
  • 1
    Yes you should, if you are going to save image files. Laravel process image like, sent the image file to Laravel server, then add to local storage then save to the path to database. Try moving the if() code before the $image variable, you'll see that the data sent is invalid. – John Paul Grefaldo Apr 18 '21 at 11:55
  • thank you .. it saved on database.. but when it saved .. the `images` in `data` has nothing . `"data": {"images": {}, }` – calisa Apr 18 '21 at 12:28
  • You have to append the `$imageName` to the `$data` variable maybe like `$data['images'] = $imageName` add that before `Post::create($data)` – John Paul Grefaldo Apr 18 '21 at 12:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/231276/discussion-between-john-paul-grefaldo-and-calisa). – John Paul Grefaldo Apr 18 '21 at 12:50