one-to-many relationship doesn't work - php

I am trying to create one-to-many relationship with 2 models, User and Role. So, I would like that one User can only have one role, and the Role can be assigned to more than User. I tried to follow the offical tutorial on https://laravel.com/docs/5.1/eloquent-relationships#one-to-many, and ended up with this:
class User extends Model
{
public function role()
{
return $this->belongsToMany('App\Admin\Role');
}
}
class Role extends Model
{
protected $table = 'roles';
public function users()
{
return $this->hasMany('App\Admin\User');
}
}
Based on that link, I think the Role is the same thing as the Post, one Role can be assigned to many User. However, this doesn't quite work, here is how I tried to access the role name for a specific User
$role_first = $user->role; // Eloquent\Collection
$role_second = $user->role(); // Eloquent\Relations\BelongsToMany
$role_first->role_title // 'Undefined property: Illuminate\Database\Eloquent\Collection::$role_title'
$role_second->role_title // exception 'ErrorException' with message 'Undefined property: Illuminate\Database\Eloquent\Relations\BelongsToMany::$role_title'
What is exactly wrong here?

In the User class
public function role()
{
// not $this->belongsToMany('App\Admin\Role');
return $this->belongsTo('App\Admin\Role');
}
Because you want a oneToMany not manyToMany relationship.

This should do it. Please give it a try
class User extends Model
{
public function role()
{
return $this->hasOne(App\Admin\Role::class);
}
}
// user belongs to one role
class Role extends Model
{
protected $table = 'roles';
public function users()
{
return $this->belongsTo(App\Admin\User::class);
}
}

User Model
class User extends Authenticatable{
public function roles(){
return $this->belongsTo('App\Admin\Role');
}
}
Role Model
class Role extends Model{
public function users(){
return $this->hasMany('App\Admin\User');
}
}
The user can have only one role, and the role can be assigned to many users.

Just make sure the relation
A User hasOne Role
A Role belongsToMany Users
so, in this condition
class User extends Model
{
public function role()
{
return $this->hasOne('App\Admin\Role');
}
}
class Role extends Model
{
protected $table = 'roles';
public function users()
{
return $this->belongsToMany('App\Admin\User');
}
}
and must check your user_id foreign constraint be 'unsigned' in your role migration table.

Related

why laravel 6 many-to-many relationship does not working?

Hi I simply want to get permissions of the role, I am trying following
$r = Role::find(1);
dd($r->permissions);
The above script does not return any permission however you can see there is data in the below tables. I also tried following but no luck
$role = Role::with('permissions')->where('id', 1)->first();
I have data in the table as you can see
Table:tes_permissions
Table: tes_roles
Table: tes_permission_role
And following are Models
class Permission extends Model
{
protected $table = 'tes_permissions'
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
And
class Role extends Model
{
protected $table = 'tes_roles';
public function permissions() {
return $this->belongsToMany('App\Permission', 'tes_permission_role', 'permission_id', 'role_id');
}
}
Can someone kindly guide me what can be the issue, I would appreciate.
You mixed the order of properties in the belongsToMany(). The third argument is specifying the ID for the model defining the relationship. So change to the following:
public function permissions() {
return $this->belongsToMany('App\Permission', 'tes_permission_role', 'role_id', 'permission_id');
}
And on the Permission model, also define it to be sure.
public function roles()
{
return $this->belongsToMany('App\Role', 'tes_permission_role', 'permission_id', 'role_id');
}

Problem with Laravel Eloquent - relation not working

I'am beginner in Laravel. I have project in Laravel 5.8.
I have User model:
class User extends Authenticatable implements MustVerifyEmail
{
use Notifiable;
use psCMS\Presenters\UserPresenter;
use scopeActiveTrait;
public static $roles = [];
public $dates = ['last_activity'];
// ...
public function scopeHistory()
{
return $this->hasMany('App\UserLoginHistory');
}
// ...
}
and UserLoginHistory:
class UserLoginHistory extends Model
{
protected $quarded = ['id'];
public $timestamps = false;
protected $fillable = ['user_id', 'date_time', 'ip'];
public function user()
{
return $this->belongsTo('App\User');
}
}
I want show user login history by this code:
User::history()->where('id', $idAdmin)->orderBy('id', 'desc')->paginate(25);
but it's not working.
This function not working - I haven't got results.
How can I fixed it?
First of all, you are defining your relationship as a scope (prefixing the relationship with the scope keyword). Try updating your model relationship to this:
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
Then, given your query, it seems that you want to get all the UserLoginHistory
records for a given User. You could accomplish this in two ways (at least).
From the UserLoginHistory model itself, constraining the query by the foreign key value:
$userId = auth()->id(); // get the user ID here.
$results = UserLoginHistory::where('user_id', $userId)->paginate(15);
// ^^^^^^^ your FK column name
From the User model using your defined relationship:
$userId = auth()->id(); // get the user ID here.
$results = User::find($userId)->history;
The downside of the second approach is that you'll need to paginate the results manually.
in your User model you should define your relation by this way :
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
then if you would like to select with history model you can do that with WhereHas() method :
User::whereHas(['history'=>function($q) use ($idAdmin) {
$q->where('id',$idAdmin)
}])->orderBy('id', 'desc')->paginate(25);
You must be do this changes
public function history()
{
return $this->hasMany('App\UserLoginHistory');
}
usage
$user = User::find($idAdmin);
$userHistories = $user->history()->latest()->paginate(25);
or get user with all history
User::with('history')->find($idAdmin);
// Post model
namespace App;
use App\User;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
public function categories()
{
return $this->belongsToMany('App\Category')->withTimestamps();
}
}
// Category model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
public function posts()
{
return $this->belongsToMany('App\Post')->withTimestamps();
}
}

