Моя специальная настройка добавляет и редактирует на одной странице. Все работает Но есть одна проблема. Когда я редактирую все, это не сохраняет то, что я редактировал. Я не понимаю почему. У специалиста есть таблица один к одному (profile_settings).
Настройки профиля Миграция
Schema::create('profile__settings', function (Blueprint $table) {
$table->id();
$table->integer('user_id')->unsigned();
$table->string('first_name')->nullable();
$table->string('last_name')->nullable();
$table->string('phone_number')->nullable();
$table->string('gender')->nullable();
$table->string('date_of_birth')->nullable();
$table->text('about_me')->nullable();
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('country')->nullable();
$table->string('postal_code')->nullable();
$table->timestamps();
});
Profile_Settings Модель
class Profile_Settings extends Model
{
// Fill in db
protected $fillable = [
'first_name', 'last_name', 'phone_number',
'gender', 'date_of_birth', 'about_me',
'address', 'city', 'country', 'postal_code',
];
// Profile settigns model belongs to User
public function user(){
return $this->belongsTo(User::class);
}
}
Модель пользователя (Специалист)
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public static function countPercent($count, $maxCount){
//one percent
$one_percent = $maxCount / 100;
// $count how much is percent
$percent = $count / $one_percent;
return $percent;
}
// 1 User have 1 profile settings (ONE TO ONE)
public function profile_settings(){
return $this->hasOne(Profile_Settings::class);
}
}
SpecialistDashboardController
class SpecialistDashboardController extends Controller
{
public function dashboard(){
return view('Frontend.specialists.dashboard.index');
}
public function profile_settings(){
$specialist = Auth::user();
$data = Auth::user()->profile_settings;
return view('Frontend.specialists.profile_settings.index', compact('specialist', 'data'));
}
public function profile_settings_post(Request $request){
// Auth Specialist
$user = Auth::user();
// Data Specialist Validate
$data = $request->validate([
'first_name' => 'nullable|string',
'last_name' => 'nullable|string',
'phone_number' => 'nullable|integer',
'gender' => 'nullable',
'date_of_birth' => 'nullable',
'about_me' => 'nullable',
'address' => 'nullable',
'city' => 'nullable|string',
'country' => 'nullable|string',
'postal_code' => 'nullable|integer',
]);
// If this Specialist no have profile settings data (crate new table in DB)
if (is_null($user->profile_settings())){
$new_profile_settings = new Profile_Settings($data);
$user->profile_settings()->save($new_profile_settings);
}
// Else have this Specialist profile settings data (edit and update this table in DB)
else{
$user->profile_settings()->update($data);
}
// RETURN REDIRECT PROFILE SETTINGS INDEX
return redirect()->route('frontend.specialist.profile.settings');
}
}
Маршруты
// Front End Routes
Route::group([
'namespace' => 'landing',
], function (){
//LANDING ROUTS
Route::get('/', 'PagesController@index')->name('landing.index');
//SPECIALIST ROUTS
Route::get('/specialist', 'SpecialistDashboardController@dashboard')->name('frontend.specialist.dashboard');
Route::get('/specialist/profile-settings', 'SpecialistDashboardController@profile_settings')->name('frontend.specialist.profile.settings');
Route::post('/specialist/profile-settings', 'SpecialistDashboardController@profile_settings_post')->name('frontend.specialist.profile.settings.post');
Route::get('/logout', '\App\Http\Controllers\Auth\LoginController@logout');
});
Форма лезвия
<form action="{{route('frontend.specialist.profile.settings.post')}}" method="POST">
@csrf
<!-- Basic Information -->
<div class="card">
<div class="card-body">
<h4 class="card-title">Basic Information</h4>
<div class="row form-row">
{{-- Image --}}
<div class="col-md-12">
<div class="form-group">
<div class="change-avatar">
<div class="profile-img">
<img src="/frontend/img/doctors/doctor-thumb-02.jpg" alt="User Image">
</div>
<div class="upload-img">
<div class="change-photo-btn">
<span><i class="fa fa-upload"></i> Upload Photo</span>
<input type="file" class="upload">
</div>
<small class="form-text text-muted">Allowed JPG, GIF or PNG. Max size of 2MB</small>
</div>
</div>
</div>
</div>
{{-- Username --}}
<div class="col-md-6">
<div class="form-group">
<label>Username<span class="text-danger">*</span></label>
<input type="text" class="form-control" readonly placeholder="{{$specialist->name}}">
</div>
</div>
{{-- Email --}}
<div class="col-md-6">
<div class="form-group">
<label>Email <span class="text-danger">*</span></label>
<input type="email" class="form-control" readonly placeholder="{{$specialist->email}}">
</div>
</div>
{{-- First Name --}}
<div class="col-md-6">
<div class="form-group">
<label>First Name <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="first_name" value="{!! !empty($data->first_name) ? $data->first_name : ''!!}">
</div>
</div>
{{-- Last Name --}}
<div class="col-md-6">
<div class="form-group">
<label>Last Name <span class="text-danger">*</span></label>
<input type="text" class="form-control" name="last_name" value="{!! !empty($data->last_name) ? $data->last_name : ''!!}">
</div>
</div>
{{-- Phone Number --}}
<div class="col-md-6">
<div class="form-group">
<label>Phone Number<span class="text-danger">*</span></label>
<input type="number" class="form-control" name="phone_number" value="{!! !empty($data->phone_number) ? $data->phone_number : ''!!}" >
</div>
</div>
{{-- Gender --}}
<div class="col-md-6">
<div class="form-group">
<label>Gender<span class="text-danger">*</span></label>
<select class="form-control select" name="gender">
<option>{!! !empty($data->gender) ? $data->gender : 'Select'!!}</option>
<option>Male</option>
<option>Female</option>
</select>
</div>
</div>
{{-- Date of Birth --}}
<div class="col-md-6">
<div class="form-group mb-0">
<label>Date of Birth</label>
<input type="date" class="form-control" name="date_of_birth" value="{!! !empty($data->date_of_birth) ? $data->date_of_birth : ''!!}">
</div>
</div>
</div>
</div>
</div>
<!-- /Basic Information -->
<!-- About Me -->
<div class="card">
<div class="card-body">
<h4 class="card-title">About Me</h4>
<div class="form-group mb-0">
<label>Biography</label>
<textarea class="form-control" rows="5" name="about_me">{!! !empty($data->about_me) ? $data->about_me : ''!!}</textarea>
</div>
</div>
</div>
<!-- /About Me -->
<!-- Contact Details -->
<div class="card contact-card">
<div class="card-body">
<h4 class="card-title">Contact Details</h4>
<div class="row form-row">
<div class="col-md-6">
<div class="form-group">
<label>Address</label>
<input type="text" class="form-control" name="address" value="{!! !empty($data->address) ? $data->address : ''!!}">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">City</label>
<input type="text" class="form-control" name="city" value="{!! !empty($data->city) ? $data->city : ''!!}">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Country</label>
<input type="text" class="form-control" name="country" value="{!! !empty($data->country) ? $data->country : ''!!}">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label class="control-label">Postal Code</label>
<input type="number" class="form-control" name="postal_code" value="{!! !empty($data->postal_code) ? $data->postal_code : ''!!}">
</div>
</div>
</div>
</div>
</div>
<!-- /Contact Details -->
<div class="submit-section submit-btn-bottom">
<button type="submit" class="btn btn-primary submit-btn">Save Changes</button>
</div>
</form>
// Экран https://i.stack.imgur.com/SKcVt.png