【问题标题】:I seems cant to save my Uploaded file to database LARAVEL?我似乎无法将上传的文件保存到数据库 LARAVEL?
【发布时间】:2019-03-09 08:55:06
【问题描述】:

你好我试图将我的文件名保存在数据库 laravel 中,但它给了我 我正在用 laravel 上传图片。图片已上传,但我的租金数据库中的字段未上传。这就是发生的事情。

Creating default object from empty value

我正在使用存储:链接

控制器 使用 Illuminate\Http\Request; 使用 Illuminate\Support\Facades\Storage;

public function insertproof(Request $request)
    $id = $request['id'];
    if ($request->hasFile('rent_proof')){
        $filepath = $request->file('rent_proof')->store('app/folder_proof');
    }
    $result = rent::find($id);
    $result->rent_proof=$filepath;
    $result->save();
}

这是我的观点,这个观点来自mymodal

<form method="POST" action="{{route('visitor_rent.insertproof')}}"
    enctype="multipart/form-data">

    {{ csrf_field() }}
    <div class="form-group">
        <input type="file" class="form-control" name="rent_proof"  id="proof">
    </div>
    <button type="submit" class="btn btn-success">Save</button>
</form>

这是我的模特

public class Rent extends Model
{

    protected $primaryKey = 'id';
    protected $fillable = ['rent_proof'];
}

编辑: 你好这个给我的错误

    $result = rent::find($id);
    $result->rent_proof=$filepath; //<--this line is giving me the error
    $result->save();        

感谢关心 有什么我想念的吗?

版本 laravel 5.5 PHP 7.2

【问题讨论】:

  • 如果你的 -1 帮助你让你的日子变得更好,那很好。我仍然是一个贵族,我需要指导,这很好-1我,但至少试着告诉我我的错是什么。这是同样的情况stackoverflow.com/questions/43488754/… 但仍然没有回答,我需要有人指出我缺少的东西,即使你根本不提供任何代码我也可以继续......
  • 有人有你的问题-1,因为问题不明显。现在告诉我你到底要达到什么目标,确切的错误是什么。我会重新编辑您的问题,因为社区可以了解真实情况。我正在努力提供帮助,但我不是那个 -1 你的问题的人:)
  • 你好先生,是的,我发现我没有绑定 whoops 抛出的错误代码,感谢反馈,它让我成为一个更好的人
  • 在 Laravel 异常跟踪中,它为您提供了引发错误的确切行号。
  • $result = rent::find($id); ,看起来 $resultNULL ,你确定表中存在 $id 的值吗?

标签: php laravel laravel-5


【解决方案1】:

Creating default object from empty value这个错误是因为你传递的id在你的数据库中不存在。

如果您正在创建带有 id 和图像路径的新记录

那么你应该使用

$result = new Rent;
$result->rent_proof=$filepath; //<--this line is giving me the error
$result->save();

如果您要更新现有的,则传递数据库中存在的正确 id

【讨论】:

  • 您好,感谢您的反馈!错误来自 $id = $request['id'];因为我使用 javascript 将数据传输到 myModal 但它返回 null,所以我让它工作,对于迟到的回复和新手错误感到抱歉。谢谢和GBU!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-27
  • 2012-07-21
  • 1970-01-01
  • 2017-04-18
  • 2012-11-02
  • 2020-05-12
  • 1970-01-01
相关资源
最近更新 更多