vendredi 1 septembre 2017

How to get just one Table via Larvel Eloquent?

There are theree tables in my system.

  1. Students
  2. Articles
  3. categories

Student can write many articles and a article is belong to just one student. And A Article can have only one category.

Article Model

class Articles extends Model
{
    protected $fillable = ['id','title', 'body', 'students_id', 'created_at', 'updated_at'];
    protected $table = 'articles';

    public function students(){
        return $this->belongsTo('App\Students');
    }

    public function categories(){
        return $this->belongsTo('App\Categories');
    }
}

I have created above code because I needed to get articles list with who written by that article with category name.

For that I used $article_list = Articles::get(); in controller and it works perfectly.

Then again I needed to get article list(This time I don't need student name and category names, output of article table is more than enough)

But if I use $article_list = Articles::get(); it output article table joing with category and students table also.

Is there way to get just article table using Eloquent?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire