Laravel Relationship with grabbing data - laravel

I'm not really sure how to title this one, but I have been trying to follow the docs on Laravel about relationships and I'm not sure how to make this one in the model. I have a users model:
<?php
namespace App;
use Laravel\Passport\HasApiTokens;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'email', 'first_name', 'last_name', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token', 'is_admin'
];
/**
* The attributes that should be mutated to dates.
*
* #var array
*/
protected $dates = [
'created_at',
'updated_at',
'email_verified_at'
];
public function companies()
{
return $this->hasManyThrough('App\Company', 'App\CompanyUser');
}
}
Then I have a companies model which is like the following:
<?php
namespace App;
use App\Traits\UserRelationsTrait;
use Illuminate\Database\Eloquent\Model;
class Company extends Model
{
use UserRelationsTrait;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
];
/**
* The attributes that should be mutated to dates.
*
* #var array
*/
protected $dates = [
'created_at',
'updated_at'
];
public function users()
{
return $this->hasManyThrough('App\User', 'App\CompanyUser');
}
}
Next I have the intermediate table CompanyUsers
<?php
namespace App;
use App\Traits\UserRelationsTrait;
use Illuminate\Database\Eloquent\Model;
class CompanyUser extends Model
{
use UserRelationsTrait;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'company_id', 'role_id', 'user_id'
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
];
/**
* The attributes that should be mutated to dates.
*
* #var array
*/
protected $dates = [
'created_at',
'updated_at'
];
}
In my models I am able to do a user has many companies through the CompanyUser model but how do I make a relationship of a user has one role in a company through the CompanyUser table? So when I want the role the user has with the company I can use the model relation. This is the part of the relations I dont understand. Do I have to use the morphTo methods or am I able to specify a certain company?

You are trying to retrieve data from the pivot table
-- Btw, you don't need to create the model CompanyUser, you just need the table.
Take a look at Retrieving Intermediate Table Columns in the docs
$companies = $user->companies;
foreach( $companies as $company) {
echo $role_id = $company->pivot->role_id;
}

Related

Attempt to read property "name" on null (View: C:\xampp\htdocs\Moja_OTT\resources\views\premium\profile.blade.php)

I want a simple program that shows the user profile after login. I used session and middleware. But it is saying that the name property is null. Please help me solve this error. I have used User model. The value of the properties are not retrieving from the database.
Blade code:
<h1>Profile</h1>
<h2>Welcome Mr/Ms {{$user->name}}</h2>
Logout
Model:
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use App\Models\PremiumModel;
class User extends Authenticatable implements MustVerifyEmail
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $table = 'users';
protected $primaryKey = 'user_id';
protected $timestamp = false;
protected $fillable = [
'name',
'email',
'password',
'type',
'email_verified_at',
'pro_pic',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
function admin()
{
return $this->hasOne('App\Models\admin', 'user_id', 'admin_id');
}
function premium()
{
return $this->hasMany(PremiumModel::class, 'user_id', 'user_id');
}
}
Controller code:
function profile()
{
$user = User::where('user_id',session()->get('logged'))->first();
return view('premium.profile')->with('user',$user);
}
Here, logged is the middleware.
You can try hasOne in the User model:
public function premium()
{
return $this->hasOne(PremiumModel::class, 'user_id', 'user_id');
}
Because hasMany returned an array, but hasOne returned just a single Object

How to resolve mass assignment error in laravel?

I am new to the laravel, i am implementing user registration api while registering the user it's throwing an error like mass_assignment
Add [first_name] to fillable property to allow mass assignment on [App\\Entities\\User].
User.php
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use Notifiable;
protected $table ='user';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'first_name','last_name','phone_number','email'
];
protected $guarded =[];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
}
UserController.php
public function userRegister(userRequest $request){
$data = $this->repository->create($request->all());
return $user = new UserTransformer($data);
}
}
please help why this error is happening ..?
The main problem is laravel5.8 the model paths are different so use following facade
use App\modelname
instead of use App\Entities\User,let me know if this resolves your problem :-)

How can I change the filed email from Larval 8 auth user table to username?

