Multi site news system: setup complexity problem
I am building a multi site news application. So, for instance: while creating a news item (article) I can select multiple sites where I want the item to be shown. Each site has his own "categories" and "dossiers".
Now, I have several models but I repeatedly questioning myself I this is the right approach.
Article Site Category Dossier (= file, not a physical file, but a file as in "part of larger collection of items around a certain subject". Kind of tag.)
An article is attached to 1 or more sites -> "ArticleSite" model
In my Article model I have this pivot relation defined:
public function sites()
{
return $this->belongsToMany('App\Site')->withTimestamps()->using('App\ArticleSite');
}
The same for my Site model:
public function articles()
{
return $this->hasMany('App\Article')->withTimestamps()->using('App\ArticleSite');
}
I created a ArticleSite model for the relationship.
class ArticleSite extends Pivot
{
protected $table = 'article_sites';
public function dossiers()
{
return $this->belongsToMany('App\Dossier', 'article_site_dossiers', 'articlesite_id', 'dossier_id');
}
}
As you can see, I created a method "dossiers" on the ArticleSite model because a "dossier" is related to article+site. On site A it can be attached to dossier "X" while the same article on site B it can be attached to dossier "Y". Hence, this setup. I am also planning to do this for the categories. Each site holds different categories.
Now, I am having trouble with the dossiers() method. When saving a new article, I can the exception:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'articlesite_id' cannot be null (SQL: insert into article_site_dossiers (articlesite_id, dossier_id) values (?, 1), (?, 5))
This is the part of the ArticleController save method
$articleSite->save();
if (isset($request->input('dossiers')[$articleSiteId]))
{
$articleSite->dossiers()->attach($request->input('dossiers')[$articleSiteId]);
}
(On the frontend I render multiple form sections for each selected site. That's why I use the brackets notation.)
I save the ArticleSite model, so there must be an Id available. I don't get it...
Am I trying to achieve the right setup, or am I trying to get something to work that shouldn't work in the first place? Are there other ways to achieve what I want?
Please or to participate in this conversation.