向Laravel样板用户模型添加新字段

时间:2016-05-28 11:22:50

标签: php mysql laravel post

我试图将新字段添加到laravel锅炉用户模型中。 在新鲜的Laravel中它有if value == "value1" 我可以使用$ fillable添加字段。

在样板中

它有App/User.php并且它有 id为守卫app/Models/Access/User/User.php。 也是密码,remember_token为$ hidden protected $guarded = ['id']; 所以其他一切都是默认的可填写的。

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

表已经有一个名为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

edit2:dd($ request);

<?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'];
}

编辑3:来自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" }

的来源
App\Events\Auth\UserRegistered.php

2 个答案:

答案 0 :(得分:0)

以下是我发现的解决方案, 转到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,
        ]);
    }


}

检查我如何添加新字段&#39; summery&#39;在添加新字段后,oost请求会向数据库发送新的字段数据。

答案 1 :(得分:0)

使用类似

的创建方法时,为摘要添加受保护的字段
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'];
}