currently, I'm trying to change the field email in user table to username. But seems like it did not work at all. What I did just replace the email (inside of $fillable array) to username.
2.Here is the code for User.php file.
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Tymon\JWTAuth\Contracts\JWTSubject;
class User extends Authenticatable implements JWTSubject
{
use HasFactory, Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name',
'username',
'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get the identifier that will be stored in the subject claim of the JWT.
*
* #return mixed
*/
public function getJWTIdentifier() {
return $this->getKey();
}
/**
* Return a key value array, containing any custom claims to be added to the JWT.
*
* #return array
*/
public function getJWTCustomClaims() {
return [];
}
public function username()
{
return 'username';
}
}
Here is the schema for my user table
You also have to update your user table migration. If you don't need the email field, you can replace the line :
$table->string('email')->unique();
with :
$table->string('username');
Then, run :
php artisan migrate:refresh
For this situtation you need to update your migration file for this, then you can use your username without e-mail.

Cannot sava data in database in notification file in Laravel

I'm using Laravel Notification and i want to save data in my database table if i write my insert query then i'm getting following error otherwise Notification are running :
TypeError: Return value of App\User::getLogNameToUse() must be of the
type string, null
This is the query
public function toMail($notifiable)
{
$users = User::find($notifiable->id);
$users->verification_link=$link;
$users->save();
...
...
}
Model:
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Spatie\Activitylog\Traits\LogsActivity;
class User extends Authenticatable
{
use Notifiable, LogsActivity;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'email', 'role_id', 'password',
];
/**
* The attributes that should be hidden for arrays.
*
* #var array
*/
protected $hidden = [
'password', 'remember_token',
];
/**
* The attributes that should be cast to native types.
*
* #var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
public function role()
{
return $this->belongsTo(Models\Role::class);
}
}
Seems like the error is with the logName and without looking at more code and data it is difficult to pinpoint exactly what is wrong.
Following could work
/**
* Get the log name to use for the model.
*
* #param string $eventName
* #return string
*/
public function getLogNameToUse(string $eventName = ''): string
{
return Str::kebab(Str::plural(class_basename($this)));
}
This is just overriding the piece of code causing issue. Give it a try.

Laravel relations and pivots for two models

There are some papers from Paper model and some editors and reviewers from User model that are distinguished by a role field. The editors determine which reviewers must review which papers that are stored in ReviewerPaper(user_id, paper_id) model and the comments of reviewers are stored in ReviewerComment(reviewer_id, paper_id, comment) model. Hence we have Paper, User, ReviewerPaper, ReviewerComment. Because a reviewer may have multiple comments on a paper, I didn't include the comment field in ReviewerPaper. I need to delete comments of a reviewer once it has been free from reviewing a paper (equivalently {paper_id},{reviewer_id} is removed) and I want to do it by Laravel relations not by looping through the model data.
My solution was to include a field via in ReviewerComment which is a foreign key to ReviewerPaper but it seems to be messy.
I would like to know is there any solution for this? Thanks in advance
Update
class User extends Authenticatable
{
use HasFactory, Notifiable, SoftDeletes, JqGrid;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'name', 'family', 'gender', 'birth_date', 'phone', 'country_id', 'university',
'role_id', 'email', 'password', 'email_verified_at'
];
}
class Paper extends Model
{
use HasFactory, Notifiable, SoftDeletes, JqGrid;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'title', 'file', 'user_id', 'status'
];
}
class ReviewerComment extends Model
{
use HasFactory, Notifiable, SoftDeletes, JqGrid;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'user_id', 'paper_id', 'comment', 'reviewer_paper_id'
];
}
class ReviewerPaper extends Model
{
use HasFactory, Notifiable, JqGrid;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'user_id', 'paper_id'
];
}
Never tried it (or seen it done) like this, but in your shoes, I'd try it:
$table->foreign(['reviewer_id','paper_id'])->references(['user_id','paper_id'])->on(['reviewer_paper'])->onDelete(['cascade']);
If it fails, you can also try this. :)
$table->foreign(['reviewer_id','paper_id'])->references(['user_id','paper_id'])->on(['reviewer_paper','reviewer_paper'])->onDelete(['cascade']);
And if this isn't supported in laravel, then you'll probably have to use the "via" field and/or loop manually.

Resources