我正在使用控制器存储方法来保存数据,我已经安装了模型,视图和控制器。我正在使用Laravel资源路由,并打算使用控制器执行CRUD操作。但是,似乎没有任何效果。
这是Controller的保存方法
public function store(Request $request)
{
//
$request->validate([
'adm'=>'required|unique:student',
'fullname'=>'required|alpha',
'dept'=>'required|alpha',
'course'=>'required|alpha',
'level'=>'required|alpha',
'feyear'=>'required|numeric',
'feser'=>'required|alpha',
'idnum'=>'unique:student|required|numeric|max:8',
'current_address'=>'required',
'permanent_address'=>'required',
'email'=>'unique:student|required|email',
'mobile'=>'unique:student|required|numeric',
'occupation'=>'nullable',
'occupation_place'=>'nullable',
'otherphone'=>'nullable:numeric|max:13',
'nextofkin'=>'nullable|alpha',
'nextofkinadd'=>'required',
'nextofkinphone'=>'required|numeric',
'placeofworkadd'=>'nullable',
'supervisoradd'=>'nullable'
]);
$std = new Student([
'adm'=> $request->get('adm'),
'fullname'=> $request->get('fullname'),
'dept'=> $request->get('dept'),
'course'=> $request->get('course'),
'level'=> $request->get('level'),
'feyear'=> $request->get('feyear'),
'feser'=> $request->get('feser'),
'idnum'=> $request->get('idnum'),
'current_address'=> $request->get('current_address'),
'permanent_address'=> $request->get('permanent_address'),
'email'=> $request->get('email'),
'mobile'=> $request->get('mobile'),
'occupation'=> $request->get('occupation'),
'occupation_place'=> $request->get('occupation_place'),
'otherphone'=> $request->get('otherphone'),
'nextofkin'=> $request->get('nextofkin'),
'nextofkinadd'=> $request->get('nextofkinadd'),
'nextofkinphone'=> $request->get('nextofkinphone'),
'placeofworkadd'=> $request->get('placeofworkadd'),
'supervisoradd'=> $request->get('supervisoradd')
]);
$std->save();
return redirect('/')->with('success', 'You have been added as an Student');
}
我的模特
class Student extends Model
{
protected $fillable = [
'adm',
'fullname',
'dept',
'course',
'level',
'feyear',
'feser',
'idnum',
'current_address',
'permanent_address',
'email',
'mobile',
'occupation',
'occupation_place',
'otherphone',
'nextofkin',
'nextofkinadd',
'nextofkinphone',
'placeofworkadd',
'supervisoradd'
];
}
我的表格
<form name="frmRegistration" method="post" action="{{ route('student.store') }}">
@csrf
<br> <p class="text-white">Fill this form if you have not yet Registered as student</p>
<div class="form-group">
<label class="text-white" for="inputEmail">Enter Full Names</label>
<input type="text" class="form-control" name="fullname" id="inputName" placeholder="e.g. Miss. Jane Doe">
</div>
<div class="form-group">
<label class="text-white" for="inputEmail">Enter National ID/Passport Number</label>
<input type="text" class="form-control" name="idnum" id="inputID" placeholder="e.g. 21436587">
</div>
<div class="form-group">
<label class="text-white" for="no">Current Mobile Phone Number</label>
<input type="text" class="form-control" name="mobile" id="inputphone" placeholder="e.g. 0734567890">
</div>
<div class="form-group">
<label class="text-white" for="otherno">Other Mobile Phone Number</label>
<input type="text" class="form-control" name="othermobile" id="altmobile" placeholder="e.g. 0734567890">
</div>
<div class="form-group">
<label class="text-white" for="inputEmail">Enter email Address</label>
<input type="email" class="form-control" name="email" id="inputName" placeholder="e.g. janedoe@yahoo.com ">
</div>
<div class="form-group">
<label class="text-white" for="inputEmail">Enter Current Address</label>
<input type="text" class="form-control" name="current_address" id="inputName" placeholder="e.g. P.O. Box 123456789 -00200- Nairobi ">
</div>
<div class="form-group">
<label class="text-white" for="inputEmail">Enter Permanent Address (can be similar to the above) </label>
<input type="text" class="form-control" name="permanent_address" id="inputName" placeholder="e.g. P.O. Box 123456 -30108- Timboroa ">
</div>
<div class="form-group">
<label class="text-white" for="admissionnumber">Next of Kin</label>
<input type="text" class="form-control" id="nextofkin" name ="nextofkin" placeholder="e.g. Jane Doe">
</div>
<div class="form-group">
<label class="text-white" for="admissionnumber">Next of Kin Address</label>
<input type="text" class="form-control" id="nextofkinadd" name ="nextofkinadd" placeholder="e.g. P.O. Box 234 -00100- Nairobi">
</div>
<div class="form-group">
<label class="text-white" for="admissionnumber">Next of Kin Mobile Phone</label>
<input type="text" class="form-control" id="nextofkinphone" name ="nextofkinphone" placeholder="e.g. 0734567890">
</div>
<div class="form-group">
<label class="text-white" for="admissionnumber">Current Occupation (if None use N/A) </label>
<input type="text" class="form-control" id="inputAdmissionnumber" name ="occupation" placeholder="e.g. Network Administrator">
</div>
<div class="form-group">
<label class="text-white" for="admissionnumber">Place of Current Occupation ( if None use N/A)</label>
<input type="text" class="form-control" id="inputAdmissionnumber" name ="occupation_place" placeholder="e.g. Nairobi">
</div>
<div class="form-group">
<label class="text-white" for="inputsupname">Supervisor Telephone Number ( if None use N/A)</label>
<input type="text" class="form-control" name="supervisoradd" id="supphone" placeholder="e.g. 0734567890">
</div>
<div class="form-group">
<label class="text-white" for="inputsupname">Address of Current Place of Work ( if None use N/A)</label>
<input type="text" class="form-control" name="placeofworkadd" id="occuadd" placeholder="e.g. Rift Valley Technical Training Institute, P.O. Box 244 -30100-,Eldoret">
</div>
<div class="form-group">
<label class="text-white" for="admissionnumber">Your Student Registration Number</label>
<input type="text" class="form-control" id="inputAdmissionnumber" name ="adm" placeholder="e.g. 117R0008">
</div>
<div class="form-group">
<label class="text-white" for="department">Select your Course's Department</label>
<select class="form-control" id="dept" name="dept">
<option>Automotive Engineering</option>
<option>Building & Civil Engineering</option>
<option>Business and Development Studies</option>
<option>Electrical & Electronic Engineering</option>
<option>Hospitality & Dietetics Management</option>
<option>Information Communication Technology</option>
<option>Mechanical and Automotive Engineering</option>
<option>Pharmacy & Chemical Science</option>
<option>Medical & Biological Sciences</option>
</select>
</div>
<div class="form-group">
<label class="text-white" for="admissionnumber">Course (in full as per registration)</label>
<input type="text" class="form-control" id="inputcourse" name="course" placeholder="e.g. Diploma in Information Communication Technology">
</div>
<div class="form-group">
<label class="text-white" for="courselevel">Select your current Course's Level</label>
<select class="form-control" id="select" name="level">
<option>Artisan</option>
<option>Craft</option>
<option>Diploma</option>
<option>Higher Diploma</option>
</select>
</div>
<div class="form-group">
<label class="text-white" for="year"> Final Examination Year</label>
<input type="number" class="form-control" id="datepicker" name="feyear" placeholder="e.g. 1994" min="1980" max="2018" value="2018">
</div>
<div class="form-group">
<label class="text-white" for="year"> Series</label>
<Select class="form-control" id="selectSeries" name="feser">
<option>July</option>
<option>November</option>
</select>
</div>
<div class="form-group">
<!--<a class="btn btn-primary btnNext" >Next Tab</a> --> <button type="submit" class="btn bg-primary btn-outline btn-xl js-scroll-trigger">Add</button></div>
</form>
答案 0 :(得分:1)
您是否已检查收到的请求数据?尝试转储它,并检查其是否正确dd($request->all())
还要检查您的日志,laravel.log,php-errors.log等。
此外,您的验证者还应返回经过验证的数据,例如:
$validatedData = $request->validate([
'adm'=>'required|unique:student',
'fullname'=>'required|alpha',
etc....
]);
然后,如果所有字段都在您的 fillable 数组中,则可以执行以下操作:Student::create($validatedData)
无论如何,我建议您使用Form Requests进行验证,这样您的控制器就不会那么肿。
答案 1 :(得分:1)
也许,您可以尝试使用fill方法来更具体
尝试:
$std = new Student;
$std->fill($request->all());
$std->save();
答案 2 :(得分:1)
尝试做
$std = Student::create([
'adm'=> $request->get('adm'),
'fullname'=> $request->get('fullname'),
'dept'=> $request->get('dept'),
'course'=> $request->get('course'),
'level'=> $request->get('level'),
'feyear'=> $request->get('feyear'),
'feser'=> $request->get('feser'),
'idnum'=> $request->get('idnum'),
'current_address'=> $request->get('current_address'),
'permanent_address'=> $request->get('permanent_address'),
'email'=> $request->get('email'),
'mobile'=> $request->get('mobile'),
'occupation'=> $request->get('occupation'),
'occupation_place'=> $request->get('occupation_place'),
'otherphone'=> $request->get('otherphone'),
'nextofkin'=> $request->get('nextofkin'),
'nextofkinadd'=> $request->get('nextofkinadd'),
'nextofkinphone'=> $request->get('nextofkinphone'),
'placeofworkadd'=> $request->get('placeofworkadd'),
'supervisoradd'=> $request->get('supervisoradd')
]);
或者如果您的$ request字段与可填充字段相同则更简单
$input=$request->all();
$std = Student::create($input);