I am a nube to Laravel and I tryed erally hard to figure out where the issue is but cant seems to find it. Thanks in advance to everyone..
This is the error: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (homestead
.articles_tag
, CONSTRAINT articles_tag_tag_id_foreign
FOREIGN KEY (tag_id
) REFERENCES tags
(id
) ON DELETE CASCADE) (SQL: insert into articles_tag
(articles_id
, created_at
, tag_id
, updated_at
) values (4, 2015-10-05 09:33:18, 0, 2015-10-05 09:33:18))
This is the Tag eloquent model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model { protected $fillable = array();
//get the articles associated with the given tag
public function articles(){
return $this->belongsToMany('App/Article');
}
}
This is the Articles eloquent model:
namespace App;
use Illuminate\Database\Eloquent\Model; use Carbon\Carbon;
class Articles extends Model { protected $fillable = [ 'title', 'body', 'published_at', 'user_id' ];
protected $dates = ['published_at'];
public function scopePublished($query){
$query->where('published_at', '<=', Carbon::now());
}
public function scopeUnpublished($query){
$query->where('published_at', '>', Carbon::now());
}
public function setPublishedAtAttribute($date){
$this->attributes['published_at'] = Carbon::parse($date);
}
//An article is owned by a user
public function user(){
return $this->belongsTo('App\User');
}
//An article has many tags
//get the tags associated with the tables
public function tags(){
return $this->belongsToMany('App\Tag')->withTimestamps();
}
/*
public function setPasswordAttribute($password){
$this->attributes['password'] = mcrypt($password);
} //key is to user attributes
*/
}
This is my db schema:
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
class CreateTagsTable extends Migration {
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->timestamps();
});
//we are trying to connect articles and tags
//really it sould we article_tag not articles_tag ;)
Schema::create('articles_tag', function (Blueprint $table) {
$table->integer('articles_id')->unsigned()->index();
$table->foreign('articles_id')->references('id')->on('articles')->onDelete('cascade');
$table->integer('tag_id')->unsigned()->index();
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
$table->timestamps();
});
}
public function down()
{
Schema::drop('tags');
Schema::drop('articles_id');
}
}
this is is articels schema :
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration;
class Articles extends Migration {
public function up()
{
Schema::create('articles', function(Blueprint $table){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->string('title');
$table->text('body');
$table->timestamps();
$table->timestamp('published_at');
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}
public function down()
{
Schema::drop('articles');
}
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire