Laravel 4将用户保存到数据库中无法正常工作 [英] Laravel 4 saving user into Database not working

查看:72
本文介绍了Laravel 4将用户保存到数据库中无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to save user's registration data into database, without email address each data has been saved successfully.

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

I really don't know what is wrong with may code because i don't get any error or warning feedback message from 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;
     }
}

推荐答案

添加受保护的$ fillable = array(/*模型属性*/);

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

它允许`质量分配

这篇关于Laravel 4将用户保存到数据库中无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