Laravel 4如何将表单数据提交到不同的表

时间:2013-11-28 21:44:13

标签: php mysql database laravel-4

我有一个编辑表单,其中表单的大部分数据将发布到users表。 对于需要许可证的用户,他们的信息将发布到许可证表中,许可证字段仅对相应的用户类型可见。 I.E如果用户类型是接待员,那么他们将不会看到许可证字段,但如果用户类型是牙医,则字段将显示等。我在许可证表中具有user_id,以便我可以将用户与许可证相关联。我建立了一个名为license的新的雄辩模型,我的大部分工作都在更新控制器中完成.........

许可证模型

class License extends Eloquent {


    protected  $table = 'temps_license';

    public function user()
    {
        return $this->belongsTo('User');
    }



}

控制器

public function update($id)
{

    // validate
    // read more on validation at http://laravel.com/docs/validation
    $rules = array(
        'firstname' => 'required|min:3|alpha',
        'lastname' => 'required|min:3|alpha',
        'zipcode' => 'required|min:5|numeric',
        'temp_experience' => 'required|min:1|max:50|numeric',
        'temp_travel' =>   'required|numeric',
        'temp_hourly_rate' => 'required|numeric|min:10'
    );
    $validator = Validator::make(Input::all(), $rules);


    // process the login
    if ($validator->fails()) {
        return Redirect::to('user/profile/' . $id . '/edit')
            ->withErrors($validator)
            ->withInput();
    } else {
        $software_checked = Input::get('software');
        if(is_array($software_checked))
        {
            $imploded_software = implode(',', $software_checked);
        }


        // store
        $user = User::find($id);

        $user->firstname       = Input::get('firstname');
        $user->lastname      = Input::get('lastname');
        $user->zipcode = Input::get('zipcode');
        $user->temp_experience = Input::get('temp_experience');
        $user->temp_travel = Input::get('temp_travel');
        $user->temp_hourly_rate = Input::get('temp_hourly_rate');
        $user->temp_software_experience = $imploded_software;
        if( $user->save() ) {
            if( $user->usertype == 'dental hygienist' && !$user->license->temp_id ) {
                $license = new License();
                $license->temp_id = $id;
                $license->license_expiration_date = Input::get('expiration_date');

                $license->save();
            }

                // redirect
                Session::flash('message', 'Successfully updated profile!!');
                return Redirect::to('user/profile/'.$id.'');
        }
    }

}

用户模型

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 e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }


    public static function getTemps()
    {

       // Create a array of allowed types.
        $types = array('dental hygienist', 'dentist', 'dental assistance');

        // Get what type the user selected.
        $type = Input::get('temptype');

        //Get user location
        //$location = Input::get('zipcode');


        // Make sure it is a valid type.
        if(!in_array($type, $types))
        {
            return App::abort(500, "Invaild temptype.");
        }

      $temps =  DB::table('users')
            ->join('availability', 'users.id', '=', 'availability.userid')
            ->select('users.id', 'users.firstname', 'users.lastname', 'users.zipcode', 'users.salary', 'availability.dateavailable')
            ->where('usertype', $type)
           //->where('zipcode', $location)
            ->get();

        return $temps;
    }

    public function license()
    {
        return $this->hasOne('License');
    }

修改后的控制器

这样做效果更好,但每当我更新用户的到期日期,状态或许可证号码时,它都可以正常运行并更新,但数据库中有重复的条目......

if( $user->usertype == 'dental hygienist' || $user->usertype == 'dentist'  && !$user->license->temp_id ) {
                    $license = new License();
                    $license->user_id = $id;
                    $license->temp_id = $id;
                    $license->license_expiration_date = Input::get('expiration_date');
                    $license->license_number = Input::get('license_number');
                    $license->license_state = Input::get('license_state');

                    $license->save();
                }

1 个答案:

答案 0 :(得分:0)

不确定如何创建User并且在创建新用户时,您已经在License模型中为user做了任何事情,无论如何。您说type表中有users字段,有两种类型(receptionist dentist),如果类型为dentist,则需要提供{在更新license期间,{1}}数据和这些数据将保存到license表中,因此,在您的控制器中,您现在拥有此功能

user

在这种情况下,我相信您将$user = User::find($id); 字段设为type,以确定您可以使用的用户类型

$user->type

但是,在您的if( $user->type == 'dentist' ) { // save license data } 模型中,您应该声明与User的关系为

license

在您需要的class User extends Eloquent { // ... public function license() { return $this->hasOne('Lisense'); } } 模型中

license

所以,你的代码应该是这样的

class License extends Eloquent {
    protected  $table = 'temps_license';
    // ...
    public function user()
    {
        return $this->belongsTo('User');
    }
}

还有其他方法可以使用$user = User::find($id); $user->firstname = Input::get('firstname'); $user->lastname = Input::get('lastname'); // more ... if( $user->save() ) { if( $user->type == 'dentist' ) { $license = new License(); $license->temp_id = $id; $license->license_expiration_date = Input::get('expiration_date'); // ... $license->save(); } Session::flash('message', 'Successfully updated profile!!'); return Redirect::to('user/profile/'.$id.''); } 代替push保存相关模型,但不了解您的save模型和关系的更多信息,因此这是您的选择根据用户类型创建User模型。