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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?