如何用4个字段填充中间表

时间:2019-05-06 12:11:03

标签: laravel many-to-many html-select

我创建了一个中间表,我将在其中收集4个数据,即培训师的ID,课程的ID,当天的ID和小时的ID,我必须在select中填充这些字段视图

这是查看代码

<div class="container mb-5">
    <div class="row">
        <div class="col-lg-4">
            <span>In quale giorno viene svolto questo corso ?</span>
            <select class="form-control" name="day_id[]">
                <option value="" selected></option>
                @foreach ($days as $day)
                    <option value="{{$day->id}}">{{$day->day}}</option>
                @endforeach
            </select>
        </div>
        <div class="col-lg-4">
            <span>In quale orario ?</span>
            <select class="form-control" name="hour_id[]">
                <option value="" selected></option>
                @foreach ($hours as $hour)
                    <option value="{{$hour->id}}">{{$hour->hour}}</option>
                @endforeach
            </select>
        </div>
        <div class="col-lg-4">
            <span>Scegli l'istruttore</span>
            <select class="form-control" name="trainer_id[]">
                <option value="" selected></option>
                @foreach ($trainers as $trainer)
                    <option value="{{$trainer->id}}">{{$trainer->name}} {{$trainer->surname}}</option>
                @endforeach
            </select>
        </div>
    </div>
</div>

这是控制器中的创建功能

public function store(Request $request)
{
    $data = $request->all();

    $image = Storage::disk('public')->put('courses', $data['logo_course']);
    $bgimage = Storage::disk('public')->put('courses/background', $data['bg_image']);

    $newCourse = New Course();

    $newCourse->name_course = $data['name_course'];
    $newCourse->lesmills = $data['lesmills'];
    $newCourse->description_course = $data['description_course'];
    $newCourse->logo_course = $image;
    $newCourse->bg_image = $bgimage;
    $newCourse->slug = Str::slug($data['name_course']);

    $newCourse->save();

    $newCourse->days()->attach($data['day_id']);
    $newCourse->hours()->attach($data['hour_id']);
    $newCourse->trainers()->attach($data['trainer_id']);

    return redirect()->route('courses.index');
}

这是课程模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Course extends Model
{
    protected $fillable = ['name_course', 'lesmills', 'description_course', 'logo_course', 'slug', 'bg_image'];

    public function trainers()
    {
        return $this->belongsToMany(Trainer::class)->withTimestamps();
    }

    public function days()
    {
        return $this->belongsToMany(Day::class)->withTimestamps();
    }

    public function hours()
    {
        return $this->belongsToMany(hour::class)->withTimestamps();
    }
}

这是教练机模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class Trainer extends Model
{
    protected $fillable = ['name', 'surname', 'description', 'image'];

    public function courses()
    {
        return $this->belongsToMany(Course::class)->withTimestamps();
    }

    public function hours()
    {
        return $this->belongsToMany(Hour::class)->withTimestamps();
    }

    public function days()
    {
        return $this->belongsToMany(Day::class)->withTimestamps();
    }
}

这是日间模式

namespace App;

use Illuminate\Database\Eloquent\Model;

class day extends Model
{
    public function hours()
    {
        return $this->belongsToMany(Hour::class)->withTimestamps();
    }

    public function courses()
    {
        return $this->belongsToMany(Course::class)->withTimestamps();
    }

    public function trainers()
    {
        return $this->belongsToMany(Trainer::class)->withTimestamps();
    }
}

这是小时模型

namespace App;

use Illuminate\Database\Eloquent\Model;

class hour extends Model
{
    public function days()
    {
        return $this->belongsToMany(Day::class)->withTimestamps();
    }

    public function courses()
    {
        return $this->belongsToMany(Course::class)->withTimestamps();
    }

    public function trainers()
    {
        return $this->belongsToMany(Trainer::class)->withTimestamps();
    }
}

1 个答案:

答案 0 :(得分:0)

我首先要检查以确保您期望接收的数据实际上是您在控制器中获得的数据:

dd($data);

从上方检查结果,以确保设置了hour_idday_idtrainer_id。如果一切看起来不错,我接下来要尝试的是更改元素名称以排除[]-假设您不需要数组格式的id

<div class="col-lg-4">
    <span>In quale giorno viene svolto questo corso ?</span>
    <select class="form-control" name="day_id">
        <option value="" selected></option>
        @foreach ($days as $day)
            <option value="{{$day->id}}">{{$day->day}}</option>
        @endforeach
    </select>
</div>
<div class="col-lg-4">
    <span>In quale orario ?</span>
    <select class="form-control" name="hour_id">
        <option value="" selected></option>
        @foreach ($hours as $hour)
            <option value="{{$hour->id}}">{{$hour->hour}}</option>
        @endforeach
    </select>
</div>
<div class="col-lg-4">
    <span>Scegli l'istruttore</span>
    <select class="form-control" name="trainer_id">
        <option value="" selected></option>
        @foreach ($trainers as $trainer)
            <option value="{{$trainer->id}}">{{$trainer->name}} {{$trainer->surname}}</option>
        @endforeach
    </select>
</div>