Laravel没有采用我的数据库输入

时间:2015-12-04 13:37:28

标签: php mysql laravel-5.1 laravel-routing laravel-form

我试图在数据库表中插入条目,但它无法正常工作。它在两周前工作,但我不小心改变了一些东西。

我的相关模型,迁移和控制器都在这里

模型 - Profile.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    //
    protected $table='profile';

    protected $fillable=['user_id', 'Gender', 'Age', 'Address'];
}

迁移 -

<?php

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

class CreateProfileTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        //
            Schema::create('profile', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->unsigned();
            $table->foreign('user_id')->references('id')->on('users');
            $table->char('Gender', 1);
            $table->string('Age')->unique();
            $table->string('Address');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        //
    }
}

Controller - profileController.php

<?php

namespace App\Http\Controllers;


use Illuminate\Support\Facades\Auth;
use App\Profile;
use App\Http\Requests;
use Request;
use Carbon\Carbon;
//use App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;

class profileController extends Controller
{
    //
    public function index(){


        return view('pages.profile');
    }

    public function store(){
        $uid=Auth::user()->id;
        $input=Request::all();
        $input['user_id']=$uid;
        $profile = new Profile(array('user_id'=>$uid, 'gender'=>$input['gender'], 'age'=>$input['age'], 'address'=>$input['address']));
        return view('welcome');
    }
}

包含表单的页面 - pages / profile.blade.php

{!! Form::open(array('class' => 'form-horizontal col-xs-10 col-sm-6 col-md-4')) !!}
<!-- <form class="form-horizontal col-xs-10 col-sm-6 col-md-4 ">-->
<fieldset>

<!-- Form Name -->
<legend>User Profile</legend>

<!-- Multiple Radios -->
<div class="form-group">
  <label class="col-md-4 control-label" for="gender">Gender</label>
  <div class="col-md-4">
  <div class="radio">
    <label for="gender-0">
      <input type="radio" name="gender" id="gender-0" value="1" checked="checked">
      Male
    </label>
  </div>
  <div class="radio">
    <label for="gender-1">
      <input type="radio" name="gender" id="gender-1" value="2">
      Female
    </label>
  </div>
  </div>
</div>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="age">Age</label>  
  <div class="col-md-2">
  <input id="age" name="age" type="text" placeholder="Age" class="form-control input-md">

  </div>
</div>

<!-- Textarea -->
<div class="form-group">
  <label class="col-md-4 control-label" for="address">Address</label>
  <div class="col-md-8">                     
    <textarea class="form-control" id="address" name="address">Address</textarea>
  </div>
</div>

<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" for="submit"></label>
  <div class="col-md-4">
    <button id="submit" name="submit" class="btn btn-primary">Submit</button>
  </div>
</div>

</fieldset>
<!--</form>-->
{!! Form::close() !!}

另外,以防万一,我的route.php -

Route::get('profile', 'profileController@index');
Route::post('profile', 'profileController@store');

2 个答案:

答案 0 :(得分:1)

在迁移中,您使用大写列名称(即&#34; Gender&#34;),而在尝试填充模型时使用小写(&#34; gender&#34;)。此外,您永远不会保存您的模型:

$profile = new Profile(array('user_id'=>$uid, 'Gender'=>$input['gender'], 'Age'=>$input['age'], 'Address'=>$input['address']));    
$profile->save(); // <- You need to call save() to persist the model to your database.
return view('welcome');

答案 1 :(得分:1)

在:$profile->save();

wrtite:{!! Form::open(array('class' => 'form-horizontal col-xs-10 col-sm-6 col-md-4')) !!}

和行动? html,body {margin:0; padding:0; height:100%; width:100%;} #wrapper, .container-fluid, .fullscreen { height:100%; width:100%; }

阅读此http://laravelcollective.com/docs/5.1/html#form-model-binding