Я создал промежуточную таблицу, куда я иду, чтобы собрать 4 данных, идентификатор тренера, идентификатор курса, идентификатор дня и идентификатор часа, я должен заполнить эти поля с помощью выбора напредставление
Это код представления
<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();
}
}