dongzhuo3059 2019-05-15 10:31
浏览 25
已采纳

在Laravel注册期间自动归档字段

Welcome. I have been learning Laravela for a few days and stopped at registration. I have migration:

Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name', 120);
        $table->string('surname', 120);
        $table->string('email', 120)->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->bigInteger('counter')->default(0);
        $table->string('url_address', 160);
        $table->string('ip', 25);
        $table->dateTime('date_of_registration');
        $table->bigInteger('company_id')->unsigned();
        $table->boolean('isCompany')->default(0);
        $table->boolean('isMailing')->default(0);
        $table->text('note');
        $table->string('nip1', 12);
        $table->string('business1', 120);
        $table->string('phone1', 60);
        $table->string('street1', 150);
        $table->string('number1', 8);
        $table->string('postal_code1', 12);
        $table->string('city1', 100);
        $table->bigInteger('country_id1')->default(0);
        $table->bigInteger('provincial_id1')->default(0);
        $table->string('nip2', 12);
        $table->string('business2', 120);
        $table->string('phone2', 60);
        $table->string('street2', 150);
        $table->string('number2', 8);
        $table->string('postal_code2', 12);
        $table->string('city2', 100);
        $table->bigInteger('country_id2')->default(0);
        $table->bigInteger('provincial_id2')->default(0);
        $table->string('nip3', 12);
        $table->string('business3', 120);
        $table->string('phone3', 60);
        $table->string('street3', 150);
        $table->string('number3', 8);
        $table->string('postal_code3', 12);
        $table->string('city3', 100);
        $table->bigInteger('country_id3')->default(0);
        $table->bigInteger('provincial_id3')->default(0);
        $table->decimal('cash', 9, 2)->default(0);
        $table->rememberToken();
        $table->timestamps();
        $table->engine = "InnoDB";
    });

and functions:

function generateSeoUrl(string $string): string
{
    $string = preg_replace("/ +/", "-", trim($string));
    $string = mb_strtolower(preg_replace('/[^A-Za-z0-9-]+/', '', $string), 'UTF-8');
    return $string;
}

The above table uses Laravel's default authorization (I use Laravel 5.8).

How can I add during registration:

- ip - IP address of the user who registered
- date_of_registration - current date
- url_address - here I would like to save the generatedSeoUrl result ($ email)

Please help. I've been sitting here for hours and I do not know how to deal with it :(

  • 写回答

3条回答 默认 最新

  • dongmao3131 2019-05-15 11:51
    关注

    Step: 1 :

    Add the extra Fileds to the users table migration

    You have done already

    Step: 2

    Add the fileds to the fillable model of User.php

    <?php
    
    namespace App;
    
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Contracts\Auth\MustVerifyEmail;
    use Illuminate\Foundation\Auth\User as Authenticatable;
    
    /**
     * @property integer $id
     * @property string $name
     * @property string $surname
     * @property string $email
     * @property string $email_verified_at
     * @property string $password
     * @property integer $counter
     * @property string $url_address
     * @property string $ip
     * @property string $date_of_registration
     * @property integer $company_id
     * @property boolean $isCompany
     * @property boolean $isMailing
     * @property string $note
     * @property string $nip1
     * @property string $business1
     * @property string $phone1
     * @property string $street1
     * @property string $number1
     * @property string $postal_code1
     * @property string $city1
     * @property integer $country_id1
     * @property integer $provincial_id1
     * @property string $nip2
     * @property string $business2
     * @property string $phone2
     * @property string $street2
     * @property string $number2
     * @property string $postal_code2
     * @property string $city2
     * @property integer $country_id2
     * @property integer $provincial_id2
     * @property string $nip3
     * @property string $business3
     * @property string $phone3
     * @property string $street3
     * @property string $number3
     * @property string $postal_code3
     * @property string $city3
     * @property integer $country_id3
     * @property integer $provincial_id3
     * @property float $cash
     * @property string $remember_token
     * @property string $created_at
     * @property string $updated_at
     */
    class User extends Authenticatable
    {
        use Notifiable;
    
        /**
         * @var array
         */
        protected $fillable = ['name', 'surname', 'email', 'email_verified_at', 'password', 'counter', 'url_address', 'ip', 'date_of_registration', 'company_id', 'isCompany', 'isMailing', 'note', 'nip1', 'business1', 'phone1', 'street1', 'number1', 'postal_code1', 'city1', 'country_id1', 'provincial_id1', 'nip2', 'business2', 'phone2', 'street2', 'number2', 'postal_code2', 'city2', 'country_id2', 'provincial_id2', 'nip3', 'business3', 'phone3', 'street3', 'number3', 'postal_code3', 'city3', 'country_id3', 'provincial_id3', 'cash', 'remember_token', 'created_at', 'updated_at'];
    
        /**
         * The attributes that should be hidden for arrays.
         *
         * @var array
         */
        protected $hidden = [
            'password', 'remember_token',
        ];
    
        /**
         * The attributes that should be cast to native types.
         *
         * @var array
         */
        protected $casts = [
            'email_verified_at' => 'datetime',
        ];
    
    }
    

    Step:3

    Add the fileds in the registration form can be found in projectnameesources\views\authegister.blade.php

    Add the required filed to the form

    Step: 4

    Now the Logic part

    Open the file project\app\Http\Controllers\Auth\RegisterController.php

    Now You will find the validator and create method

    Add your method to the RegisterController.php

    function generateSeoUrl(string $string): string
        {
            $string = preg_replace("/ +/", "-", trim($string));
            $string = mb_strtolower(preg_replace('/[^A-Za-z0-9-]+/', '', $string), 'UTF-8');
            return $string;
        }
    

    Add your validation logic in validator method

    /**
         * Get a validator for an incoming registration request.
         *
         * @param  array  $data
         * @return \Illuminate\Contracts\Validation\Validator
         */
        protected function validator(array $data)
        {
            return Validator::make($data, [
                'name' => ['required', 'string', 'max:255'],
                'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
                'password' => ['required', 'string', 'min:8', 'confirmed'],
            ]);
        }
    

    Now we need to manually add some fileds while storing

    /**
         * Create a new user instance after a valid registration.
         *
         * @param  array  $data
         * @return \App\User
         */
        protected function create(array $data)
        {
            $newArray = [
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => Hash::make($data['password']),
                'ip' => RequestFacade::ip(),
                'date_of_registration' => now(),
                'url_address' => $this->generateSeoUrl($data['email']),
            ];
            return User::create($newArray);
        }
    

    Finally Your RegisterController will look like

    <?php
    
    namespace App\Http\Controllers\Auth;
    
    use App\User;
    use App\Http\Controllers\Controller;
    use Illuminate\Support\Facades\Hash;
    use Illuminate\Support\Facades\Validator;
    use Illuminate\Foundation\Auth\RegistersUsers;
    use Illuminate\Http\Request as HttpRequest;
    use Illuminate\Support\Facades\Request as RequestFacade;
    
    class RegisterController extends Controller
    {
        /*
        |--------------------------------------------------------------------------
        | Register Controller
        |--------------------------------------------------------------------------
        |
        | This controller handles the registration of new users as well as their
        | validation and creation. By default this controller uses a trait to
        | provide this functionality without requiring any additional code.
        |
        */
    
        use RegistersUsers;
    
        /**
         * Where to redirect users after registration.
         *
         * @var string
         */
        protected $redirectTo = '/home';
    
        /**
         * Create a new controller instance.
         *
         * @return void
         */
        public function __construct()
        {
            $this->middleware('guest');
        }
    
        /**
         * Get a validator for an incoming registration request.
         *
         * @param  array  $data
         * @return \Illuminate\Contracts\Validation\Validator
         */
        protected function validator(array $data)
        {
            return Validator::make($data, [
                'name' => ['required', 'string', 'max:255'],
                'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
                'password' => ['required', 'string', 'min:8', 'confirmed'],
            ]);
        }
    
        /**
         * Create a new user instance after a valid registration.
         *
         * @param  array  $data
         * @return \App\User
         */
        protected function create(array $data)
        {
            $newArray = [
                'name' => $data['name'],
                'email' => $data['email'],
                'password' => Hash::make($data['password']),
                'ip' => RequestFacade::ip(),
                'date_of_registration' => now(),
                'url_address' => $this->generateSeoUrl($data['email']),
            ];
            return User::create($newArray);
        }
    
        function generateSeoUrl(string $string): string
        {
            $string = preg_replace("/ +/", "-", trim($string));
            $string = mb_strtolower(preg_replace('/[^A-Za-z0-9-]+/', '', $string), 'UTF-8');
            return $string;
        }
    }
    

    Please do comment if there is any issue

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码