Laravel:无法将数据保存到表中

时间:2014-07-09 12:29:56

标签: php html laravel

我想在令牌表中保存一个令牌:

我的商店 - 我的TokenController.php的方法如下所示:

public function store() {

    $user = Sentry::getUser();
    $user = User::with('tokens')->where('id', $user->id)->first();

    Token::create([
        'user_id'     => $user->id,
        'name'        => Input::get('tokenname'),
        'description' => 'whatever',
        'tag'         => 'whatever1',
    ]);

这是我的视图中的表格,它将数据发送到store-method:

    <table>
    {{Form::open(array('route' => array('admin.referencing.store', $user->id), 'method' => 'POST', 'id'=>'tokenCreateForm'))}}

        <div class="form-group">
            {{ Form::input('number', 'numberoftokens', false, array('placeholder' => 'Number of Tokens', 'id' => 'numberoftokens')) }} 
        </div>

        <div class="form-group">
            {{ Form::input('text', 'tokenname', false, array('placeholder' => 'Name of Token', 'id' => 'tokenname')) }} 
        </div>

        <div class="form-group">
            {{ Form::input('text', 'tokendescription', false, array('placeholder' => 'Description', 'id' => 'tokendescription')) }} 
        </div>

        <div class="form-group">
            {{ Form::input('text', 'tokentag', false, array('placeholder' => 'Tag', 'id' => 'tokentag')) }} 
        </div>

        <div class="form-group">
            <button class="btn btn-primary" type="submit">
                <i class="fa fa-save"></i>
                Create
            </button>
        </div>

    {{Form::close()}}
    </table>

这就是我的数据库的样子(我的令牌迁移)

public function up()
{
    Schema::create('tokens', function(Blueprint $table)
    {
        $table->increments('id');
        $table->integer('user_id')->unsigned()->index();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('name', 60)->index();
        $table->string('description');
        $table->string('tag');
        $table->timestamps();
    });
}

奇怪的是,我的Seedfile将所有内容完美地保存在我的桌子中:

我的TokenTableSeeder:

 class TokenTableSeeder extends Seeder {

    public function run()
    {
        $faker = Faker::create();

        Token::truncate();

        foreach(range(1, 10) as $index)
        {
            Token::create([
                'user_id'     => rand(1,4),
                'name'        => $faker->country,
                'description' => $faker->name,
                'tag'         => rand(1,4)
            ]);
        }
    }

}

所以,这真的很奇怪,因为NAME得到了完美的保存,但没有得到“&#39;描述”。而不是&#39;标签&#39;,以便在我的播种机准备就绪并且我手动创建令牌后,除了描述和标签之外,所有内容都会被保存。

以下是它的样子:

http://i.imgur.com/x3dRF6d.jpg

似乎一定有一个单词写错了,但我检查了一百次而无法找到原因,为什么没有保存。

非常感谢任何帮助。

亲切的问候,

乔治

1 个答案:

答案 0 :(得分:1)

为了在模型上使用create方法,必须使用fillable属性指定要设置的属性。 Here's the documentation

我不知道为什么它适用于播种但不适用于生产代码。也许播种以某种方式绕过create方法并直接将值写入数据库。