Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel 4如何将表单数据提交到不同的表_Php_Mysql_Database_Laravel 4 - Fatal编程技术网

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

Php Laravel 4如何将表单数据提交到不同的表,php,mysql,database,laravel-4,Php,Mysql,Database,Laravel 4,我有一个编辑表单,其中表单的大部分数据将发布到用户表。 对于需要许可证的用户,其信息将发布到许可证表中,许可证字段仅对相应的用户类型可见。也就是说,如果用户类型是接待员,他们将看不到许可证字段,但如果用户类型是牙医,则字段将显示等。我在许可证表中有一个用户id,以便我可以将用户与许可证关联。我建立了一个新的雄辩模型,名为许可证。我的大部分工作都是在更新控制器中完成的 许可证型号 class License extends Eloquent { protected $table =

我有一个编辑表单,其中表单的大部分数据将发布到用户表。 对于需要许可证的用户,其信息将发布到许可证表中,许可证字段仅对相应的用户类型可见。也就是说,如果用户类型是接待员,他们将看不到许可证字段,但如果用户类型是牙医,则字段将显示等。我在许可证表中有一个用户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');
    }
修改控制器

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.'');
        }
    }

}
这工作得更好,但每当我更新用户的到期日期、状态或许可证号时,它就会工作并更新,但数据库中有重复的条目

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

不确定您是如何创建
User
的,并且在创建新用户时,您是否已经在
User
模型中为
License
做了任何事情。您说您在
用户
表中有一个
类型
字段,有两种类型(
接待员
牙医),如果类型是
牙医
,则他们需要提供
许可证
数据,这些数据将在
用户
更新期间保存在
许可证
表中,在你的控制器中,你现在就有了这个

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

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

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

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
来保存相关模型,但是不知道更多关于
用户的
模型和关系的信息,因此这是您根据用户类型创建
许可证
模型的一个选项。

谢谢您的建议,我更新了代码以显示我的用户模型和您的建议。我得到一个未知列错误。当我在save方法中删除这行代码时$用户->许可->临时id信息保存得很好。但是,当我尝试更新信息时,会为该用户创建一条新记录,但不会更新它。。。。错误在where子句中显示未知列“temps_license.user_id”。我认为它正在temp_license表中查找user_id列,但没有。我通过temp_id将这两个表关联起来。我可以删除它并添加user_id@RCVI'veremove
&&$用户->许可->临时id
我删除了&&$用户->许可->临时id,我仍然得到未知列错误。我修改了控制器,更新了上面的代码。这似乎工作得更好,我没有收到任何错误,除了当我返回并更改过期日期时,我得到同一用户的两个条目,一个显示以前的日期,另一个显示新的日期@RCVI没有得到它,它应该更新,因为您正在使用
:find($id)
,然后使用
save()
,返回是什么意思?它确实可以工作,但当我更新许可证信息时,它会在数据库中创建一个重复条目。。。。。。因此,如果用户_id 3输入他们的许可证信息,它就可以很好地保存。但是如果用户3更新了他们的许可证到期日期,那么usrr\U id 3的数据库中就有两个不同的条目。我想这是因为我正在使用$license=newlicense()@RCV