【问题标题】:Laravel 4 how to submit form data to different tablesLaravel 4 如何将表单数据提交到不同的表
【发布时间】:2013-12-15 00:33:47
【问题描述】:

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

许可模式

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();
                }

【问题讨论】:

    标签: php mysql database laravel-4


    【解决方案1】:

    不确定您是如何创建User 的,并且在创建新用户时,您是否已经在user 模型中为License 做过任何事情。你说你在users表中有一个type字段,有两种类型(receptionistdentist),如果类型是dentist,那么他们需要提供license数据,这些数据将保存在在更新user 期间到license 表所以,在你的控制器中,你现在就拥有了这个

    $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 模型和关系的更多信息,因此这是您创建 License 模型的选项,具体取决于关于用户类型。

    【讨论】:

    • 感谢您的建议,我更新了代码以显示我的用户模型和您的建议。我收到一个未知的列错误。当我在保存方法 !$user->license->temp_id 中删除这行代码时,信息保存得很好。但是,当我尝试更新信息时,会为该用户创建一条新记录,但它没有更新......错误在 where 子句中显示 Unknown column 'temps_license.user_id。我认为它正在 temp_license 表中寻找 user_id 列,但没有。我通过 temp_id 关联这两个表。我可以删除它并添加 user_id @RCV
    • 我已经删除了&& !$user->license->temp_id
    • 我删除了 && !$user->license->temp_id ,但我仍然收到未知列错误。我修改了控制器,我在上面更新了我的代码。这似乎效果更好,我没有收到任何错误,除了当我返回并更改到期日期时,我会为同一个用户获得两个条目,一个显示上一个日期,另一个显示新日期。 @RCV
    • 没听懂,应该更新,因为你用的是:find($id),然后是save(),你说的返回是什么意思?
    • 它确实有效,但是当我更新许可证信息时,它会在数据库中创建一个重复的条目......所以如果 user_id 3 输入他们的许可证信息,它就可以保存得很好。但是,如果 user_I'd 3 更新他们的许可证到期日期,那么数据库中有两个不同的条目用于 usrr_id 3。我认为这是因为我使用的是 $license = new license(); @RCV
    猜你喜欢
    • 2020-12-04
    • 1970-01-01
    • 2017-02-23
    • 2016-06-14
    • 1970-01-01
    • 2023-03-28
    • 2020-06-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多