i worked with angular for front end and and i want to perform CRUD with laravel
i have two tables users and students, students table have a foreign key referencing to users table.
i want to insert the data that comes from one form, i tried this:
student model
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Etudiant extends Model
{
protected $table = 'students';
protected $primaryKey = 'idStudent';
public $timestamps = false;
protected $fillable = [
'dateNaissance',
'filier',
'niveauEtude',
'cne',
'specialite'
];
public function user(){
return $this->belongsTo(AppModelsUser::class);
}
use HasFactory;
}
user model
<?php
namespace AppModels;
use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel;
class Utilisateur extends Model
{
protected $table = 'users';
protected $primaryKey = 'idUser';
public $timestamps = false;
protected $fillable = [
'nom',
'prenom',
'password',
'email',
'numTel'
];
public function student(){
return $this->hasMany(AppModelsStudent::class);
}
use HasFactory;
}
Student Cotroller
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use AppModelsUsers;
use AppModelsStudent;
class StudentController extends Controller
{
protected $student;
protected $user;
protected $studentData;
protected $userData;
public function __construct(){
$this->student = new Student();
$this->user = new Utilisateur();
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$studentData = array_diff_key($request->all(), array(['nom', 'prenom', 'password', 'email', 'numTel']));
$userData = array_diff_key($request->all(), array(["dateNaissance", "filier", "niveauEtude", "cne", "specialite"]));
$this->user->student()->create($userData);
$this->student->user()->create($studentData);
return [];
}}
Error i get
Column not found: 1054 Unknown column ‘utilisateur_idUser’ in ‘field list’ (Connection: mysql, SQL: insert into etudiant
(dateNaissance
, filier
, niveauEtude
, cne
, specialite
, utilisateur_idUser
) values (12/12/12, mip, bac+3, m130118922, MA, ?))