0

I was wondering on how to upload an image via API postman in Laravel and save to my database which is phpMyAdmin. When I try to run my code in postman, this is what is showing:

"image null" 

This is currently my code:

Banner CRUD Controller:

    <?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Banner;
use Illuminate\Http\Request;

class BannerCRUDController extends Controller
{
    /**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index()
    {
        $banners = Banner::all();
        return response($banners,200);
    }
 
    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }
 
    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $request->validate([
            'id'=>'required',
            'web_banner_profile'=>'required',
        ]);
 
        $image = $request->file('image');
        if($request->hasFile('image')) {
            $new_name = rand() . '.' . $image->getClientOriginalExtension();
            return response()->json($new_name); 
        }else{
            return response()->json('image null');
        }

        $banners = Banner::create($data);

        return response ($banners, 200);
    }
 
    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }
 
    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }
 
    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
       //
    }
 
    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
       //
    }
 }
 

Banner Model:

    <?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Banner extends Model
{
    public $table = "webinar";
    use HasFactory;

    protected $fillable = [
        'id',
        'web_banner_profile',
    ];
}

Any ideas as to what I've done wrong, I want to get this right now, as I've got a lot of tables I need to create.

Hope someone can help me to get started. Thanks a lot.

Mhoreen
  • 27
  • 7
  • Frankly speaking, nobody cares about Postman (especially, when it is the own API). All I can see is one low quality question and the answers matching these standards. Better post with your own form, then it might appear more serious. This question is just alike "how do I add a file input into a HTML form?" and then even expressing some sense of urgency. I'd suggest to read the instruction manual, to begin with: https://stackoverflow.com/help/asking – Martin Zeitler Jun 17 '21 at 08:51
  • Does this answer your question? [How to upload a file and JSON data in Postman?](https://stackoverflow.com/questions/39037049/how-to-upload-a-file-and-json-data-in-postman) – Martin Zeitler Jun 17 '21 at 08:55

3 Answers3

0

You just use this code to upload an image and in postman select type file

public function store(Request $request)
    {
        $data = $request->validate([
            'id'=>'required',
            'web_banner_profile'=>'required',
        ]);
 
        $image = $request->file('image');
        if($request->hasFile('image')) {
            $image = time() . '.' . $request->image- 
           >getClientOriginalExtension();
           $request->image->move(public_path('Your Path'), $image);
           $banner->image = 'Your Path' . $image;
   
            return response()->json($image); 
        }else{
            return response()->json('image null');
        }

        $banners = Banner::create($data);

        return response ($banners, 200);
    }
0

The error is clear request has empty image. So You should select key type file in postman

enter image description here

enter image description here

John Lobo
  • 14,355
  • 2
  • 10
  • 20
0

Replace store function with below in your BannerCRUDController.php.
Create images directory in storage.

public function store(Request $request)
    {
        $data = $request->validate([
            'id'=>'required',
            'web_banner_profile'=>'required',
        ]);
 
        $image = $request->file('image');
        if($request->hasFile('image')) {
             $image = $request->file('image');
             $destinationPath = storage_path('images');
             $fileName = str_random(6) . time() . "." . $image->getClientOriginalExtension();
             $image->move($destinationPath, $fileName);
             $banner = new Banner();
             $banner->web_banner_profile = $fileName:
             $banner->save()
            return response()->json($fileName); 
        }else{
            return response()->json('image null');
        }

      
    }

Image will be uploaded in storage/images.

divyesh makvana
  • 384
  • 1
  • 5