2
votes

I'm trying to add new field to laravel boiler user model. in the fresh Laravel it have App/User.php where I can add field using $fillable .

in the boilerplate it have app/Models/Access/User/User.php and it have id as guarded protected $guarded = ['id']; . also password, remember_token as $hidden protected $hidden = ['password', 'remember_token']; So everything else would be default fillable.

But When I'm trying to add new field to user model , it's not adding to the database table.

table already have field called summery .

I've added summery field to the register.blade.php and tried to check if it's passing.

added return ($request); to the 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());
    }
}

and it returns

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

but not table is not updated correctly .

my other tables inputs and data retrieving well . only have problem with this model.

please let me know if you need more information for helping me on this .

Edit :

Source of 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"
}

Edit 3 : source from 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;
    }
}
2
Show app/Models/Access/User/User.php source.Pyton
@Pyton updated thread with the source code . :)Ruwan Ranganath
Show dd($user) after create. And what is in $this->user. And what is this: App\Models\Access\User\Traits\Attribute\UserAttribute ??Pyton
@Pyton I added dd ($requst); summery , not sure how I can post all of it here, and I think $this->user is from App\Events\Auth\UserRegistered.php will those helpful ?Ruwan Ranganath
One more thing: dump($request->all());Pyton

2 Answers

0
votes

Here is the solution I've found , goto app\repositories\frontend\access\user\EloquentUserRepository.php

you will find

    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,
        ]);
    }


}

check how i added new field 'summery' to the function.after you add new field there oost request will send new field data to the database.

0
votes

Add a protected field for your summary when you are using a create method like so

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