Trying to get property 'designation_name' of non-object in Laravel 8 - laravel

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

Related

How to get multiple relationships Data in My Laravel Vue Project

I have a 3 model "User","Review","ReviewReplay"
in user Model
public function reviews()
{
return $this->hasMany('App\Models\Review');
}
public function reviewReplys()
{
return $this->hasMany('App\Models\ReviewReply');
}
In Review Model
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function reviewReplys()
{
return $this->hasMany('App\Models\ReviewReply');
}
in Review Replay Model
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function review()
{
return $this->belongsTo('App\Models\Reviews');
}
in My Controller
public function getReview($id)
{
$review= Review::where('product_id',$id)->with('user','reviewReplys')->paginate(10);
return response()->json($review);
}
Now here I run Review foreach loop and here I need review.reviewReplay.user information. How I Get The Review Replay User information inside Review Foreach loop.
You can do it as below.
$review= Review::where('product_id',$id)->with(['reviewReplys.user'])->paginate(10);
with(['reviewReplys.user']) --> it will make connection between Review model to reviewReplys model +
reviewReplys model to User model

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');
}

Laravel Retrieve related model through pivot table

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');
}

Many to Many to One relationship

I've got four tables Users[user_id] - role_user[user_id,role_id] - Roles[role_id] - Permissions[role_id]. A User could have many Roles, while the Role has many Permissions. So, a Permission has one Role, while a Role belongs to many Users.
// User.php ...
class User extends Model
{
public function roles()
{
return $this->belongsToMany('Role');
}
}
// Roles.php
class Role extends Model
{
public function users()
{
return $this->belongsToMany('App\User');
}
public function permissions()
{
return $this->hasMany('Permission');
}
}
// Permission.php
class Permission extends Model
{
public function role()
{
return $this->belongsTo('Role');
}
}
I guess the real question is; can you chain relationship methods, like: App\User::find(1)->roles->permissions; I don't think you can because the ->roles returns a Collection and not an eloquent model, so the permissions method doesn't exists off roles.
Is there another way I can get the collection of permissions for all roles for a given use, preferably with a single line?
I haven't tested it, but I think this will work or work with very small twick. Add this function in your User Model.
public function getPermission($id){
$roles = Roles::where('user_id','=', id)->get();
$permissions = array();
foreach($roles as $role){
array_push($permissions, $role->permissions);
}
return $permissions;
}
and access as $user->getPermission($user->id);. This might not be the best solution, but it should solve the problem.
UPDATED CODE
You can use accessor like the example bellow and this will return a permission collection. Use this function in your User Model
public function getPermissions($value){
$role_ids = Roles::where('user_id','=', $value)
->select(array('permission_id'))
->toArray();
return Permission::find($role_ids);
}
and access it like $permissions = App\User::find(1)->permissions;. I believe this will work as you expected.

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