Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Table Relationship #24

Closed
qyqyqy03 opened this issue Aug 2, 2016 · 7 comments
Closed

Table Relationship #24

qyqyqy03 opened this issue Aug 2, 2016 · 7 comments
Labels

Comments

@qyqyqy03
Copy link

qyqyqy03 commented Aug 2, 2016

Hello, I have an question
I want to display all posts in a page

Here is my dynamodb

users Table: user_id | name | password
posts Table: id | body | user_id

user_id for me to link, so one user has many posts
I just simply use Auth::user()->user_id to get user id and request body to create a new post item

but how to foreach the following?

name(from users) : body(from posts)

@baopham
Copy link
Owner

baopham commented Aug 2, 2016

You can just do:

foreach ($users as $user) {
   $posts = Post::where('user_id', $user->user_id)->get();
}

@qyqyqy03
Copy link
Author

qyqyqy03 commented Aug 3, 2016

Yes Thank you so much, it got the data
I used {{ $users = App\User::where('user_id', $post->user_id)->get() }} in foreach in blade.php
and it show

[{"user_id":"123","email":"123@com","password":"1234","first_name":"Tony"}]

but how to get "Tony"?
$users.....[]??

@baopham
Copy link
Owner

baopham commented Aug 4, 2016

Just do $user = App\User::find($post->user_id) - you will get the one user record.

Closing since this question actually has nothing to do with this library. I suggest you reading up on Laravel Eloquent docs first.

@baopham baopham closed this as completed Aug 4, 2016
@qyqyqy03
Copy link
Author

qyqyqy03 commented Aug 4, 2016

and now I got this

{"user_id":"123","email":"123@com","password":"1234","first_name":"Tony"}

how can I get the first_name -- "Tony"?

@baopham
Copy link
Owner

baopham commented Aug 4, 2016

$user->first_name

@qyqyqy03
Copy link
Author

qyqyqy03 commented Aug 4, 2016

But it hv an error

Trying to get property of non-object

After define {{$user = App\User::find($post->user_id)}}
I can {{ $user }}
with the same result of

{"user_id":"123","email":"123@com","password":"1234","first_name":"Tony"}

@baopham
Copy link
Owner

baopham commented Aug 4, 2016

Try to debug by looking at the variable type. Sorry I can't help much on this as it has nothing to do with the library

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants