【问题标题】:Adding new field to Laravel boilerplate user model向 Laravel 样板用户模型添加新字段
【发布时间】:2016-09-26 16:07:49
【问题描述】:

我正在尝试向 laravel 锅炉用户模型添加新字段。 在新鲜的 Laravel 中,它有 App/User.php 我可以使用 $fillable 添加字段。

在样板中它有 app/Models/Access/User/User.php 并且它有 id 为受保护的 protected $guarded = ['id']; 。 还有密码,remember_token 为 $hidden protected $hidden = ['password', 'remember_token']; 所以其他一切都是默认可填充的。

但是当我尝试向用户模型添加新字段时,它并没有添加到数据库表中。

表已经有名为 summery 的字段。

我在 register.blade.php 中添加了summery 字段并尝试检查它是否通过。

return ($request); 添加到App\Service\Access\Trait\RegistersUsers.php

 public function register(RegisterRequest $request)
{
    if (config('access.users.confirm_email')) {
        $user = $this->user->create($request->all());
        event(new UserRegistered($user));
        dd ($request);
      //  return redirect()->route('frontend.index')->withFlashSuccess(trans('exceptions.frontend.auth.confirmation.created_confirm'));
    } else {
        auth()->login($this->user->create($request->all()));
        event(new UserRegistered(access()->user()));
        return redirect($this->redirectPath());
    }
}

然后它返回

POST /register HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.8 Cache-Control: max-age=0 Connection: keep-alive Content-Length: 170 Content-Type: application/x-www-form-urlencoded Cookie: XSRF-TOKEN={{removed}} Host: localhost:8000 Origin: http://localhost:8000 Referer: http://localhost:8000/ Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.94 Safari/537.36 _token=jwLZC59098AaUGFp4pYwv0m3sdEX91BhPvQn43Xq&name=test+user&password=testpass&password_confirmation=testpass&email=testmail%40test.com&summery=test+summery&terms=terms

但不是表没有正确更新。

我的其他表输入和数据检索良好。只有这个模型有问题。

如果您需要更多信息来帮助我,请告诉我。

编辑:

app/Models/Access/User/User.php的来源

<?php

namespace App\Models\Access\User;

use App\Models\Access\User\Traits\UserAccess;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Models\Access\User\Traits\Attribute\UserAttribute;
use App\Models\Access\User\Traits\Relationship\UserRelationship;

/**
 * Class User
 * @package App\Models\Access\User
 */
class User extends Authenticatable
{

    use SoftDeletes, UserAccess, UserAttribute, UserRelationship;



    /**
     * The attributes that are not mass assignable.
     *
     * @var array
     */
    protected $guarded = ['id'];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token'];

    /**
     * @var array
     */
    protected $dates = ['deleted_at'];
}

edit2 : dd ($request);

RegisterRequest {#430 ▼
  #container: Application {#3 ▶}
  #redirector: Redirector {#437 ▶}
  #redirect: null
  #redirectRoute: null
  #redirectAction: null
  #errorBag: "default"
  #dontFlash: array:2 [▼
    0 => "password"
    1 => "password_confirmation"
  ]
  #json: null
  #convertedFiles: []
  #userResolver: Closure {#356 ▶}
  #routeResolver: Closure {#355 ▶}
  +attributes: ParameterBag {#432 ▶}
  +request: ParameterBag {#429 ▶}
  +query: ParameterBag {#431 ▶}
  +server: ServerBag {#435 ▶}
  +files: FileBag {#434 ▶}
  +cookies: ParameterBag {#433 ▶}
  +headers: HeaderBag {#436 ▶}
  #content: "_token=Md4aX1QOSbxjDhLWQStyrKWMW25oKiD1mWU9xhgE&name=Test+User+two&password=testpass&password_confirmation=testpass&email=test%40test.com&summery=test+summery&terms=terms"
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: null
  #requestUri: null
  #baseUrl: null
  #basePath: null
  #method: "POST"
  #format: null
  #session: Store {#386 ▶}
  #locale: null
  #defaultLocale: "en"
}

编辑 3:来自App\Events\Auth\UserRegistered.php

<?php

namespace App\Events\Frontend\Auth;

use App\Events\Event;
use Illuminate\Queue\SerializesModels;

/**
 * Class UserRegistered
 * @package App\Events\Frontend\Auth
 */
class UserRegistered extends Event
{
    use SerializesModels;

    /**
     * @var $user
     */
    public $user;

    /**
     * @param $user
     */
    public function __construct($user)
    {
        $this->user = $user;
    }
}

【问题讨论】:

  • 显示app/Models/Access/User/User.php来源。
  • @Pyton 用源代码更新了线程。 :)
  • 创建后显示dd($user)$this-&gt;user 中有什么。这是什么:App\Models\Access\User\Traits\Attribute\UserAttribute??
  • @Pyton 我添加了 dd ($requst);夏天,不知道如何在这里发布所有内容,我认为$this-&gt;user 来自App\Events\Auth\UserRegistered.php 会有帮助吗?
  • 还有一件事:dump($request-&gt;all());

标签: php mysql laravel post


【解决方案1】:

这是我找到的解决方案, 转到app\repositories\frontend\access\user\EloquentUserRepository.php

你会发现

    public function create(array $data, $provider = false)
{
    if ($provider) {
        $user = User::create([
            'summery' => $data['summery'], // add new field here
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => null,
            'confirmation_code' => md5(uniqid(mt_rand(), true)),
            'confirmed' => 1,
            'status' => 1,
        ]);
    } else {
        $user = User::create([
            'summery' => $data['summery'], // add new field here
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            'confirmation_code' => md5(uniqid(mt_rand(), true)),
            'confirmed' => config('access.users.confirm_email') ? 0 : 1,
            'status' => 1,
        ]);
    }


}

检查我如何将新字段“summery”添加到函数中。添加新字段后,oost 请求会将新字段数据发送到数据库。

【讨论】:

    【解决方案2】:

    当您使用这样的创建方法时,为您的摘要添加一个受保护的字段

    class User extends Authenticatable
    {
    
        use SoftDeletes, UserAccess, UserAttribute, UserRelationship;
    
    
     * The attributes that are mass assignable.
     *
     * @var array
         protected $fillable = ['sumary', 'and other variables'];
    
        /**
         * The attributes that are not mass assignable.
         *
         * @var array
         */
        protected $guarded = ['id'];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = ['password', 'remember_token'];
    
        /**
         * @var array
         */
        protected $dates = ['deleted_at'];
    }
    

    【讨论】:

    • 添加 $fillable 不起作用,我猜这是因为 EloquentUserRepository.php 上的创建函数只接受某些输入
    • 你能把代码粘贴到 EloquentUSerRepository.php 中吗?
    • 请检查所选答案,来自样板 github 页面的 blomdahldaniel 也确认应该从中添加它。
    猜你喜欢
    • 2013-04-22
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-04
    相关资源
    最近更新 更多