2
I’m trying to make the relationship between three tables in the Latvian. With 2 tables I managed, thanks to the help of the forum, but I’m not able to make it work with 3 tables. the relationship is as follows: I have 3 tables:
News, Photos and Unit (school units)
tabela noticia -> id_noticias
tabela fotos-> id_fotos e id_noticias
tabela noticia_unidade
The relationship news with photos is ok: on the main page appear 2 news and a picture of each. Now I need it to be filtered by unit: On the college page appear only the news from unit 2, for example.
Follow the Models and Controllers:
Controller
public function index(){
$not_faculdade = Noticia::with(['foto' => function($query){
$query->get()->first();
}])
->with(['unidade' => function($query2){
$query2->where('id_unidade','2')->get();
}])
->orderBy('id_noticias','DESC')
->take(2)
->get();
// dd($not_faculdade);
return view('pages_faculdade.noticia')->with('not_faculdade',$not_faculdade);
}
Models
class Noticia extends Model
{
protected $table = 'noticias';
protected $primaryKey = 'id_noticias';
public $timestamps = false;
protected $dates = ['data'];
protected $fillable =[
'texto',
'titulo',
'legenda',
'pasta',
'subtitulo',
'evento',
'titulo_evento'
];
public function foto()
{
//return $this->hasMany(Foto::class);
return $this->hasMany('App\Foto','id_noticia','id_noticias');
}
public function unidade()
{
//return $this->hasMany(Foto::class);
return $this->belongsTo('App\Unidade','id_noticias','id_noticia');
}
}
class Unidade extends Model
{
protected $table = 'noticia_unidade';
// protected $primaryKey = 'id_noticia, id_unidade';
public $timestamps = false;
protected $dates = ['deleted_at'];
protected $fillable = [
'id_unidade',
'id_noticia'
];
public function noticias()
{
return $this->hasMany('App\Noticia','id_noticias','id_noticia');
}
}
View
@foreach ($not_faculdade as $key=> $not)
<div class="col-md-6">
<div class="panel-heading">
<div class="painel_foto"><img src={{asset('public/'.$not->foto[0]->endereco)}}></div>
<h4>{{ $not->titulo }}</h4>
<p align="justify">
<a href="#" class="noticia">
{{$texto = substr($not->texto,0,150)." ..."}}
</a>
</p>
</div>
</div>
@endforeach
As I said, the 2 news with the "cover" photo usually appear, but the filter does not work.
If I give a dd($not_faculdade)
, is shown the apparently right relationships!
What could be wrong?
Follow relationship (I did it the way I could because I don’t know how to work with these tools)
1 news owns several photos belonging to a single news 1 news can appear in multiple units and each unit can contain various news
What happens? Not filtering by ID unit 2?
– Diego Souza
It’s not! Return all news, no filter!
– Gisele Passoni
Aren’t the relationships wrong? if you could put in your question the 3 tables and their relationships (the diagram of these three tables with relation would be sufficient)?
– novic
@Virgilionovic, I tried to draw the diagram, but I can’t use the tools to do it.
– Gisele Passoni
Cool relationship is. Many.para many between drive and news is wrong relationships but beyond said what intended, bring the news that???
– novic
I intend to bring the news regarding each unit. For example: The college is unit 2, the college is unit 3... then bring on the college page all the news that are unit 2.... and they’re all showing up, unfiltered...
– Gisele Passoni
Got it @Giselepassoni but, I’m going to put on how relationships should be for it to work! OK?
– novic
Okay, @Virgilionovic
– Gisele Passoni