我试图只使用一个表单来创建和编辑表单.这是一个简单的表单,我只使用HTML和Im使用Laravel 5.3.当我处于编辑模式时,我不知道如何填充数据.我不知道只在一个表单或视图中使用创建和编辑.因为我的其他编辑视图有一个值,但现在我想让它成为我创建和编辑表单的一个表单.我希望你能帮助我.顺便说一句,我使用ajax进行创建和更新.
add.blade.PHP
@extends('main_layout')
@section('content')
<!-- MAIN PANEL -->
<div id="main" role="main">
<!-- RIBBON -->
<div id="ribbon">
<div class="row">
<!-- breadcrumb -->
<section class="col-sm-6 col-md-6 col-lg-6">
<ol class="breadcrumb">
<li>
{{$breadcrumb}}
</li>
</ol>
</section>
<!-- end breadcrumb -->
<section class="col-sm-6 col-md-6 col-lg-6 header-button">
<button class="btn btn-primary" type="submit" form="form1" id="btnAdd">{{$saveButtonText}}</button>
</section>
</div>
</div>
<!-- END RIBBON -->
<!-- MAIN CONTENT -->
<div id="content">
<section id="" class="">
<!-- START ROW -->
<div class="row">
<!-- NEW COL START -->
<article class="col-sm-8 col-md-8 col-lg-8">
<!-- Widget ID (each widget will need unique ID)-->
<div class="jarviswidget jarviswidget-color-red" id="wid-id-0" data-widget-sortable="false" data-widget-deletebutton="false" data-widget-colorbutton="false" data-widget-editbutton="false" data-widget-custombutton="false">
<header>
<span class="widget-icon"> <i class="fa fa-user"></i> </span>
<h2>Personal information</h2>
</header>
<!-- widget div-->
<div>
<!-- widget content -->
<div class="widget-body no-padding">
<form class="smart-form" method="post" action="/register" name="myForm" id="form1">
<input type="hidden" name="_token" value="<?PHP echo csrf_token(); ?>">
<div class="alert alert-success fade in" id="alertSuccess">
<i class="fa-fw fa fa-check"></i>
<strong>Success!</strong> Indicates a successful or positive action.
</div>
<div class="alert alert-danger fade in" id="alertDanger">
<i class="fa-fw fa fa-times"></i>
<strong>Failed!</strong> Please Indicate the following info.
</div>
<fieldset>
<div class="row">
<section class="col col-4">
<label class="label">Full Name</label>
<label class="input">
<input type="text" name="first_name" id="first_name" placeholder="First Name">
</label>
</section>
<section class="col col-4">
<label class="label"> </label>
<label class="input">
<input type="text" name="middle_name" id="middle_name" placeholder="Middle Name">
</label>
</section>
<section class="col col-4">
<label class="label"> </label>
<label class="input">
<input type="text" name="last_name" id="last_name"placeholder="Last Name">
</label>
</section>
</div>
<section>
<label class="label">Present Address</label>
<label class="input">
<input type="text" id="present_address" name="present_address" placeholder="Present Address">
</label>
</section>
<section>
<label class="label">Permanent Address <span class="permanent-address">
<input type="checkBox" name="billingtoo" onClick="FillBilling(this.form)"> Same with Present Address</span></label>
<label class="input">
<input type="text" id="permanent_address" name="permanent_address" id="lay" placeholder="Permanent Address">
</label>
</section>
<div class="row">
<section class="col col-4">
<label class="label">Gender</label>
<label class="select">
<select name="gender" id="Gender" placeholder="Gender">
<option value="" disabled selected>Gender</option>
@foreach (Config::get('constants.GENDER') as $gender)
<option value="{{$gender}}">{{$gender}}</option>
@endforeach
</select>
<i></i>
<!-- <input type="text" name="fname" placeholder="First Name"> -->
</label>
</section>
<section class="col col-4">
<label class="label">Birthday</label>
<label class="input"> <i class="icon-append fa fa-calendar"></i>
<input type="text" name="birthday" placeholder="Birthday" class="datepicker" data-dateformat='mm/dd/yy'>
</label>
</section>
<section class="col col-4">
<label class="label">Email</label>
<label class="input">
<input type="email" id="email" name="email" placeholder="E-mail">
</label>
</section>
</div>
<div class="row">
<section class="col col-4">
<label class="label">Work Phone #</label>
<label class="input">
<input type="tel" name="work_phone" placeholder="Work Phone #" data-mask="(999) 999-9999" class="valid"/>
</label>
</section>
<section class="col col-4">
<label class="label">Home Phone #</label>
<label class="input">
<input type="tel" name="home_phone" placeholder="Home Phone #" data-mask="(999) 999-9999" class="valid"/>
</label>
</section>
<section class="col col-4">
<label class="label">Mobile Phone #</label>
<label class="input">
<input type="tel" name="mobile_phone" placeholder="Mobile Phone #" data-mask="(+63) 999-999-9999" class="valid"/>
</label>
</section>
</div>
<div class="row">
<section class="col col-4">
<label class="label">Religion</label>
<label class="input">
<input type="text" name="religion" placeholder="Religion">
</label>
</section>
<section class="col col-4">
<label class="label">Citizenship</label>
<label class="input">
<input type="text" name="citizenship" placeholder="Citizenship">
</label>
</section>
<section class="col col-4">
<label class="label">Civil Status</label>
<label class="input">
<input type="text" name="civil_status" placeholder="Civil Status">
</label>
</section>
</div>
<section class="">
<label class="label"><br/>Emergency Contacts</label>
</section>
<div class="row">
<section class="col col-4">
<label class="label">Name</label>
<label class="input">
<input type="text" name="emergency_name1" placeholder="Emergency Contact Name 1" id="emergency_name1">
</label>
</section>
<section class="col col-4">
<label class="label">Relationship</label>
<label class="input">
<input type="text" name="emergency_rel1" placeholder="Emergency Relationship 1">
</label>
</section>
<section class="col col-4">
<label class="label">Contact #</label>
<label class="input">
<input type="text" name="emergency_contact1" placeholder="Emergency Contact # 1"onkeypress="return isNumber(event)" />
</label>
</section>
</div>
<div class="row">
<section class="col col-4">
<label class="input">
<input type="text" name="emergency_name2" placeholder="Emergency Contact Name 2">
</label>
</section>
<section class="col col-4">
<label class="input">
<input type="text" name="emergency_rel2" placeholder="Emergency Relationship 2">
</label>
</section>
<section class="col col-4">
<label class="input">
<input type="text" name="emergency_contact2" placeholder="Emergency Contact # 2"onkeypress="return isNumber(event)" />
</label>
</section>
</div>
<div class="row">
<section class="col col-4">
<label class="input">
<input type="text" name="emergency_name3" placeholder="Emergency Contact Name 3">
</label>
</section>
<section class="col col-4">
<label class="input">
<input type="text" name="emergency_rel3" placeholder="Emergency Relationship 3">
</label>
</section>
<section class="col col-4">
<label class="input">
<input type="text" name="emergency_contact3" placeholder="Emergency Contact # 3"onkeypress="return isNumber(event)" />
</label>
</section>
</div>
</fieldset>
</div>
<!-- end widget content -->
</div>
<!-- end widget div -->
</div>
<!-- end widget -->
</article>
</form>
这些是路线
Route::get('/employee/add', 'PagesController@addEmployee');
Route::get('/employee/add/{paramStatus}', 'PagesController@addEmployee');
Route::get('/employee/{id}/add', 'EmployeesController@editEmployee');
Route::post('employee/{id}/ajaxShow', 'EmployeesController@ajaxShow');
Route::post('employee/{id}/ajaxUpdate', 'EmployeesController@ajaxUpdate');
PagesController.PHP
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use View;
use App\Entry;
use App\SSSSetting;
use Request as Req;
use App\manage;
use DB;
use App\Employee;
class PagesController extends Controller
{
public function addEmployee()
{
$breadcrumb = 'Employees > Add Employee';
$pageTitle = 'CollabUX | Add Employee';
$saveButtonText = "Add Employee";
$prepath ='../';
$currentURL = Req::url();
$resultem = Employee::all();
$EmployeeInfo = DB::table('employees')->get();
return View::make('employee.add')->with(
array('breadcrumb' => $breadcrumb,'pageTitle' => $pageTitle,'currentURL' => $currentURL,
'saveButtonText' => $saveButtonText,'prepath' => $prepath,'resultem' => $resultem,'EmployeeInfo' => $EmployeeInfo));
}
public function editEmployee($id)
{
$breadcrumb = 'Employees > Manage Employees > Edit';
$pageTitle = 'CollabUX | Edit Employee';
$saveButtonText = "Save Changes";
$prepath ='../';
$currentURL = Req::url();
$resultem = manage::all();
$EmployeeInfo = DB::table('employees')->where('id', $id)->get();
return View::make('employee.add')->with(
array('breadcrumb' => $breadcrumb,'pageTitle' => $pageTitle,'currentURL' => $currentURL,
'saveButtonText' => $saveButtonText,'prepath' => $prepath,'resultem' => $resultem, ));
}
}
EmployeeController.PHP
<?PHP
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use DB;
use Session;
use App\Employee;
use Request as Req;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class EmployeesController extends Controller
{
public function insert(Request $request){
$first_name = $request->input('first_name');
$middle_name = $request->input('middle_name');
$last_name = $request->input('last_name');
$present_address = $request->input('present_address');
$permanent_address = $request->input('permanent_address');
$gender = $request->input('gender');
$birthday = $request->input('birthday');
$email = $request->input('email');
$work_phone = $request->input('work_phone');
$home_phone = $request->input('home_phone');
$mobile_phone = $request->input('mobile_phone');
$religion = $request->input('religion');
$citizenship = $request->input('citizenship');
$civil_status = $request->input('civil_status');
$data = array('id'=>$employeeNum,'first_name'=>$first_name,'middle_name'=>$middle_name,'last_name'=>$last_name,'present_address'=>$present_address,'permanent_address'=>$permanent_address,'email'=>$email,'work_phone'=>$work_phone,'home_phone'=>$home_phone,'mobile_phone'=>$mobile_phone,'religion'=>$religion,'gender'=>$gender,'birthday'=>$birthday,'citizenship'=>$citizenship);
$save = DB::table('employees')->insert($data);
return redirect('/employee/add');
return view('add');
}
public function ajaxShow(Request $request, $id) {
$manageEmployee = Employee::find($id);
return array('status' => 'OK', 'result' => $manageEmployee);
}
public function editEmployee($id)
{
$breadcrumb = 'Employees > Manage Employees > Edit';
$pageTitle = 'CollabUX | Edit Employee';
$saveButtonText = "Save Changes";
$currentURL = Req::url();
$prepath ='../';
$resultem = Employee::all();
$EmployeeInfo = DB::table('employees')->where('id', $id)->get();
return view('employee.add')->with(array('breadcrumb'=>$breadcrumb,'pageTitle'=>$pageTitle,'currentURL' => $currentURL,'prepath'=>$prepath,'saveButtonText'=>$saveButtonText,'EmployeeInfo'=>$EmployeeInfo, 'resultem' => $resultem));
}
public function update(Request $request, $id)
{
// validate the data
$this->validate($request, [
'first_name' => 'required',
'middle_name' => 'required',
'last_name' => 'required',
'present_address' => 'required',
'permanent_address' => 'required',
'gender' => 'required',
'birthday' => 'required',
'email' => 'required',
'work_phone' => 'required',
'home_phone' => 'required',
'religion' => 'required',
'citizenship' => 'required'
]);
$manageEmployee = Employee::where('id', '!=', $id)->where(array('first_name' => $request->first_name, 'middle_name' => $request->middle_name,'last_name' => $request->last_name,'present_address' => $request->present_address,'permanent_address' => $request->permanent_address,'gender' => $request->gender,'birthday' => $request->birthday,'email' => $request->email,'work_phone' => $request->work_phone,'home_phone' => $request->home_phone,'religion' => $request->religion,'citizenship' => $request->citizenship,'civil_status' => $request->civil_status,'employeeNum' => $request->employeeNum,'Position' => $request->Position,'Supervisor' => $request->Supervisor,'starting_salary' => $request->starting_salary,'status' => $request->status,'tin' => $request->tin,'SSS' => $request->SSS,'philhealth' => $request->philhealth,'pagibig' => $request->pagibig,'no_dependents' => $request->no_dependents))->first();
if ($manageEmployee)
return array('status' => 'ERROR', 'error' => 'Record already exists.');
$manageEmployee = Employee::find($id);
if ($manageEmployee) {
$manageEmployee->first_name = $request->first_name;
$manageEmployee->middle_name = $request->middle_name;
$manageEmployee->last_name = $request->last_name;
$manageEmployee->present_address = $request->present_address;
$manageEmployee->permanent_address = $request->permanent_address;
$manageEmployee->gender = $request->gender;
$manageEmployee->birthday = $request->birthday;
$manageEmployee->email = $request->email;
$manageEmployee->work_phone = $request->work_phone;
$manageEmployee->home_phone = $request->home_phone;
$manageEmployee->religion = $request->religion;
$manageEmployee->citizenship = $request->citizenship;
$manageEmployee->civil_status = $request->civil_status;
$manageEmployee->employeeNum = $request->employeeNum;
$manageEmployee->Position = $request->Position;
$manageEmployee->Supervisor = $request->Supervisor;
$manageEmployee->starting_salary = $request->starting_salary;
$manageEmployee->status = $request->status;
$manageEmployee->tin = $request->tin;
$manageEmployee->SSS = $request->SSS;
$manageEmployee->philhealth = $request->philhealth;
$manageEmployee->pagibig = $request->pagibig;
$manageEmployee->no_dependents = $request->no_dependents;
$manageEmployee->save();
}
return array('status' => 'OK', 'result' => $manageEmployee);
}
}
解决方法:
制作三个视图文件
1. create.blade.PHP
2. edit.blade.PHP
3. _form.blade.PHP
_form.blade.PHP是一个部分文件,将包含在创建和编辑中.
注意:您必须使用laravelcollective / html包.
create.blade.PHP
<!-- Your template HTML code here -->
{!! Form::open([
'action' => 'EmployeesController@store',
'id' => 'employee-form'
]) !!}
@include('employee._form')
{!! Form::close() !!}
<!-- Continue with your template HTML code here -->
edit.blade.PHP
<!-- Your template HTML code here -->
{!! Form::model([
$employee,
'method' => 'POST', <!-- Or whatever you have used for update() -->
'action' => ['EmployeesController@update', $employee->id],
'id' => 'employee-form'
]) !!}
@include('employee._form')
{!! Form::close() !!}
<!-- Continue with your template HTML code here -->
_form.blade.PHP
<fieldset>
<div class="row">
<section class="col col-4">
<label class="label">Full Name</label>
<label class="input">
{!! Form::text('first_name', null, ['placeholder => 'First Name']) !!}
</label>
</section>
<section class="col col-4">
<label class="label"> </label>
<label class="input">
<input type="text" name="middle_name" id="middle_name" placeholder="Middle Name">
{!! Form::text('middle_name', null, ['placeholder' => 'Middle Name']) !!}
</label>
</section>
...
</fieldset>
表单模型绑定将负责使用数据库中存储的值填充编辑表单字段.
如果要避免多个视图文件:
只需要一个视图,比如employee_form.blade.PHP
<!-- Your template HTML code here -->
@if($employee instanceof \App\Employee)
{!! Form::model([
$employee,
'method' => 'POST', <!-- Or whatever you have used for update() -->
'action' => ['EmployeesController@update', $employee->id],
'id' => 'employee-form'
]) !!}
@else
{!! Form::model([
$employee = new \App\Employee,
'method' => 'POST', <!-- Or whatever you have used for store() -->
'action' => ['EmployeesController@store', $employee->id],
'id' => 'employee-form'
]) !!}
@endif
@include('employee._form')
{!! Form::close() !!}
<!-- Continue with your template HTML code here -->
另一个建议
请不要将所有业务逻辑放在控制器中.
使用Request类进行验证.
尝试将业务逻辑移动到其他类(如Service和Repository类)以保持控制器的轻量级.请参阅this,了解如何使用Helper类来保持代码清洁和易于管理.