Итак, согласно комментариям, ваши таблицы выглядят так:
+--------+ +--------------+ +------------------------------+
|filiere | |mode_formation| |mode_filiere |
+--------+ +--------------+ +------------------------------+
|id (pk) | |id (pk) | |filiere_id (fk to filiere) |
|intitule| |intitule | |mode_id (fk to mode_formation)|
+--------+ +--------------+ +------------------------------+
Исходя из этого и вашего кода, я предполагаю следующее:
Filiere
В модели используется таблица filiere
. - Для таблицы
mode_formation
нет модели. - Модель для таблицы
mode_filiere
отсутствует.
Шаг 1: Создайте модель для таблицы mode_formation
.
- Запустите команду
php artisan make:model ModeFormation
- Просто, чтобы убедиться, отредактируйте файл модели (по умолчанию расположен в
app/ModeFormation.php
), чтобы добавить таблицу, которую она представляет.
Шаг 2: Добавить belongsToMany
отношение между Filiere
и ModeFormation
моделями
Шаг 3: Использовать eager loading
в вашем контроллер, чтобы избежать выполнения стольких запросов
Шаг 4: Используйте отношения в своем представлении
В конце ваши модели должны выглядеть следующим образом:
# app/Filiere.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Filiere extends Model
{
protected $table = 'filiere';
public function mode_formations()
{
return $this->belongsToMany(ModeFormation::class, 'mode_filiere', 'filiere_id', 'mode_id');
}
}
# app/ModeFormation.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class ModeFormation extends Model
{
protected $table = 'mode_formation';
public function filieres()
{
return $this->belongsToMany(Filiere::class, 'mode_filiere', 'mode_id', 'filiere_id');
}
}
Эта часть в вашем контроллере должна выглядеть так:
// you only need this one variable for what you're trying to accomplish
$filieres = Filiere::with('mode_formations')->get();
И ваш взгляд:
@foreach($filieres as $filiere)
<tr class="item{{ $filiere->id }}">
<td style="font-size: 13px;">
<a href="f/{{ $filiere->id }}">{{ $filiere->intitule }}</a>
</td>
@foreach($filiere->mode_formations as $mode_formation)
<td style="font-size: 13px;">{{ $mode_formation->intitule }}</td>
@endforeach
</tr>
@endforeach