我是Laravel的新手。我正在尝试在我的一个表中插入数据而我正在使用mysql.Now这是我的模型,
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class BloodDonner extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable=array
( 'name',
'email',
'blood_group',
'phone_number',
'location',
'date_of_birth',
'last_date_of_donation'
);
}
这是我的迁移文件
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class BloodDonner extends Migration {
public function up()
{
Schema::create('bloodDonners', function(Blueprint $table)
{
$table->increments('id');
$table->unsignedInteger('donner_id');
$table->foreign('donner_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('donner_name');
$table->string('email')->unique();
$table->string('blood_group');
$table->string('phone_number')->unique();
$table->string('location');
$table->date('date_of_birth');
$table->date('last_date_of_donation');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('bloodDonners');
}
}
这是我的控制器
<?php
class BloodController extends \BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
//return View::make('bloodDonation.donners');
}
public function indexBloodDonation()
{
return View::make('bloodDonation.donners');
}
public function getBloodDonation()
{
return View::make('bloodDonation.blood-donation-form');
}
public function postBloodDonation()
{
$validator = Validator::make(Input::all(),array
(
'donner_name' => 'required|min:3|max:255',
'date_of_birth' => 'required',
'blood_group' => 'required',
'location' => 'required',
'email'=> 'required|email|max:255|unique:blooddonners',
'phone_number' => 'required|unique:blooddonners',
'last_date_of_donation' => 'required'
));
if($validator->fails())
{
return Redirect::route('donation-form')
->withErrors($validator)
->withInput();
}
else
{
$donner_name = Input::get('donne_name');
$date_of_birth = Input::get('date_of_birth');
$blood_group = Input::get('blood_group');
$location = Input::get('location');
$email = Input::get('email');
$phone_number = Input::get('phone_number');
$last_date_of_donation = Input::get('last_date_of_donation');
$bloodDonner = BloodDonner::create(array(
'donner_name' => $donner_name,
'date_of_birth' => $date_of_birth,
'blood_group' => $blood_group,
'location' => $location,
'email' => $email,
'phone_number' => $phone_number,
'last_date_of_donation'=> $last_date_of_donation
)
);
if($bloodDonner){
return Redirect::route('donation-form')->with('global','You have Successfully registered as Blood Donner!');
}
}
}
这是路线
<?php
Route::get('/donners',array
(
'as' => 'donners',
'uses' => 'BloodController@indexBloodDonation'
));
Route::get('/blood-donation-form',array(
'as' => 'donation-form',
'uses' => 'BloodController@getBloodDonation'
));
/*
/Authenticated Group
*/
Route::group(array('before' => 'auth'),function(){
/*
/Sign Out(GET)
*/
Route::get('/signout',array
(
'as' => 'signout',
'uses' => 'AccountController@signoutGet'
));
/*
/csrf protection
*/
Route::group(array('before' => 'csrf'),function()
{
/*
/ Blood Donation (POST)
*/
Route::post('/donner-registration',array
(
'as' => 'blood-donation-post',
'uses' => 'BloodController@postBloodDonation'
));
});
/*
/ Blood Donation (GET)
*/
Route::get('/blood-donation-form',array
(
'as' => 'donation-form',
'uses' => 'BloodController@getBloodDonation'
));
});
这是我的观点
@extends('layouts.base')
@section('content')
@if(Session::has('global'))
<p>{{Session::get('global')}}</p>
@endif
<!--==========END OF BLOOD DONNER=============-->
<div class="container" style="margin-bottom:30px; margin-top:5px">
<ul class="nav nav-tabs">
<li class="active"><a href="{{URL::route('donners')}}" ><strong>DONNERS</strong></a></li>
<li><a href="{{URL::route('donation-form')}}" ><strong>DONATE BLOOD</strong></a></li>
</ul>
<!-- Tab panes -->
<div class="tab-content">
<!--<div class="tab-pane fade" id="donate"> --><!--==========BLOOD DONATION=============-->
<div class="container">
<div class="col-md-5 col-sm-5 hidden-xs" style="margin-top:20px;">
<img class="img-responsive" src="img/save-life-1.jpg" alt="blood-donation">
</div>
<div class="col-md-7 col-sm-7 col-xs-12" style="margin-top:20px;">
<form class="form-horizontal" role="form" action="{{URL::route('blood-donation-post')}}" method="post">
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Name</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputEmail3" placeholder="Name" name='donner_name'{{(Input::old('donner_name')) ? ' value="'.e(Input::old('donner_name')).'"' : ''}}>
@if($errors->has('donner_name'))
<span style="color:red">{{$errors->first('donner_name')}}</span>
@endif
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Date of Birth</label>
<div class="col-sm-8">
<input type="date" class="form-control" id="inputEmail3" placeholder="Age" name='date_of_birth'{{(Input::old('age')) ? ' value="'.e(Input::old('age')).'"' : ''}}>
@if($errors->has('date_of_birth'))
<span style="color:red">{{$errors->first('date_of_birth')}}</span>
@endif
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Blood Group</label>
<div class="col-sm-8">
<select class="form-control" name='blood_group'>
<option>You Must Select your Blood Group</option>
<option>O-</option>
<option>O+</option>
<option>A-</option>
<option>A+</option>
<option>B-</option>
<option>B+</option>
<option>AB-</option>
<option>AB+</option>
</select>
@if($errors->has('blood_group'))
<span style="color:red">{{$errors->first('blood_group')}}</span>
@endif
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Location</label>
<div class="col-sm-8">
<select class="form-control" name='location'>
<option>GAIBANDHA</option>
<option>KURIGRAM</option>
<option>LALMONIRHAT</option>
<option>NILPHAMARI</option>
<option>PANCHAGARH</option>
</select>
@if($errors->has('location'))
<span style="color:red">{{$errors->first('location')}}</span>
@endif
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Email</label>
<div class="col-sm-8">
<input type="email" class="form-control" id="inputEmail3" placeholder="Email" name='email'{{(Input::old('email')) ? ' value="'.e(Input::old('email')).'"' : ''}}>
@if($errors->has('email'))
<span style="color:red">{{$errors->first('email')}}</span>
@endif
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Phone Number</label>
<div class="col-sm-8">
<input type="text" class="form-control" id="inputEmail3" placeholder="Phone Number" name='phone_number'{{(Input::old('phone_number')) ? ' value="'.e(Input::old('phone_number')).'"' : ''}}>
@if($errors->has('phone_number'))
<span style="color:red">{{$errors->first('phone_number')}}</span>
@endif
</div>
</div>
<div class="form-group">
<label for="inputEmail3" class="col-sm-4 control-label">Last Date of Blood Donation</label>
<div class="col-sm-8">
<input type="date" class="form-control" id="inputEmail3" placeholder="Last Date of Blood Donation" name='last_date_of_donation'{{(Input::old('donate_date')) ? ' value="'.e(Input::old('donate_date')).'"' : ''}}>
@if($errors->has('last_date_of_donation'))
<span style="color:red">{{$errors->first('last_date_of_donation')}}</span>
@endif
</div>
</div>
<div style="padding-left:20%">
<button type="submit" class="btn btn-default ">Submit</button>
{{ Form::token() }}
</div>
</form>
</div>
</div>
<!-- </div> --><!--==========END OF BLOOD DONATION=============-->
</div>
</div>
@stop
这是我的laravel.log文件(最后一部分)
#3 C:\xampp\htdocs\Needa\app\controllers\BloodController.php(41): Illuminate\Support\Facades\Redirect::route('blood-donation-...')
#4 [internal function]: BloodController->postBloodDonation()
#5 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\Controller.php(231): call_user_func_array(Array, Array)
#6 C:\xampp\htdocs\Needa\vendor\laravel\framework\src\Illuminate\Routing\ControllerDispatcher. php(93): Illuminate\Routing\Controller->callAction('postBloodDonati...', Array)
#7 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ ControllerDispatcher。 php(62):Illuminate \ Routing \ ControllerDispatcher-&gt; call(Object(BloodController),Object(Illuminate \ Routing \ Route),&#39; postBloodDonati ...&#39;) #8 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php(930):Illuminate \ Routing \ ControllerDispatcher-&gt; dispatch(Object(Illuminate \ Routing \ Route),Object (Illuminate \ Http \ Request),&#39; BloodController&#39;,&#39; postBloodDonati ...&#39;) #9 [内部功能]:Illuminate \ Routing \ Router-&gt; Illuminate \ Routing {closure}() #10 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Route.php(105):call_user_func_array(Object(Closure),Array) #11 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php(996):Illuminate \ Routing \ Route-&gt; run(Object(Illuminate \ Http \ Request)) #12 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php(964):Illuminate \ Routing \ Router-&gt; dispatchToRoute(Object(Illuminate \ Http \ Request)) #13 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php(73 8):Illuminate \ Routing \ Router-&gt; dispatch(Object(Illuminate \ Http \ Request)) #14 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php(70 8):Illuminate \ Foundation \ Application-&gt; dispatch(Object(Illuminate \ Http \ Request)) #15 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Session \ Middleware.php(72):Illuminate \ Foundation \ Application-&gt; handle(Object(Illuminate \ Http \ Request),1 ,真的) #16 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Cookie \ Queue.php(47):Illuminate \ Session \ Middleware-&gt; handle(Object(Illuminate \ Http \ Request),1 ,真的) #17 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Cookie \ Guard.php(51):Illuminate \ Cookie \ Queue-&gt; handle(Object(Illuminate \ Http \ Request),1 ,真的) #18 C:\ xampp \ htdocs \ Needa \ vendor \ stack \ builder \ src \ Stack \ StackedHttpKernel.php(23):Illuminate \ Cookie \ Guard-&gt; handle(Object(Illuminate \ Http \ Request),1,true ) #19 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php(60 6):Stack \ StackedHttpKernel-&gt; handle(Object(Illuminate \ Http \ Request)) #20 C:\ xampp \ htdocs \ Needa \ public \ index.php(49):Illuminate \ Foundation \ Application-&gt; run() #21 C:\ xampp \ htdocs \ Needa \ server.php(19):require_once(&#39; C:\ xampp \ htdocs ...&#39;) #22 {main} [] [] [2014-08-08 14:57:19] production.ERROR:exception&#39; Symfony \ Component \ Debug \ Exception \ FatalErrorException&#39;有消息&#39;语法错误,意外&#39;}&#39;,期待变量(T_VARIABLE)或&#39; $&#39;&#39;在C:\ xampp \ htdocs \ Needa \ app \ controllers \ BloodController.php:51 堆栈跟踪: #0 [内部函数]:Illuminate \ Exception \ Handler-&gt; handleShutdown() #1 {main} [] [] [2014-08-08 15:21:15] production.ERROR:exception&#39; Symfony \ Component \ Debug \ Exception \ FatalErrorException&#39;消息&#39;语法错误,意外&#39 ;;&#39;,期待&#39;)&#39;&#39;在C:\ xampp \ htdocs \ Needa \ app \ controllers \ BloodController.php:58 堆栈跟踪: #0 [内部函数]:Illuminate \ Exception \ Handler-&gt; handleShutdown() #1 {main} [] [] [2014-08-10 16:12:32] production.ERROR:exception&#39; Symfony \ Component \ HttpKernel \ Exception \ NotFoundHttpException&#39;在C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ RouteCollection.php:1 46 堆栈跟踪: #0 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php(1017):Illuminate \ Routing \ RouteCollection-&gt; match(Object(Illuminate \ Http \ Request)) #1 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php(985):Illuminate \ Routing \ Router-&gt; findRoute(Object(Illuminate \ Http \ Request)) #2 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Routing \ Router.php(964):Illuminate \ Routing \ Router-&gt; dispatchToRoute(Object(Illuminate \ Http \ Request)) #3 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php(73 8):Illuminate \ Routing \ Router-&gt; dispatch(Object(Illuminate \ Http \ Request)) #4 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php(70 8):Illuminate \ Foundation \ Application-&gt; dispatch(Object(Illuminate \ Http \ Request)) #5 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Session \ Middleware.php(72):Illuminate \ Foundation \ Application-&gt; handle(Object(Illuminate \ Http \ Request),1 ,真的) #6 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Cookie \ Queue.php(47):Illuminate \ Session \ Middleware-&gt; handle(Object(Illuminate \ Http \ Request),1 ,真的) #7 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Cookie \ Guard.php(51):Illuminate \ Cookie \ Queue-&gt; handle(Object(Illuminate \ Http \ Request),1 ,真的) #8 C:\ xampp \ htdocs \ Needa \ vendor \ stack \ builder \ src \ Stack \ StackedHttpKernel.php(23):Illuminate \ Cookie \ Guard-&gt; handle(Object(Illuminate \ Http \ Request),1,true ) #9 C:\ xampp \ htdocs \ Needa \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Application.php(60 6):Stack \ StackedHttpKernel-&gt; handle(Object(Illuminate \ Http \ Request)) #10 C:\ xampp \ htdocs \ Needa \ public \ index.php(49):Illuminate \ Foundation \ Application-&gt; run() #11 C:\ xampp \ htdocs \ Needa \ server.php(19):require_once(&#39; C:\ xampp \ htdocs ...&#39;) #12 {main} [] []
如果您查看我的Controller,那么您会注意到我在 postBloodDonation 函数中添加了验证,如果验证失败,此验证工作正常,但如果验证成功则验证 ,然后发布的数据没有插入我的数据库。在控制器中, else 语句没有执行,为什么会发生,是什么问题?
答案 0 :(得分:1)
尝试质量分配。并在模型中设置可填充数组, BloodDonner ::保存(支持::所有());
并且如果给出错误而不是将“Rquest和BloodDoner”添加到各自的命名空间... 并使用composer更新你的项目。
答案 1 :(得分:0)
如果你把
怎么办?Session::flash('global','it is ok');
//then redirect to home
在那个其他声明之后,你会看到它吗?
答案 2 :(得分:0)
尝试这种方法
$b = new BloodDonner();
$b->donner_name = $donner_name;
$b->date_of_birth = $date_of_birth;
$b->blood_group = $blood_group;
$b->location = $location;
$b->email = $email;
$b->phone_number = $phone_number;
$b->last_date_of_donation =
$ last_date_of_donation;
if($b->save()){
return Redirect::route
(&#39;捐款表格&#39;) - &gt; with(&#39; global&#39;,&#39;您已成功注册为Blood Donner!&#39;);
答案 3 :(得分:0)
在您的控制器中,尝试替换以下内容:
$donner_name = Input::get('donne_name');
与
$donner_name = Input::get('donner_name');
答案 4 :(得分:0)
问题可能是日期字段没有正确格式化以适应MySQL日期类型字段,幸运的是Laravel 5.1在Eloquent中内置了可以在执行插入和更新时将这些变换为正确格式的变换器。 / p>
您可以在此处了解如何使用此功能:https://laravel.com/docs/5.1/eloquent-mutators#date-mutators