Agil
Agil

Reputation: 396

Trying to get data via related table in Laravel

What I'm trying to do is to show the posts that have been saved by the user in the profile. I will try to explain it as good as possible refering to my code. So:

public function userProfil($id)

I have the profile function which get the data from userprofile table. and inside I have the following code for saved data:

$authed = User::find($id);
$savedarticles = $authed->mysaves;
$allsavings = DB::select("Select * from article where id=$savedarticles->id");

But this code does not work like this anyway. I can do this instead:

$authed = User::find($id);
$savedarticles = $authed->mysaves;

But when I try to get articles from article table with the article_id of mysaves, it does not work such as this:

$allsaved= DB::table('article')->where('id', $savedarticles->article_id);

the error it gives is like:

Property [article_id] does not exist on this collection instance.

although savearticle table has article_id I can output it without the line above and in view I get them as:

@foreach($savedarticles as $savedarticle)
    <p>{{$savedarticle}}</p>
@endforeach

it gives me everything that is in the savearticle table and I can get do savedarticle->article_id and get article_id but can't get it in controller.

I am using Laravel 5.4.

Upvotes: 0

Views: 133

Answers (2)

Nadeem Latif
Nadeem Latif

Reputation: 174

What I have understood is that A USER has many POSTS and a POST belong to an article.

If this is true then you have to do following.

1: In USER model define a relation to get all posts. like below.

public function posts() {
   // Foreign key will be a key that is stored in posts table and represent the user MAY BE: user_id
   $this->hasMany(Posts::class, 'foreign_key', 'local_key')
}

This will allow you to get all posts belong to a user.

2: In posts, model defines a user relation like below.

public function user() {
   $this->belongsTo(User::class, 'foreign_key', 'local_key');
}

This will allow you to get a post User;

3: Now in your controller you will have something like this.

public function show($user_id) {

      // find a user with posts as eager loading(to avoid query again)
      $user = User::with(['posts'])->where('id', $user_id)->first();

      // get all posts that belong to this user
      $posts = $user->posts; 
   }

In controller show($user_id) method you will have a user data as well as user posts data. Now if you want to get a post relations then simply define as below. let say a post belongs to an article as well.

4: In posts, model defines a relation to get an article.

public function article() {
  // This will allow you to get a post artcle
  $this->belongsTo(Article::class, 'foreign_key', 'local_key');
}

Now you can get the article as well while finding a user. please see below. I am rewriting controller show action to give you a better understanding.

5: Get a user with user_id

public function show($user_id) {

// find a user with posts as eager loading(to avoid query again)
// eager loading for posts & post child, this will give you NOSQL at runtime and all data will come from one query. 
          $user = User::with(['posts', 'posts.article'])->where('id', $user_id)->first();

          // get all posts that belong to this user
          $posts = $user->posts; 
foreach($posts as $post) {
  $article = $post->article; // Child relation of post. 
}

   }

Hope you will understand the flow, you have to make sure models relation to work it perfectly. If you need further help please let me know.

Upvotes: 0

JoshKisb
JoshKisb

Reputation: 752

The error message Property [article_id] does not exist on this collection instance. means you are trying to get an attribute of a single instance but from a collection.

For example the collection could be like

[$article1, $article2, $article3]

therefore what you tried to do is something similar to

[$article1, $article2, $article3]->article_id

You are trying to get an attribute from a collection instead of a single instance.

As for your query, you can use where in sql statement to search for rows that match any item in an array

$allsaved= DB::table('article')->whereIn('id', $savedarticles->pluck('article_id')->all());

Upvotes: 1

Related Questions