I’m trying to save the data from the form. However, there is no record when I check the database side.
I’m using Laravel 10 and mySQL v5.7.
**Controller **store()
public function store(Request $request)
{
$personnelRegistry = null;
$personnerlRegistryId = (int)$request->input('personnelRegistryId', null);
if($personnerlRegistryId && $request->isMethod('post')){
$personnelRegistry = PersonnelRegistry::find($personnerlRegistryId)->getAttributes();
$joinDate = $request->get('joinDate');
$personnel = new PersonnelDetail;
$personnel->personnel_registry_id = $personnerlRegistryId;
$personnel->service_id = $request->get('serviceId');
$personnel->emp_status_id = (int)$request->get('employmentStatusId');
$personnel->joining_date = date("Y-m-d", strtotime($joinDate));
$personnel->ref_pos_id = (int)$request->get('posId');
$personnel->step_id = (int)$request->get('stepId');
$personnel->current_salary = 1;
$personnel->is_current = (int)$request->get('isPersonnelCurrent');
$personnel->created_by = 1;
$personnel->updated_by = 1;
$personnel->save();
$request->session()->flash('status', 'Personnel details was created.');
return redirect()->route('employee.show', ['employee' => $personnerlRegistryId]);
} else {
//Validation failed. Redirect
}
}
Model
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class PersonnelDetail extends Model
{
use HasFactory;
public $table = 'personnel_details';
protected $primaryKey = 'id';
/**
*
* @var bool
*/
public $timestamps = true;
}
View
<form action="{{ route('employee.store') }}" method="POST">
@csrf
<div class="mb-3 row">
<label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Personnel Name</label>
<div class="col-sm-8 col-md-9 col-lg-10">
<span id="personnelNamePlaceholder"></span>
<input type="hidden" name="personnelRegistryId" id="personnelRegistryId" required />
<button type="button" class="btn btn-outline-primary mb-1" data-bs-toggle="modal" data-bs-target="#searchPersonnel">Search Records</button>
</div>
</div>
<div class="mb-3 row">
<label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Service Id</label>
<div class="col-sm-8 col-md-9 col-lg-10">
<input type="text" name="serviceId" class="form-control" required />
</div>
</div>
<div class="mb-3 row">
<label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Employee Status Id</label>
<div class="col-sm-8 col-md-9 col-lg-10">
<select name="employmentStatusId" class="select2 form-control" id="employmentStatus">
<option label=" "></option>
<option value="24">Permanent</option>
<option value="25">Contractual</option>
</select>
</div>
</div>
<div class="mb-3 row">
<label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Join Date</label>
<div class="col-sm-8 col-md-9 col-lg-10">
<input type="text" name="joinDate" class="form-control date-picker-close" id="joindate" value="{{ $date_now }}">
</div>
</div>
<div class="mb-3 row">
<label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Position</label>
<div class="col-sm-8 col-md-9 col-lg-10">
<select name="posId" class="form-control" id="positionId" require>
<option label=" "></option>
@foreach($plantilla_items as $item)
<option value="{{ $item->id }}">{{ $item->uniq_item_no }}</option>
@endforeach
</select>
</div>
</div>
<div class="mb-3 row">
<label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Step</label>
<div class="col-sm-8 col-md-9 col-lg-10">
<select name="stepId" class="form-control" id="stepId" require>
<option label=" "></option>
@foreach($steps as $step)
<option value="{{ $step->id }}">{{ $step->description }}</option>
@endforeach
</select>
</div>
</div>
<div class="mb-3 row">
<label class="col-lg-2 col-md-3 col-sm-4 col-form-label">Is Current?</label>
<div class="col-sm-8 col-md-9 col-lg-10">
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="isPersonnelCurrent" id="inlineRadio1" value="1" />
<label class="form-check-label" for="inlineRadio1">Yes</label>
</div>
<div class="form-check form-check-inline">
<input class="form-check-input" type="radio" name="isPersonnelCurrent" id="inlineRadio2" value="0" />
<label class="form-check-label" for="inlineRadio2">No</label>
</div>
</div>
</div>
<div class="mb-3 row mt-5">
<div class="col-sm-8 col-md-9 col-lg-10 ms-auto">
<button type="submit" class="btn btn-outline-primary">Save</button>
</div>
</div>
</form>
No errors from laravel and it successfully redirects to the employee.show page.
Please and thank you..
I was expecting to have data in the database after save().
Create Table personnel_details
CREATE TABLE `personnel_details` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`personnel_registry_id` bigint(20) unsigned NOT NULL,
`service_id` smallint(5) unsigned NOT NULL,
`org_code_id` smallint(5) unsigned DEFAULT NULL,
`position_id` smallint(5) unsigned DEFAULT NULL,
`emp_status_id` smallint(5) unsigned NOT NULL,
`joining_date` date NOT NULL,
`leave_date` date DEFAULT NULL,
`ref_pos_id` bigint(20) unsigned NOT NULL,
`step_id` smallint(5) unsigned NOT NULL,
`is_current` tinyint(3) unsigned NOT NULL,
`current_salary` decimal(10,2) DEFAULT NULL,
`created_by` bigint(20) unsigned DEFAULT NULL,
`updated_by` bigint(20) unsigned DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `FKPersonnelRegistryTOPersonnelDetails_idx` (`personnel_registry_id`),
CONSTRAINT `FKPersonnelRegistryTOPersonnelDDetails` FOREIGN KEY (`personnel_registry_id`) REFERENCES `personnel_registry` (id)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci'
Create table personnel_registry
CREATE TABLE `personnel_registry` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`agency_emp_id` bigint(20) unsigned NOT NULL,
`last_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`first_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`middle_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
`extension_name` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL,
...
`created_by` bigint(20) unsigned NOT NULL,
`updated_by` bigint(20) unsigned NOT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
2
Answers
You’re missing parentheses "()" mark on the model initiation. try this:
the different thing is :
I changed it to :
In Laravel, when you want to instantiate a model within a controller, you need to use parentheses () after the model name because it represents a method call. The model name followed by parentheses indicates that you are calling the constructor of the model class to create a new instance of that model.
By including the parentheses, you are invoking the constructor method, which is responsible for setting up the initial state of the model object and preparing it for use. It allows you to create a new instance of the model class and access its properties and methods within your controller.
You must define fillable attributes On your Model.
Like this:
or define $guarded with empty property. but consider it’s risk!
refer to document if you need more details