向DB添加数据失败,没有错误 - Laravel

时间:2016-10-23 14:46:35

标签: php database laravel phpmyadmin laravel-5.3

我在向数据库添加信息时遇到问题。我没有收到任何错误,但每次我尝试验证或向表格添加信息时,我都会被重定向到主页面“/”并且没有任何内容可以添加。我真的希望你能帮助我:

PagesController:

    <?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Kids;
use App\Guardians;
use App\Donations;
use App\Expenses;
use App\Food;
use App\Medicines;
use App\Inventory;
use App\User;
use App\Http\Requests;
use App\Http\Controllers\Controller;


class PagesController extends Controller
{

     public function viewAddPage(){

        return view('add');
    }

    public function storeKids(Request $request)
    {
    $validation= array(
                      'KidID'=>'required',
                      'fname'=>'required',
                      'lName'=>'required',
                      'DateOfBirth'=>'required',
                      'Sex'=>'required',
                      'Age'=>'required',
                      'SchoolName'=>'required',
                      'SchoolGrade'=>'required',
                      'SchoolGroup'=>'required',
                      'BloodType'=>'required',
                      'Allergies'=>'required',
                      'enroll_date'=>'required',
                      'exit_date'=>'required',
                      'created_by'=>'required',
                      'updated_by'=>'required'

                          );
        $v1= Validator::make(Input::all(),$validation);

        if( $v1->fails())
        {
        return Redirect::to('add/list/')->withErrors($v1);
        }
        else
        { $poststudent=$request->all();
          $data = array('KidID'=>$poststudent['KidID'],
                         'fname'=>$poststudent['fname'],
                         'mName'=>    $poststudent['mName'],
                    'lName'=>    $poststudent['lName'], 
                         'DateOfBirth'=>   $poststudent['DateOfBirth'],
                         'Sex'=>   $poststudent['Sex'],
                         'Age'=>   $poststudent['Age'],
                         'SchoolName'=>   $poststudent['SchoolName'],
                         'SchoolGrade'=>   $poststudent['SchoolGrade'],
                         'SchoolGroup'=>   $poststudent['SchoolGroup'],
                         'BloodType'=>   $poststudent['BloodType'],
                         'Allergies'=>   $poststudent['Allergies'],
                         'enroll_date'=>   $poststudent['enroll_date'],
                         'exit_date'=>   $poststudent['exit_date'],
                         'created_by'=>   $poststudent['created_by'],
                         'updated_by'=>   $poststudent['updated_by']

                         );

        $check=0;
        $check=DB::table('kids')->insert($data);

        if($check > 0)
        {
        return Redirect::to('kids/list/');
        }
        else
        {
        return Redirect::to('add/list/');
        }

        }
    }
}

web.php(路线):

<?php
Route::get('/', function(){
return view('welcome');
});

Route::post('/signin',[
'uses'=>'UserController@postSignIn',
'as'=>'signin'
]);

Route::get('user/logout',function(){
Auth::logout();
return redirect('/');
});

Route::get('/home', [
'uses'=> 'UserController@getDashboard',
'as'=> 'home'
]);

Route::get('kids/list', 'PagesController@viewKidsList');
Route::get('guardians/list', 'PagesController@viewGuardiansList');
Route::get('donations/list', 'PagesController@viewDonationsList');
Route::get('expenses/list', 'PagesController@viewExpensesList');
Route::get('food/list', 'PagesController@viewFoodList');
Route::get('medicines/list', 'PagesController@viewMedicinesList');
Route::get('inventory/list', 'PagesController@viewInventoryList');
Route::get('users/list', 'PagesController@viewUsersList');
Route::get('add/list','PagesController@viewAddPage');
Route::post('/save', array('uses'=>'PagesController@storeKids'));

查看(add.blade.php):

@extends('menu')

@section ('add')

<style>

.imagen{
width:400px;
height:auto;
margin-top: 10px;
margin-left: 40px;
margin-bottom: 10px;
}

.user{
position:absolute;
margin-top: -70px;
right:20px;
font-size:14px;
font-family:"courier";
}

</style>

        <form class ="form" action="<?=URL::to('/save')?>" method="POST">
        <h3><center>New kid Information</center></h3>

             <input type="hidden" name="_token" value="{{ csrf_token() }}">
             <div class="form-group">
                <input type="text" name="KidID" id="KidID" placeholder="Kid ID" class = "form-control">

                <br>

                <input class="form-control" type="text" name="fname" id="fname" placeholder="First Name">
                <br>

                <input class="form-control" type="text" name="mName" id="mName" placeholder="Middle Name">
                <br>

                <input class="form-control" type="text" name="lName" id="lName" placeholder="Last Name">
                <br>

                <input class="form-control" type="text" name="DateOfBirth" id="DateOfBirth" placeholder="Date Of Birth">
                <br>

                <input class="form-control" type="text" name="Sex" id="Sex" placeholder="Sex">
                <br>

                <input class="form-control" type="text" name="Age" id="Age" placeholder="Age">
                <br>

                <input class="form-control" type="text" name="SchoolName" id="SchoolName" placeholder="School Name">
                <br>

                <input class="form-control" type="text" name="SchoolGrade" id="SchoolGrade" placeholder="School Grade">
                <br>

                <input class="form-control" type="text" name="SchoolGroup" id="SchoolGroup" placeholder="School Group">
                <br>

                <input class="form-control" type="text" name="BloodType" id="BloodType" placeholder="Blood Type">
                <br>

                <input class="form-control" type="text" name="Allergies" id="Allergies" placeholder="Allergies">
                <br>

                <input class="form-control" type="text" name="enroll_date" id="enroll_date" placeholder="Enroll Date">
                <br>
                 <input class="form-control" type="text" name="exit_date" id="exit_date" placeholder="Enroll Date">
                <br>

                <input class="form-control" type="text" name="created_by" id="created_by" placeholder="Created By">
                <br>
                 <input class="form-control" type="text" name="updated_at" id="updated_at" placeholder="Enroll Date">
                <br>
           <input type="submit" value="Submit" name="save" class="text" />

        </form>
@endsection

儿童表:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateKidsTable extends Migration
{

public function up()
{
    Schema::create('kids', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('KidID')->unique();
        $table->string('fname');
        $table->string('mName');
        $table->string('lName');
        $table->date('DateOfBirth');
        $table->string('Sex');
        $table->tinyInteger('Age');
        $table->string('SchoolName');
        $table->tinyInteger('SchoolGrade');
        $table->char('SchoolGroup');
        $table->string('BloodType');
        $table->longText('Allergies');
        $table->date('enroll_date');
        $table->date('exit_date');
        $table->string('created_by');
        $table->timestamps();
    });
}

 public function down()
{
    Schema::drop('kids');
}

}

孩子们

namespace App;

use Illuminate\Database\Eloquent\Model;

class Kids extends Model
{

protected $table = 'kids';

}

我一直在寻找这个问题好几天了,我真的不知道它为什么不存储任何东西,它一直把我送到“/”网页。

3 个答案:

答案 0 :(得分:0)

如果您将可填充数组添加到Kids模型

protected $fillable = ['fName', 'mName','lName'...];

然后,您可以使用eloquent插入记录,这比查询构建器简单得多:

$kids = Kids::create($request->all());

您的验证器非常复杂,请将其移至FormRequest:

php artisan make:request KidsRequest

将验证数组添加到rules()中的Requests\KidsRequest方法,并确保将authorize()方法设置为返回true。

您现在可以在商店方法中输入提示:

public function storeKids(KidsRequest $request)

然后您将自动应用验证。

现在我们有以下方法:

public function storeKids(KidsRequest $request)
{
        $kids = Kids::create($request->all());

        return Redirect::to('add/list/');      
}

我认为你会同意的是更加整洁!

如果您坚持使用命名约定,那么您的代码有点不正统:index()show()store()edit()等&#39;您可以使用资源路由,这样可以防止必须单独列出每个路由,并尝试保持您的字段名称在db格式上保持一致,对于db字段,la case中最好使用snake case(例如last_name)。

如果您正在努力了解其中的一些内容,那么laracasts

就会有一些优秀的教程

答案 1 :(得分:0)

肯定你需要关注@craig_h回答并注意你在验证语句中需要updated_by并且它不是在迁移中进行的 所以你要添加它

答案 2 :(得分:0)

您应该在代码中的某些位置添加dd(),以确保您实际执行了良好的控制器操作。我怀疑你的路线有问题,可能是一些auth中间件或其他东西。