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**
}
Related
I want to show the user role from my user role table, but I can't.
User.php
public function role()
{
return $this->belongsTo('App\Models\Designation');
}
UserController
public function index()
{
$user = User::all();
return view('dashboard2', compact('user'));
}
View
<h1>{{ Auth::user()->role->designation_name}}</h1>
You actually should have shared more details about the relationships. But I will give an answer based on an assumption. It seems like there is a one-to-one relationship betwen User and Role as well as Role and Designation. So you want to reach designation from user. Based on that:
//User.php
public function role()
{
return $this->hasOne(Role::class);
}
//Role.php
public function user()
{
return $this->belongsTo(User::class);
}
public function designation()
{
return $this->hasOne(Designation::class);
}
//Designation.php
public function role()
{
return $this->belongsTo(Role::class);
}
// Controller
public function index()
{
// If you need only the auth user's data, you don't
// need adding relationships into the query.
$users = User::with('role.desgination')->all();
return view('dashboard2', compact('users'));
}
// View
// For auth user:
// I haven't used this way before,
// but technically it should work.
auth()->role()->designation()->name;
// For users collection:
// Make sure you added the query to the controller.
#foreach($users as $user)
$user->role->desgination->name
#endforeach
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');
}
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');
}
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');
}
So i'm a bit confused with this situation, I have the current relations:
User hasmany Favourite
favourites references to a Recipe model
favourites is a pivot table but when I retrieve the favourites for a given user like:
$user->favourites
i get the favourite with the pivot data, but I want to get the fields of the recipe
Recipe.php:
public function favourites()
{
return $this->hasMany('App\Favourite');
}
User.php:
public function favourites()
{
return $this->hasMany('App\Favourite');
}
Favourite.php:
public function user()
{
return $this->belongsTo('App\User','user_id');
}
public function recipes()
{
return $this->belongsTo('App\Recipe','recipe_id');
}
EDIT : there was no problem at all, just needed to call:
#foreach($user->favourites as $favourite)
{{ $favourite->recipe->name }}
#endforeach
You need to set your relation to Has Many Through instead of just has Many.
https://laravel.com/docs/5.2/eloquent-relationships#has-many-through
Recipe.php:
public function favourites()
{
return $this->hasManyThrough('App\Favourite', 'App\User');
}
User.php:
public function favourites()
{
return $this->hasManyThrough('App\Favourite', 'App\Receipe');
}
Favourite.php:
public function user()
{
return $this->hasManyThrough('App\User','App\Receipe');
}