【问题标题】:here my code and i want to save image url in database i am using laravel my question is how i save image url in database这是我的代码,我想将图像 url 保存在数据库中我正在使用 laravel 我的问题是我如何将图像 url 保存在数据库中
【发布时间】:2021-03-16 03:52:15
【问题描述】:

这是我的代码,我想将图像 url 保存在数据库中我正在使用 laravel 我的问题是我如何将图像 url 保存在数据库中

公共函数保存(请求$req) { if(request()->hasFile('photo')){

        $path = base_path() . '/public/user-uploads/employee-docs/';
        $repath = '/public/user-uploads/employee-docs/'.request('project');


        if (!file_exists($path))
        {
            mkdir($path);
        }
    $path = $path.'/'.request('project');
    if (!file_exists($path))
        {
            mkdir($path);
        }

         $name = Carbon::now()->format('Y-m-d-H-i-s_u');

        $file1 = request()->file('photo');
        if($file1->isValid()) {
            $file1->move($path, $name.'.'.$file1->getClientOriginalExtension());
            $file1_url = $repath.'/'.$name.'.'.$file1->getClientOriginalExtension();
            $photo = new attendance;
        $photo ->user_id=$req->user_id;
        $photo ->image_url=$req->image_url;
            return ['status'=>1, 'data'=>$file1_url];
        } else{
            return ['status'=>0, 'data'=>'Invalid image'];

        }

    } else{
        
        return ['status'=>0, 'data'=>'There is no image'];
        
    }
}   

【问题讨论】:

  • 您没有保存对象并为图像 url 使用了错误的值。您是否收到任何特定错误??
  • “消息”:“发生未知错误”,

标签: laravel


【解决方案1】:

看起来您还没有保存对象 $photo

$photo = new attendance;
$photo->user_id=$req->user_id;
$photo->image_url=$req->image_url;
$photo->save() // add this line

【讨论】:

    猜你喜欢
    • 2014-03-01
    • 2017-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-01
    相关资源
    最近更新 更多