Warm tip: This article is reproduced from serverfault.com, please click

How to attach when enter a text in laravel?

发布于 2020-11-28 00:11:23

After login a user, The user enters this skills form, selects the skills.

skills

A user can enter multiple skills, and save in database.

The field of name

  • skill_name
  • level

And in table

  • skill_name
  • level
  • user_id

I don't know if the skills and skill_user table should be, or not, I don't know.

What would you do if you were me?

Can you show me the cleanest coding?

public function up()
{
    Schema::create('skills', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->bigInteger('user_id')->unsigned();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->string('skill_name');
        $table->smallInteger('level');
        $table->timestamps();
    });

    Schema::create('skill_user', function (Blueprint $table) {
        $table->bigInteger('skill_id')->unsigned()->nullable();
        $table->foreign('skill_id')->references('id')->on('skills')->onDelete('cascade');
        $table->bigInteger('user_id')->unsigned()->nullable();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->unique(['skill_id' , 'user_id']);
    });
}

blade

showDynamicExperimental();
function showDynamicExperimental()
{
    let html = '' +
        '@foreach(auth()->user()->skills as $skill)\n'+
        '<div class="col-md-6 position-relative">\n' +
        '<i class="fas fa-times text-danger position-absolute"></i>' +
        '<div class="row">\n' +
        '<div class="col-md-8">\n' +
        '<div class="form-group">\n' +
        '<label for="skill_name">skill_name</label>\n' +
        '<input type="text" id="skill_name" name="skill_name[]" class="form-control" value="{{ $skill->skill_name }}">\n' +
        '</div>\n' +
        '</div>\n' +
        '<div class="col-md-4">\n' +
        '<div class="form-group">\n' +
        '<label for="level">level</label>\n' +
        '<select class="form-control" id="level" name="level[]">\n' +
        '<option value="1" {{ $skill->level == 1 ? 'selected' : '' }}>★☆☆☆☆</option>\n'+
        '<option value="2" {{ $skill->level == 2 ? 'selected' : '' }}>★★☆☆☆</option>\n'+
        '<option value="3" {{ $skill->level == 3 ? 'selected' : '' }}>★★★☆☆</option>\n'+
        '<option value="4" {{ $skill->level == 4 ? 'selected' : '' }}>★★★★☆</option>\n'+
        '<option value="5" {{ $skill->level == 5 ? 'selected' : '' }}>★★★★★</option>\n'+
        '</select>\n'+
        '</div>\n' +
        '</div>\n' +
        '</div>\n' +
        '</div>\n ' +
        '@endforeach';
    $('#showExperimental').append(html);
}

Controller

$user->skills()->detach();

if ($request->skill_name) {
    foreach ($request->skill_name as $key => $value) {
        $user->skills()->attach($value, [
            'user_id' => auth()->id(),
            'level' => $request->level[$key],
        ]);
    }
}

User.php

public function skills()
{
    return $this->belongsToMany(Skill::class)->withPivot('skill_name');
}

I get this error.

error

Questioner
irankhostravi
Viewed
12
Phil 2020-11-28 08:59:27

It seems that there isn't a level column in the skill_user table.

I think the skill table should just store the id and skill name. Then you add the level column in the skill_user and use skill_id and level to track the user level.