I have an question I want to display all posts in a page
Here is my dynamodb
users Table: user_id | email | 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)
Post.php
class Post extends \BaoPham\DynamoDb\DynamoDbModel
{
public function user()
{
return $this->belongsTo('App\User');
}
protected $table = 'posts';
protected $fillable = array('id');
}
User.php
class User extends \BaoPham\DynamoDb\DynamoDbModel implements Authenticatable
{
use \Illuminate\Auth\Authenticatable;
public function posts()
{
return $this->hasMany('App\Post');
}
protected $table = 'users';
protected $fillable = array('email');
protected $primaryKey = 'user_id';
}
PostController.php
class PostController extends Controller
{
public function getDashboard()
{
$posts = Post::all();
return view('dashboard', ['posts' => $posts]);
}
dashboard.blade.php
@foreach($posts as $post)
<p>{{ $post->body }}</p>
by {{ $post->users->name}}
@endforeach
Error when load dashboard page
Trying to get property of non-object (View: resources\views\dashboard.blade.php)
but it works with just $post->body Thanks!