【问题标题】:Laravel not taking my Database InputLaravel 不接受我的数据库输入
【发布时间】:2016-03-09 11:06:17
【问题描述】:

我正在尝试在数据库表中插入条目,但它不起作用。两周前它还在工作,但我觉得我不小心改变了一些东西。

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

模型 - 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()
    {
        //
    }
}

控制器 - 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');

【问题讨论】:

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


    【解决方案1】:

    下:$profile = new Profile(array('user_id'=&gt;$uid, 'gender'=&gt;$input['gender'], 'age'=&gt;$input['age'], 'address'=&gt;$input['address']));

    写:$profile-&gt;save();

    和行动? {!! Form::open(array('class' =&gt; 'form-horizontal col-xs-10 col-sm-6 col-md-4')) !!}

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

    【讨论】:

      【解决方案2】:

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

      $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');
      

      【讨论】:

      • 使用后$profile->save();我收到此错误 - SQLSTATE[42S22]: Column not found: 1054 Unknown column 'updated_at' in 'field list' (SQL: insert into profile (user_id, updated_at, created_at) 值 ( 3、2015-12-04 16:28:40、2015-12-04 16:28:40))
      • public $timestamps = false; 放在模型的顶部。
      • 很高兴听到,肖恩!编码愉快!
      猜你喜欢
      • 1970-01-01
      • 2016-10-05
      • 2021-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-06
      相关资源
      最近更新 更多