duanfangfei5558 2014-07-09 04: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 06: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.

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

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部