Laravel CRUD ORM Can't find model

I have been trying to associate a User with a Profile.
Here is my code:
Profile Model:
class Profile extends Model
{
public function user(){
return $this->belongsTo('App\User');
}
}
User Model:
class User extends Authenticatable
{
...
public function profile(){
return $this->hasOne('App/Profile');
}
}
ProfileController:
public function show($username)
{
$user = User::where('username',$username)->first();
$profile = $user->profile;
return view('profiles.show')->withProfile($profile);
}
Route (It's meant to be "profil"):
Route::resource('profil','ProfileController');
Error:
http://prntscr.com/ff6y0a
Any tips on doing the rest of the CRUD functionality is very welcome!
Change App/Profile to App\Profile in your User model relationship.

No query results for model [App\Subreddit] when adding moderators to a forum category

I am trying to assign users as moderators to a forum category. At the moment, I am only trying to display route where a user can add moderators subreddit/{id}/moderators and display the subreddit name.
For that, I am getting No query results for model [App\Subreddit]
I must've messed up the relations between the tables somewhere.
My tables
#users: id, name, email...
#subreddits: id, name, user_id...
#moderators: id, user_id, subreddit_id
Routes.php
Route::get('subreddit/{id}/moderators', [
'as' => 'moderators',
'uses' => 'ModeratorsController#create'
]);
Moderator.php Model
class Moderator extends Model
{
protected $table = 'moderators';
protected $fillable = ['user_id', 'subreddit_id'];
public function subreddit() {
return $this->belongsToMany('App\Subreddit');
}
public function user() {
return $this->belongsTo('App\User');
}
}
In User Model, I have a hasManyThrough
public function moderators() {
return $this->hasManyThrough('App\Moderator', 'App\Subreddit');
}
In Subreddit Model
public function moderators() {
return $this->hasMany('App\Moderator');
}
And then in ModeratorsController I have the following create() method
public function create(Moderator $moderator, Subreddit $subreddit, User $user)
{
$subreddit = Subreddit::with('user')->findOrFail($subreddit->id);
return view('user/moderators')->with('subreddit', $subreddit)->with('user', $user)->with('moderator', $moderator);
}
If I change findOrFail to firstOrFail it will get me the first subreddit in the database, but I don't want the first, I want the exact one I'm trying to add moderators to.
class User extends Model{
public function canModerate(){ //name this as you wish
return $this->belongsToMany('App\Subreddit','moderators');
}
public function subreddits(){
return $this->hasMany('App\Subreddit');
}
}
class Subreddit extends Model{
public function moderators(){ // name this as you wish
return $this->belongsToMany('App\User','moderators');
}
public function creator(){
return $this->belongsTo('App\User');
}
}
remove id from moderators table and you wont need the Moderator class.
Additional documentation can be found here.
Edit RouteServiceProvider::boot method and add the following line:
$router->model('subreddit', 'App\Subreddit');
Documentation can be found here.
Your route should then look like:
Route::resource('subreddit.moderator','ModeratorsController');
The URL should look like http://localhost/subreddit/{subreddit}/moderator/create
And finally the controller method should be:
public function create(Subreddit $subreddit)
{
$user = $subreddit->creator;
$moderators = $subreddit->moderators()->get();
return view('user/moderators')->with(compact('subreddit','user','moderators'));
}

How can I make a row of relations in laravel?

I am struggling with this for a while now, but I can't figure it out how it works.
In laravel I have a few models with relationships. I wan't to have al the accounts based on the logged in user and the passed parameter for the workspace.
This is how the models looks like: (I only coppied the methods to keep it short)
The user Model:
class User extends Eloquent implements UserInterface, RemindableInterface {
public function workspaces()
{
return $this->hasMany('Workspace', 'user_id');
}
public function account()
{
return $this->hasManyThrough('account', 'Workspace', 'id', 'workspace_id');
}
}
The workspace model:
class Workspace extends Eloquent implements UserInterface, RemindableInterface {
public function account()
{
return $this->hasMany('account', 'workspace_id', 'id');
}
public function user()
{
return $this->belongsTo('User', 'user_id', 'id');
}
}
The account model
class account extends Eloquent implements UserInterface, RemindableInterface {
public function account_url()
{
return $this->hasOne('acountUrl', 'id', 'account_url_id');
}
public function workspace()
{
return $this->belongsTo('Workspace', 'workspace_id', 'id');
}
}
The account_url model
class account_url extends \Eloquent implements UserInterface, RemindableInterface {
public function account()
{
return $this->belongsToMany('account', 'id', 'account_url_id');
}
}
So I want from the logged-in user with a specific workspace all the account with the account_urls
something like this: user->workspace->account->account_url
I tried the following things but it don't work:
$account_urls = user::find( Auth::user()->id)->first()->workspaces()->where('id', '=', 1)->account()->account_url()->select('url')->get();
and:
$account_urls = account::where('workspace_id', '=', '1')->account_url()->select('url')->get();
Only when I do it like this:
$account_urls = account::find(1)->account_url()->select('url')->get();
But then I get only 1 url, but when I replase find(1) for all() I get an error?
Is there someone who can help me with this?
Tanks,
Your relations are wrong, change them to:
// User
public function account()
{
return $this->hasManyThrough('Account', 'Workspace', 'user_id', 'workspace_id');
}
// Account
// use camelCase for relations
public function accountUrl()
{
// I assume you have account_url_id on accounts table
// If it's opposite, then use hasOne
return $this->belongsTo('AcountUrl', 'account_url_id', 'id');
}
// AccountUrl (use camelCase)
public function account()
{
// if above is hasOne, then here belongsTo instead.
return $this->hasOne('account', 'account_url_id', 'id');
}
Now, fetching models:
// this part is .. amazing ;)
user::find( Auth::user()->id )->first();
// it does this:
Auth::user()->id // fetch user and get his id
user::find( .. ) // fetch user with given id, you have this user already above...
->first() // fetch first row from users table (not the one with id provided before)
so you want:
$account_urls = Auth::user()->workspaces()
->where('id', '=', 1)->first() // first fetches the result
// or simply
// ->find(1)
->accounts()->first()->accountUrl()
->pluck('url'); // this does 'SELECT url' and returns only this field instead of model
Just remember that:
$user->workspaces
$workspace->accounts
these are collections, so you can't call anything of the model on them, you need to get single model first.

Categories