【发布时间】: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