dimanche 17 avril 2016

Laravel 5.1 get data from connected table

In my app admin users can create articles. Articles table is: ID | user_id | title ...

For every Article other user (which not admin) can post an offer and Offer table is: ID | user_id(not admin) | article_id | price ...

Now I need to get all users for an admin user which post Offer to their Article... I try:

 public function userbase()
    {
        $id = Auth::user()->id;//get admin id
        $articles = Article::where('user_id', $id)->get();//select all admin articles
        foreach ($articles as $article) {

         $users = Offer::where('article_id', $article['id'])->orderBy('created_at', 'desc')->get(); //get all admin users

        }


         return $users;


    }

but I get just: []

also my Model is: Article:

public function offers() {
  return $this->hasMany('App\Offer');
}

Offer:

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

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

So how I can get all user details which was posted offer to an admin user?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire