Laravel 4将用户保存到数据库中无法正常工作

时间:2014-04-27 17:46:25

标签: laravel-4

我想将用户的注册数据保存到数据库中,没有电子邮件地址,每个数据都已成功保存。

我真的不知道may代码有什么问题,因为我没有收到Laravel的任何错误或警告反馈消息。

这是我的功能

 public function doSignUp()
  {
      $rules = array(
          'email' => 'required|email|unique:users',
          'password' => 'required|confirmed|min:5',
          'first_name' =>'required',
          'last_name' =>'required',
       );

      $messages = array(
                  'email.email' => 'Please enter a valid E-mail address.',
                  'email.required' => 'E-mail address is required.',
                  'first_name.required' => 'First Name is required.',
                  'last_name.required' => 'Last Name is required.',
                  'password.required' => 'Password is required.',
                  'password.confirmed' => 'Password does not match the confirm password.',
                  'password.min' => 'Your password is too short! Min 5 symbols.',
                  'email.unique' => 'This E-mail has already been taken.',
              );

      $validator = Validator::make(Input::all(), $rules);

      if ($validator->fails()) 
      {
        return Redirect::to('showSignUp')
          ->withErrors($validator) 
          ->withInput(Input::except('password')); 
      }
      else
      {
          $user     = new User();
          $user->email = Input::get('email');
          //print Input::get('email'); //Not empty
          $user->password = Hash::make(Input::get('password'));
          $user->first_name = Input::get('first_name');
          $user->last_name = Input::get('last_name');
          $user->created_at = date('Y-m-d H:i:s');
          $user->status = 0;

          if($user->save())
          {            
            // Create the mail transport configuration
            $transport = Swift_MailTransport::newInstance(); 
            // Create the message
            $message = Swift_Message::newInstance();
            $message->setTo(array(
             Input::get('email')
            ));
            $message->setSubject('Welcome a.a');
            $message->setBody('Welcome, a.a');
            $message->setFrom('a@a.a','a.a');
            // Send the email
            $mailer = Swift_Mailer::newInstance($transport);
            $mailer->send($message);
             return Redirect::action('UserController@showLogin')->with('success', 'Your account is ready, you can login.');
          }
      }
  }

用户模型

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the token value for the "remember me" session.
     *
     * @return string
     */
    public function getRememberToken()
    {
        return $this->remember_token;
    }

    /**
     * Set the token value for the "remember me" session.
     *
     * @param  string  $value
     * @return void
     */
    public function setRememberToken($value)
    {
        $this->remember_token = $value;
    }

    /**
     * Get the column name for the "remember me" token.
     *
     * @return string
     */
    public function getRememberTokenName()
    {
        return 'remember_token';
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

    public function isAdmin()
     {
        return $this->superuser == 1 ? TRUE : FALSE;
     }
}

1 个答案:

答案 0 :(得分:2)

添加protected $ fillable = array(/ * model properties * /);

允许“mass assignement