So I have 2 tables, articles and sub_categories. They are linked throug Eloquent: articles has many sub_categories's, and sub_categories belongsTo article. They are linked with foreign keys as such: in "article" categorie_id.
How do I retrieve the entire table data article where categorie is "DOG" for exemple Sorry for the abstraction, but this is the best way I can explain it? :D
article model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Articles extends Model
{
use SoftDeletes;
public function user() {
return $this->belongsTo('App\User') ;
}
public function sous_categories() {
return $this->belongsTo('App\SouCategories') ;
}
}
sub_categorie model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class SouCategories extends Model
{
public function categories() {
return $this->belongsTo('App\Categories') ;
}
public function articles() {
return $this->hasMany('App\Articles','cat_id') ;
}
}
in my controller i am trying to fetch data based on the foreign key on the sub_category and foreach sub category i am creating an a array like the mainslider contain articles that have a certain sub_category
public function index()
{
$infos = Infos::all();
$categories = Categories::all();
$articles=Articles::all();
$mainslider=Soucategories::with('articles')->get();
foreach($mainslider as $record){
dd($record->articles);
}
die();
return view('frontEnd.homepage',compact('infos','categories','articles','mainslider'));
}