Sorry for such a stupid question but im on deadend right now, Im working on a personal project using Laravel (first time btw) and i ran into some problem.
I tried to do {{ $kategori->post()->count() }}
(this is works btw) but with Users table. I already set the foreign key and stuff.
I tried to modify the User.php
too (See code) but still didnt work as i intended. I need some helps. And im using the Laravel "php artisan make:auth
"
I've tried edited the Model (User.php
and Post.php
) and Controller but i still cant figure out whats wrong.
Post.php
public function user()
{
return $this->belongsTo('App\User', 'user_id');
}
User.php
public function post()
{
return $this->hasMany('App\Post','user_id');
}
Controller
use App\User;
// other methods in the Controller
public function index()
{
$posts = post::paginate(5);
$user = User::post;
return view('index', compact('posts', 'user'));
}
blade view
{{ $user->post()->count() }}