How to get username from blog post? - laravel

I have tried to relationship between post and user so that show the post having the username. But the error has occurred. It has showed the message : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause' (SQL: select * from users where users.post_id in (2))
//User Model
public function posts()
{
return $this->hasMany('App\Post');
}
//Post Model
public function user()
{
return $this->hasOne('App\User', );
}
// Now the post controller
public function show($id)
{
$post = Post::with('user')->find($id);
return view('posts.show')->with('post',$post);
}

replace with the below code, it's working fine.
User Model
public function posts()
{
return $this->hasMany('App\Post');
}
Post model
public function user()
{
return $this->belongsTo('App\User');
}

User has many Post so it would be hasMany relationship.
Post belongs to (depends on) User, so it would be belongsTo.
User Model :
public function posts()
{
return $this->hasMany('App\Post');
}
Post Model :
public function user()
{
return $this->belongsTo('App\User');
}

Related

Laravel get all user who comment on post via post

Ive been trying to get all user who commented on post
$post->comments()->user() //does not work
$post->comments()->load('user', function($q) { $q->select('email'); }));//does not work
$post->comments()->with('user')->get() //worked but not getting the user as result (still need to loop)
but im getting a BadMethodCallException
this are the models
//Comment Model
public function user(){
return $this->belongsTo(User::class);
}
public function post(){
return $this->belongsTo(Post::class);
}
//User Model
public function comments(){
return $this->hasMany(Comment::class);
}
public function posts(){
return $this->hasMany(Post::class);
}
//Post Model
public function user(){
return $this->belongsTo(User::class);
}
public function comments(){
return $this->hasMany(Comment::class);
}
you can try getting all comments belongs to that post with their users:
$users= Comment::where('post_id', $post->id)->with('user')->pluck('user');
I found an answer (i think this is a duplicate question, but anyway)
Laravel get all posts that a user commented on
Instead i use this
$post->whereHas('comments', function($query) use($user) {
$query->where('id',$user->id);
})->get();

laravel one to many relationship return no error

I try to create a relationship between user and post. 1 user have many posts and 1 post has 1 user. I already write a code relationship between model User.php and Post.php and I want to store user data in database. my question is how to save data in database? btw I use postgresql. when I click button there is no error and cannot redirect to admin.post.index.
PostController.php
public function store(Request $request)
{
$user = auth()->user();
$post = $user->posts()->create($request->only('title','body'));
return redirect()->route('admin.post.index');
}
Post.php
public function user()
{
return $this->belongsTo('App\User');
}
User.php
public function posts()
{
return $this->hasmany('App\Post');
}
Change User.php
public function posts()
{
return $this->hasMany('App\Post');
}
you spelled incorrect
You can try to Change User.php
public function posts()
{
return $this->hasMany('App\Post','user_id');
}

simple One to many relationship laravel

i have a user(ADMIN) and a student i want them to be in one table that is on the dashboard controller i cant find and exact answer in the searching please help me
Student model
public function user()
{
return $this->belongsTo("App\User", 'User_id', 'id');
}
User model
public function student()
{
return $this->hasMany("App\Student");
}
DashboardController
$users = User::with('user_id');
return view('superadminpage.admin_table')->with('users', $users);
Edit Student model code like this:
public function user(){
return $this->belongsTo("App\User");
}
Also edit User model:
public function students(){
return $this->hasMany("App\Student");
}
then use in DashboardController
$users = User::query()->with("students")->all();
return view('superadminpage.admin_table')->with('users', $users );
then you can do this:
foreach($users as $user){
$students = $user->students;
// use
}
first change the User_id to user_id at student model
public function user()
{
return $this->belongsTo("App\User", 'user_id');
}
second change at DashboardController
$users = User::with('user_id'); to $users = User::with('student')->all();
first change the User_id to user_id at student model
public function user()
{
return $this->belongsTo("App\User", 'user_id' , 'id');
}
second change at DashboardController
$users = User::with('user_id'); to $users = User::with('student')->all();
it's also good to change at user model public function student() to public function students() and call at DashboardController $users = User::with('students')->all();
public function users()
{
return $this->hasMany('App\User');
}
public function role()
{
return $this->belongsTo('App\Role');
}

Updated: Why relationship works when user is Admin, but won`t when Regular user?

I have 3 models: User, Role and Currency. I am confused about relationship between them.
Then User is Admin this works:
Auth::user()->currency->symbol)
When regular User I get error:
Trying to get property of non-object
if dd(Auth::user()) it show user, but cant access to relationship with model Currency. Why it is so?
User model relationship:
public function currency()
{
return $this->belongsTo(Currency::class, 'currency_id');
}
Currency model relationship:
public function created_by()
{
return $this->belongsTo(User::class, 'created_by_id');
}
If you need extra information, let me know.
User model
public function currency()
{
return $this->belongsTo(Currency::class);
}
Currency model
public function users()
{
return $this->hasMany(User::class, 'created_by_id');
}
Going with an assumption that your User model has an attribute currency_id
$this->belongsTo(TABLE, FORIEGN KEY, TABLE KEY);
refer this
User model
public function currency()
{
return $this->belongsTo(Currency::class, 'currency_id', 'id');
}
Currency model
public function users()
{
return $this->hasMany(User::class, 'created_by_id', 'id');
}

Need help laravel 4.2

I want to get the username in table user, of post ID in post table and return View::make('post.index'). I need to view in post.index blade.
Database
users table
id
username
post table
id
message
posts_id
Post.php #models folder
public function posts()
{
return $this->belongsTo('User');
}
User.php #models folder
public function post()
{
return $this->hasMany('Post');
}
PostController.php #controller folder
public function index()
{
$posts = Post::with('username')->get();
return View::make('post.index')->with('posts', $posts);
}
post/index.blade.php
the result here
i need to the result of author id become the username
for the best practices:
your model class names should be User and Post. database table names should be users and posts.
in Post.php file ,
public function user()
{
return $this->belongsTo('User');
}
in User.php file,
public function posts()
{
return $this->hasMany('Post');
}
in your PostController.php
public function index()
{
$user_of_the_post = Post::find(postId)->user; //replace the postId //this will return the user object of the specified post
$user_name_of_the_author = $user_of_the_post->username;
**pass the necessary data to the view and print and return the view**
}

Resources