Laravel 5.3 PHP 5.4 MySQL 5.55
I am struggling with Laravel 5 nested models with a parent-child relationship. I have a page, let's call it the Article, where I get elements in an certain order. Text - it is just a text entity. But Album consist of Stacks (from 1 to n) in an certain order. Any Stack consist of Images (from 1 to n) in an certain order. My design for DB is
Articles
id | name | cover_image_id
class Articles extends Model
public function components (){
return $this->hasMany('App\Component')->orderBy('position');
}
public function image(){
return $this->belongsTo('App\Image');
}
}
Components
id | article_id | type | position
class Component extends Model
{
public function texts (){
return $this->hasOne('App\Text');
}
public function albums (){
return $this->hasOne('App\Album');
}
}
Texts
id | component_id | content
class Component extends Model
{
public function texts (){
return $this->hasOne('App\Text');
}
public function albums (){
return $this->hasOne('App\Album');
}
}
Albums
id | component_id | caption | description
class Album extends Model
{
public function stacks (){
return $this->hasMany('App\Stacks')->orderBy('position');
}
public function component()
{
return $this->hasOne('App\Component', 'id', 'component_id');
}
}
Stacks
id|album_id|cover_image_id|caption|description | position | images_amount
class Stacks extends Model
{
public function images (){
return $this->hasMany('App\Image')->orderBy('position');
}
public function image(){
return $this->belongsTo('App\Image');
}
}
Images
id | stack_id | image_name | position
class Image extends Model
{
public function stack()
{
return $this->hasOne('App\Stacks');
}
}
I don't have trouble to show content at parent-child order like.
public function show(Article $article){
@foreach($article->components as $key => $component)
@if($component->type=='text')
<p></p>
@endif
@if($component->type=='album')
@foreach($component->albums->stacks as $stack)
@foreach($stack->images as $image)
<img data-content-type="image/jpeg" width="630" src="/content/articles////" />
@endforeach
@endforeach
@endif
@endforeach
}
Even if it is wrong way, it works.
The first difficulty arose when I tried to get cover_image for a given article/stack on the output page of the all articles. But I fixed it by adding to the hasMany additional hasOne relation.
@foreach
<img alt="" src="/content/articles/">
@endforeach
Now problem, I want to make search of stacks by title through all articles with pagination. Now problem, I want to make search of stacks by title of articles, through all articles, with pagination by stacks. I cant find the solution and I even don't know, may be I need redesign DB or make addition field in current DB (an example fields: article_title, album_id, component_id and article_id in the Stack table. But I realy do not want to duplicate fields). Sorry for my English. Any ideas?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2lAnUVP
via IFTTT
Aucun commentaire:
Enregistrer un commentaire