duanfangfei5558 2014-07-09 12:29
浏览 64
已采纳

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

I want to save a token inside my tokens Table:

My store-Method of my TokenController.php looks like this:

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',
    ]);

Here is my Table in my view, which sends the data to the 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>

And this is how my database looks like (my Tokens Migration)

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();
    });
}

What is weird, is that my Seedfile saves everything perfectly inside my table:

My 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)
            ]);
        }
    }

}

So, it is really weird, because the NAME gets saved perfectly, but not the 'description' and not the 'tag', so that after my seeder is ready and I manually create tokens, everything gets saved, except the description and tag.

Here is how it looks like:

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

It seems like there must be just a word written wrong, but I checked it a hundred times and can't find the reason, why it is not saved.

Any help is greatly appreciated.

Kind Regards,

George

  • 写回答

1条回答 默认 最新

  • duandeng7132 2014-07-09 14:48
    关注

    In order to use the create method on a model, the properties which you wish to set must be designated with the fillable property. Here's the documentation.

    I don't know why it would work for seeding but not production code. Maybe seeding somehow bypasses the create method and just writes values into the database directly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?