это мой первый Laravel проект, и мне нужно сделать LMS (систему управления обучением). У меня есть таблица курса и таблица пользователя.
Вот код моего контроллера:
public function afficheFormation($id)
{
$formation = DB::table('formation')->select('formation.*')->where('id', $id)->first();
$cours = DB::table('Course')->join('Course_formation', 'course_id', '=', 'Course.id')
->select('Course.*')->where('formation_id', $id)->get();
return view('formation', compact('formation', 'cours'));
}
Вот код моего вида:
<tr>
<td class="align-middle">{{ $cours->name }}</td>
<td class="align-middle">{{ $cours->level}}</td>
<td class="align-middle">{{ $cours->user() }}</td>
</tr>
@endforeach
@endif
У курса есть внешний ключ Professor_id, и я хочу отобразить имя пользователь, который создал курс, но имеет некоторые ошибки
Это моя модель курса:
class Course extends Model
{
protected $table = 'cours';
public $timestamps = true;
protected $fillable = [
'name', 'lien', 'type', 'level', 'theme_id', 'prof_id'
];
public function user()
{
return $this->hasOne('App\Models\User');
}
}
, и это моя модель пользователя:
class User extends Authenticatable
{
use Notifiable;
protected $table = 'users';
public $timestamps = true;
protected $fillable = [
'pseudo', 'email', 'password', 'name', 'prenom', 'image'
];
public function roles()
{
//return $this->belongsToMany('App\Models\Role', 'user_role', 'user_id', 'role_id');
return $this->belongsToMany(Role::class);
}
public function cours()
{
return $this->belongsToMany('App\Models\Course', 'professor_id', 'id');
}
}
спасибо за помощь .