laravel-5.4 - error :Creating default object from empty value(laravel-5.4 - 错误:从空值创建默认对象)
问题描述
我想在数据库中存储图像路径.vendorlaravelframeworksrcIlluminateFoundationAuthRegistersUsers.php
下的我的控制器代码如下:
I want to store image path in database. My Controller code undervendorlaravelframeworksrcIlluminateFoundationAuthRegistersUsers.php
follows:
public function register(Request $request)
{
$this->validator($request->all())->validate();
if($request->hasFile('image')) {
$image_name = $request->file('image')->getClientOriginalName();
$image_path = $request->file('image')->store('public');
$user->image = Storage::url($image_name);
$user->save();
}
event(new Registered($user = $this->create($request->all())));
// $this->guard()->login($user); disable autologin for new users
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
我收到以下错误:RegistersUsers.php 第 40 行中的 ErrorException:从空值创建默认对象 .
I am getting the following error: ErrorException in RegistersUsers.php line 40: Creating default object from empty value .
图像正在保存在公共文件夹中,但无法将图像路径保存在用户表数据库中.用户表的架构Schema::create('users', function (Blueprint $table) {$table->increments('user_id');$table->string('name');$table->string('image');$table->rememberToken();$table->timestamps();
The image is getting saved in public folder but not able to save image path in the user table database. schema of user table
Schema::create('users', function (Blueprint $table) {
$table->increments('user_id');
$table->string('name');
$table->string('image');
$table->rememberToken();
$table->timestamps();
如何将图像路径存储在数据库中的用户表中
how to proceed to store the image path in the user table in database
推荐答案
你还没有初始化变量 $user
所以在控制器中,在使用它之前你必须添加 $user = new User;
假设你已经有 use AppUser;
you haven't initialized the variable $user
so in the controller and before using it you've to add $user = new User;
assuming that you already have use AppUser;
或 $user = new AppUser;
如果你不这样做.
or $user = new AppUser;
if you don't.
更新如果您尝试更新现有的用户记录,您必须通过根据应在请求中发送的主键进行选择来初始化 $user
变量,然后进行如下初始化:>
Update
if you're trying to update an existing user record you've to initialize the $user
variable by selecting based on it's primary key which should be sent in the request and then doing the initialization like:
$user_id = $request->input('user_id');
$user = User::find($user_id);
这篇关于laravel-5.4 - 错误:从空值创建默认对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:laravel-5.4 - 错误:从空值创建默认对象


- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01