简体   繁体   中英

Laravel: Can't Save Data into Table

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM