Laravel:带有接受图像的Passport API

问题描述 投票:0回答:1

所以我已经使用api使用了laravel passport

在我的controller中命名为AuthController.php

我有这个保存数据的工作代码

    public function activity_log(Request $request){

    $validatedData = $request->validate([
        'projCode'=>'required',
        'activity_desc'=>'required',
        'type'=>'required'
    ]);

    $tbl_projectlist = DB::connection('mysql')->select("SELECT * from tbl_projectlist WHERE proj_code = '".$request->projCode."'");

    if(empty($tbl_projectlist))
    {
        return response([
            "status"=>"bad",
            "message"=>"Invalid projCode doesn't exists."
        ]);
    }
    else if($request->type == "REPORT" || $request->type == "ISSUE")
    {
        $ActivityLog = new ActivityLog;
        $ActivityLog->projCode = $request->projCode;
        $ActivityLog->activity_desc = $request->activity_desc;
        $ActivityLog->type = $request->type;
        $ActivityLog->attachment = "/img/default-image.jpg";
        $ActivityLog->created_by_id = Auth::user()->company_id;
        $ActivityLog->created_by_name = Auth::user()->name;
        $ActivityLog->created_at = now();
        $ActivityLog->updated_at = now();
        $ActivityLog->save();

        return response([
            "status"=>"ok",
            "message"=>"Activity successfully submitted!"
        ]);
    }
    else
    {
        return response([
            "status"=>"bad",
            "message"=>"Invalid choose REPORT or ISSUE"
        ]);
    }

}

以及我的api.php

Route::post('/login','Auth\Api\AuthController@login');

Route::middleware('auth:api')->group(function () {

     Some routes...

     Route::post('/activity_log','Auth\Api\AuthController@activity_log');
});

到目前为止,我只是存储img所在的图像hard coded的文件路径。

我想做的是接受img文件并保存到我的文件夹并将文件路径存储在我的数据库中

我正在使用邮递员以此来测试我的api

enter image description here


enter image description here

php laravel laravel-passport
1个回答
0
投票
从邮递员:使用POST方法,选择正文和表单数据,选择文件并使用图像作为键,然后从需要上传的值中选择文件。

public function uploadTest(Request $request) { if(!$request->hasFile('image')) { return response()->json(['upload_file_not_found'], 400); } $file = $request->file('image'); if(!$file->isValid()) { return response()->json(['invalid_file_upload'], 400); } $path = public_path() . '/uploads/images/store/'; $file->move($path, $file->getClientOriginalName()); return response()->json(compact('path')); }

© www.soinside.com 2019 - 2024. All rights reserved.