I have three columns: article, type_article and theme.
The article table has two foreign keys, which are type and theme.
Article migration
public function up()
{
Schema::create('article', function (Blueprint $table) {
$table->bigIncrements('id_article');
$table->unsignedBigInteger('type');
$table->unsignedBigInteger('theme');
$table->string('titre');
$table->string('contenu');
$table->date('date_creation');
$table->foreign('type')->references('id')
->on('type_article')->onDelete('cascade');
$table->foreign('theme')->references('id')->on('theme')
->onDelete('cascade');
});
}
Article model
class Article extends Model
{
use HasFactory;
protected $table = 'article';
protected $primaryKey = 'id_article';
public $timestamps = false;
public function type_article()
{
return $this->belongsTo(Type_Article::class);
}
public function theme()
{
return $this->belongsTo(Theme::class);
}
}
Type_article and Theme has this (except the end 'type' for 'type_article' and 'theme' for 'theme' :
public function article()
{
return $this->hasMany('App\Article', 'type');
}
They both have a primary key id.
What I'm trying to do here is to fill a simple form with a title, an article type (with foreach loop inside my view (it's working), a theme, and content.
As you can see, inside the error message (SQL: insert into article (type_article_id, theme, titre, contenu, date_creation). There are only five elements; the id of the article is not there. II do not have a type_article_id inside my table, I don't really know where it comes from, but I do create an article_id
$table->foreign('type')->references('id')->on('type_article')->onDelete('cascade');it is inbuilt functionality of the Laravel to search for the column refer as it's foreign+table+name_id. the best way to remove foreign fey from migration and handle it at the code level. - AnkitunsignedBigIntegerand putint+ remove the lines withforeign? - Leoncode level? Where should I do that then ? - Leon