Saving Relationships in Laravel on the go - laravel

I have a user model that is related to a enduser model with a hasOne relationship.
I want to save the inputs of a form field in both user and enduser model in one go.
here is my code;
$user = new User();
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->phone_number = $request->phone_number;
$user->email = $request->email;
$user->password = $request->password;
$user->enduser->username = $request->username;
$user->save();
return user;
my relationships are
Class User extends Authenticable
{
public function enduser()
{
return $this->hasOne('App\Enduser');
}
}
my migration for users is thus
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('phone_number');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->enum('roles',['admin', 'enduser', 'organization', 'radio', 'employee'])->nullable();
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
and for enduser is thus
public function up()
{
Schema::create('endusers', function (Blueprint $table) {
$table->increments('id');
$table->text('address')->nullable();
$table->string('lga')->nullable();
$table->string('city')->nullable();
$table->string('state')->nullable();
$table->string('username');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('endusers');
}
i get an error that says 'unknown column 'username' in where clause'. How can i save such relationship?

Your user model should look like this.
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 $fillable = [
'first_name','last_name','phone', 'email', 'password',
];
public function enduser() {
return $this->hasOne('App\EndUser');
}
}
and your EndUser model should look like this
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class EndUser extends Model
{
use Notifiable;
protected $fillable = [
'username',
];
public function user() {
return $this->belongsTo('App\User');
}
}
Then you can write this code in your controller
//first becrypt your password
$request->merge('password',bcrypt($request->password));
$user = User::create(
$request->only(['first_name','last_name','phone','email','password'])
);
$user->enduser->create([$request->username]);
return $user;

Therefore, in order to accomplish what you're trying to do, you can do first create the user then associate the enduser with it by doing the following:
$user = new User();
$user->first_name = $request->first_name;
$user->last_name = $request->last_name;
$user->phone_number = $request->phone_number;
$user->email = $request->email;
$user->password = $request->password;
$user->save();
$endUser = new EndUser();
$endUser->username = $request->username;
$user->enduser()->save($endUser);
return $user;
However, prior to doing this, you must ensure your User and enduser models have their relationships set up correctly.

Maybe you can solve it using laravel observers:
php artisan make:observer UserObserver --model User
This command will create a UserObserver.php file inside app/Observers folder.
Add this to the created function:
public function created(User $user)
{
EndUser::create([
'username' => $user->username,
'user_id' => $user->id,
]);
}
You also need to edit the AppServiceProvider file, inside the app/Providers folder. Add this to the boot method:
public function boot()
{
User::observe(UserObserver::class);
}
Now, every time that a User get registered, it will create a EndUser with the user data.
Hope it helps.

Related

Laravel - I want to get all the messages between the auth user and the receiver

I have a users table, a message table and a user-message table.
i want to get all the messages between the authenticated user and the receiver.
here are the tables and the models:
the users table and model are obvious.
Message model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Message extends Model
{
protected $fillable = [
'parent_id', 'message', 'type','status',
];
public function user_messages(){
return $this->hasMany('App\UserMessage');
}
public function users(){
return $this->belongsToMany('App\User','user_messages','message_id','sender_id')->withTimestamps();
}
}
User_message model :
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class UserMessage extends Model
{
protected $fillable = [
'message_id', 'sender_id', 'receiver_id','type','seen_status','deliver_status',
];
public function message(){
return $this->belongsTo('App\Message');
}
}
Message table :
* #return void
*/
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->integer('parent_id')->nullable();
$table->longText('message')->nullable();
$table->integer('type')->default(1)->comment('1 : message , 2 : file');
$table->integer('status')->default(1);
$table->timestamps();
});
}
User_message table :
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('user_messages', function (Blueprint $table) {
$table->id();
$table->integer('message_id');
$table->integer('sender_id');
$table->integer('receiver_id');
$table->integer('type')->default(0)->comment('1 : groupe message , 0 : personal message');
$table->integer('seen_status')->default(0)->comment('1 : seen');
$table->integer('deliver_status')->default(0)->comment('1 : delivered');
$table->timestamps();
});
}
so all i want to know is how the logic inside the controller function will be.
I found the solution :
public function getMessages($id, $ids){
$final_messages_list = array();
$auth_id = $id;
$friend_id = $ids;
$messages = UserMessage::where('sender_id',$auth_id)->where('receiver_id',$friend_id)->orderby('created_at','desc')->get();
foreach($messages as $message){
$final_messages_list[] = $message->message;
}
return response()->json([
'status' => 200,
'messages' => $final_messages_list,
]);
}

Database relations in Laravel Module

I've a problem in using database relationships together with Laravel module.
for example:
I've User module & Profile Module
I've One-to-One relation between user & profile (in 2 separated modules)
I need to get profile information when I have user data (example: $user->profile)
But I think because of creating relation between two modules, I cannot access to this relation.
My received error:
"message": "SQLSTATE[42S02]: Base table or view not found: 1146 Table 'user.profiles' doesn't exist (SQL: select * from profiles where profiles.user_id = 2 and profiles.user_id is not null limit 1)",
"exception": "Illuminate\Database\QueryException",
User Module/Entities/User.php
<?php
namespace Modules\User\Entities;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Modules\VofourCourse\Entities\Profile;
class User extends Authenticatable
{
use HasFactory;
protected $connection = 'user';
protected $guarded = [];
protected $hidden = [
'password',
'remember_token',
];
protected $casts = [
'email_verified_at' => 'datetime',
];
public function profile()
{
return $this->hasOne(Profile::class);
}
}
Profile Module/Entities/Profile.php
<?php
namespace Modules\Profile\Entities;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Modules\User\Entities\User;
use RichanFongdasen\EloquentBlameable\BlameableTrait;
class Profile extends Model
{
use HasFactory, SoftDeletes, BlameableTrait;
protected $guarded = [];
public function user()
{
return $this->belongsTo(User::class);
}
}
Profile Module/Controller/ProfileController.php
<?php
namespace Modules\Profiole\Http\Controllers;
use Illuminate\Routing\Controller;
class ProfileController extends Controller
{
public function showProfile()
{
$user = auth('api')->user();
return $user->profile;
}
}
Profile migration
Schema::create('profiles', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->onDelete('cascade');
$table->string('whatsapp_mobile', 15)->unique();
$table->string('phone', 15)->unique();
$table->string('national_code', 10)->nullable();
$table->string('postal_code', 10)->nullable();
$table->text('address')->nullable();
$table->string('job_title')->nullable();
$table->foreignId('city_id')->nullable();
$table->string('father_name')->nullable();
$table->enum('marital_status', ['married', 'single', 'widow', 'divorced', 'unknown'])->default('married');
});
User migration
Schema::connection(config('modules.connections.user_database'))->create('users', function (Blueprint $table) {
$table->id();
$table->string('firstname')->nullable();
$table->string('lastname')->nullable();
$table->string('email')->unique()->nullable();
$table->string('vofour_id' , 10)->nullable();
$table->string('username', 20)->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password')->nullable();
$table->date('birth_date')->nullable();
$table->enum('gender', ['male', 'female'])->nullable();
$table->string('mobile', 15)->unique();
$table->enum('status', ['active', 'inactive'])->default('active');
$table->rememberToken();
});```
thanks a lot for your helps & guidance
You've a typing error in your relationship. The error states there should be a relation profiles where as from your code there's a profile.
public function profile()
{
return $this->hasOne(Profile::class);
}
What you can do is return user->with('profile') which will return the user with it's relational details. I.e.
<?php
namespace Modules\Profiole\Http\Controllers;
use Illuminate\Routing\Controller;
class ProfileController extends Controller
{
public function showProfile()
{
$user = auth('api')->user();
return $user->with('profile');
}
}
Also update your profiles migration line where user_id is written from:
$table->foreignId('user_id')->onDelete('cascade'); to $table->foreignId('user_id')->constrained('users');.
This will link up the user_id as a foreign key to users table id. and return the correct collection of users with their profiles.

Insert data from multistep form with Laravel Relationships

I have a question for you.
I have a multisteps form:
Loan (this is sent from the calculator)
Create an account (email + password)
Personal data (first name, last name, phone number etc ...)
Addresses (street, city, state + if have correspondence address etc ...)
Employment (name of employer, address of employer, etc ...)
Finish (Review your data before sending ...)
the total number of inputs that are in the form is 60
I would like to split it into more tables
users
loans
personal
addresses
eployment
While I tried this, but something tells me that this method is not very safe even though it is working.
Therefore, I am turning here for advice and help as you would have done something like this?
Model User.php
class User extends Authenticatable
{
use Notifiable;
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = [
'email', '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 loans() {
return $this->belongsToMany(Loan::class)->withTimestamps();
}
public function personal() {
return $this->belongsTo(Personal::class);
}
public function adress() {
return $this->belongsTo(Adress::class);
}
public function employment() {
return $this->belongsTo(Eployment::class);
}
}
Model Loan.php
class Loan extends Model
{
protected $hidden = ['amount', 'month', 'payment'];
public function users() {
return $this->belongsToMany(User::class)->withTimestamps();
}
}
Model Personal.php
class Personal extends Model
{
protected $fillable = [
'first_name', 'last_name', 'identification_number', 'identity_card_number', 'date_of_birth', 'phone'
];
public function users() {
return $this->hasMany(User::class);
}
}
Model Adress.php
protected $fillable = [
'adress', 'adress_number', 'city', 'postcode', 'country', 'correspond_adress', 'correspond_adress_number', 'correspond_city', 'correspond_postcode', 'correspond_country', 'type_of_housing', 'since_year', 'marital_status', 'number_of_kids'
];
public function users() {
return $this->hasMany(User::class);
}
Model Employment.php
class Eployment extends Model
{
protected $fillable = [
'type_of_occupation', 'client_ico', 'client_dic', 'employer_name', 'employer_ico', 'employment_adress', 'employment_city', 'month_of_arrival', 'year_of_arrival', 'net_monthly_income', 'other_income', 'payment_method', 'expenditure_payments', 'loan_repayments', 'wage_deductions', 'other_expenditure', 'have_bank_account', 'iban_account'
];
public function users() {
return $this->hasMany(User::class);
}
}
DB: (users, loans, personal, adresses, eployment
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Schema::create('loans', function (Blueprint $table) {
$table->id();
$table->string('amount');
$table->string('month');
$table->string('payment');
$table->timestamps();
});
Schema::create('loan_user', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('loan_id');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('loan_id')->references('id')->on('loans')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
Schema::create('adresses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('adress');
$table->string('adress_number');
$table->string('city');
$table->string('postcode');
$table->string('country');
$table->string('correspond_adress')->nullable();
$table->string('correspond_adress_number')->nullable();
$table->string('correspond_city')->nullable();
$table->string('correspond_postcode')->nullable();
$table->string('correspond_country')->nullable();
$table->string('type_of_housing');
$table->string('since_year');
$table->string('marital_status');
$table->string('number_of_kids');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
Schema::create('eployments', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('type_of_occupation');
$table->string('client_ico')->nullable();
$table->string('client_dic')->nullable();
$table->string('employer_name')->nullable();
$table->string('employer_ico')->nullable();
$table->string('employment_adress')->nullable();
$table->string('employment_city')->nullable();
$table->string('month_of_arrival')->nullable();
$table->string('year_of_arrival')->nullable();
$table->string('net_monthly_income');
$table->string('other_income')->nullable();
$table->string('payment_method');
$table->string('expenditure_payments');
$table->string('loan_repayments')->nullable();
$table->string('wage_deductions')->nullable();
$table->string('other_expenditure')->nullable();
$table->string('have_bank_account');
$table->string('iban_account');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
LoanController.php
public function store(Loan $loan, User $user, Personal $personal, Adress $adress, Eployment $eployment, Request $request)
{
$user = User::create([
'email' => $request->email,
'password' => Hash::make($request->password),
]);
$data = new Loan;
$data->amount = $request->amount;
$data->month = $request->month;
$data->payment = $request->payment;
$personal = new Personal;
$personal->user_id = $user->id;
$personal->first_name = $request->first_name;
$personal->last_name = $request->last_name;
$personal->identification_number = $request->identification_number;
$personal->identity_card_number = $request->identity_card_number;
$personal->date_of_birth = $request->date_of_birth;
$personal->phone = $request->phone;
$adress = new Adress;
$adress->user_id = $user->id;
$adress->adress = $request->adress;
$adress->adress_number = $request->adress_number;
$adress->city = $request->city;
$adress->postcode = $request->postcode;
$adress->country = $request->country;
$adress->correspond_adress = $request->correspond_adress;
$adress->correspond_adress_number = $request->correspond_adress_number;
$adress->correspond_city = $request->correspond_city;
$adress->correspond_postcode = $request->correspond_postcode;
$adress->correspond_country = $request->correspond_country;
$adress->type_of_housing = $request->type_of_housing;
$adress->since_year = $request->since_year;
$adress->marital_status = $request->marital_status;
$adress->number_of_kids = $request->number_of_kids;
$eployment = new Eployment;
$eployment->user_id = $user->id;
$eployment->type_of_occupation = $request->type_of_occupation;
$eployment->client_ico = $request->client_ico;
$eployment->client_dic = $request->client_dic;
$eployment->employer_name = $request->employer_name;
$eployment->employer_ico = $request->employer_ico;
$eployment->employment_adress = $request->employment_adress;
$eployment->employment_city = $request->employment_city;
$eployment->month_of_arrival = $request->month_of_arrival;
$eployment->year_of_arrival = $request->year_of_arrival;
$eployment->net_monthly_income = $request->net_monthly_income;
$eployment->other_income = $request->other_income;
$eployment->payment_method = $request->payment_method;
$eployment->expenditure_payments = $request->expenditure_payments;
$eployment->loan_repayments = $request->loan_repayments;
$eployment->wage_deductions = $request->wage_deductions;
$eployment->other_expenditure = $request->other_expenditure;
$eployment->have_bank_account = $request->have_bank_account;
$eployment->iban_account = $request->iban_account;
$data->save();
$user->personal()->associate($user);
$personal->save();
$user->adress()->associate($user);
$adress->save();
$user->eployment()->associate($user);
$eployment->save();
$user->loans()->attach($data);
return redirect('/');
}
I don't know if I understood Laravel Relationships correctly but I try to ...
Excuse my English I'm Slovak and I helped with Google Translator
Well if you want to do everything with one request (building large form and manipulating it with JS) your store method is quite good.
You have to remove Loan $loan, User $user, Personal $personal, Adress $adress, Eployment $eployment, from your store method because your are not using model route binding for this: https://laravel.com/docs/5.8/routing#route-model-binding.
Also you don't need to associate ($user->personal()->associate($user);) or attach everything because you already adding user_id to every model you create.
Also don't forget form validation.

Laravel : foreign key -> doesn't have a default value

Hi ! I read other questions but I don't find my answer.
I want to create data but give me this error:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a
default value
Controller:
public function store(BannerRequest $request)
{
Banner::create($request->all());
flash()->success('Success', 'Your banner has been created.');
return back(); // temporary
}
and my Table:
Schema::create('banners', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade')->onUpdate('cascade');
$table->string('street', 40);
$table->string('city', 40);
$table->string('zip', 15);
$table->string('country', 40);
$table->string('state', 40);
$table->integer('price');
$table->text('description');
$table->timestamps();
});
and this is my model :
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Banner;
use Symfony\Component\HttpFoundation\File\UploadedFile;
class Photo extends Model
{
protected $table = 'banners_photos';
protected $fillable = ['name', 'path', 'Thumbnail_path'];
protected $file;
public function banners()
{
return $this->belongsTo(Banner::class);
}
public static function formFile(UploadedFile $file)
{
$photo = new static;
$photo->file = $file;
$photo->fill([
'name' => $photo->fileName(),
'path' => $photo->filePath(),
'Thumbnail_path' => $photo->ThumbnailPath()
]);
return $photo;
}
public function fileName()
{
$name = sha1(time() . $this->file->getClientOriginalName());
$extension = $this->file->getClientOriginalExtension();
return "{$name}.{$extension}";
}
public function filePath()
{
return $this->baseDir() . DIRECTORY_SEPARATOR . $this->fileName();
}
public function ThumbnailPath()
{
return $this->baseDir() . '/tn-' . $this->fileName();
}
public function baseDir()
{
return 'images/photos';
}
public function upload()
{
$this->file->move($this->baseDir(), $this->fileName());
$this->makeThumbnail();
return $this;
}
public function makeThumbnail()
{
Image::make($this->filePath())->fit(200)->save($this->ThumbnailPath());
return $this;
}
}
This code worked already and after refactoring become difficult.
Thank for your helps.
According to that message user_id is not set, therefore it's likely not present in $request->all().
If user_id is present, then you might want to check the user_id is in the $fillable property of your Banner model.
If you are trying to assign your banner to the current user, you could do something like this:
$data = $request->all();
$data['user_id'] = $request->user()->id;
Banner::create($data);
Assuming all other data in the BannerRequest is correct and the user is signed in this should work.
SIDE NOTE: ->onUpdate('cascade') looks dangerous, if the user is modified the database will try and drop the banner. not sure if that is your intention.
Solution :
In public function store :
$banner = auth()->user()->publish(new Banner($request->all()));
add public function publish in User model:
public function publish(Banner $banner)
{
return $this->banners()->save($banner);
}
In your migratin you have this
$table->integer('user_id')->unsigned();
the errors is because in your request->all() you don't have an user_id field, if you need it add it, if not in your migration add
$table->integer('user_id')->unsigned()->nullable();

How to make user, and roles relationship in Laravel 5

I have two tables :
User ->
id :
name :
role_id : ->references('id')->on('roles');
Roles ->
id :
role_name :
access :
I am trying to access roles details from user.
My User model has:
public function role()
{
return $this->belongsTo('App\Role');
}
My Role model has:
public function user()
{
return $this->hasMany('App\User');
}
When I try to do following :
$user = User::find(1);
$details = [
'name' => $user->first_name,
'role' => $user->role->role_name
];
I get error :
Trying to get property of non-object
My roles table contains access columns containing array of permissions to different routes. So my user will have only one role. While a role can have multiple users.
How to do that?
In my recent project, I handled these requirement in that way..
First of All Database Table Structure/Migration
User Table
class CreateUserTable extends Migration {
public function up() {
Schema::create('user', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->boolean('status')->default(0);
$table->boolean('is_admin')->default(0);
$table->boolean('notify')->default(0);
$table->rememberToken();
$table->timestamps();
});
}
public function down() {
Schema::drop('user');
}
}
Role Table
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRoleTable extends Migration {
public function up()
{
Schema::create('role', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('display_name')->nullable();
$table->string('description')->nullable();
$table->boolean('status')->default(0);
$table->timestamps();
});
}
public function down()
{
Schema::drop('role');
}
}
Role And User Relation Table
class CreateRoleUserTable extends Migration {
public function up() {
// Create table for associating roles to users (Many-to-Many)
Schema::create('role_user', function (Blueprint $table) {
$table->integer('user_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('user_id')->references('id')->on('user')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('role')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['user_id', 'role_id']);
});
}
public function down() {
Schema::drop('role_user');
}
}
After these table you have to handle permission by assigning to specific Role.
Permission
class Permission extends Migration {
public function up() {
Schema::create('permission', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->string('pattern');
$table->string('target');
$table->string('module');
$table->string('display_name')->nullable();
$table->boolean('status')->default(0);
$table->timestamps();
});
}
public function down() {
Schema::drop('permission');
}
}
Permission and Role Table Relation
class PermissionRole extends Migration {
public function up() {
// Create table for associating roles to permission (Many-to-Many)
Schema::create('permission_role', function (Blueprint $table) {
$table->integer('permission_id')->unsigned();
$table->integer('role_id')->unsigned();
$table->foreign('permission_id')->references('id')->on('permission')
->onUpdate('cascade')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('role')
->onUpdate('cascade')->onDelete('cascade');
$table->primary(['permission_id', 'role_id']);
});
}
public function down() {
Schema::drop('permission_role');
}
}
And Finally our model would look alike:
User Model
namespace App;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
protected $table = 'user';
protected $fillable = ['name', 'email', 'password', 'is_admin'];
protected $hidden = ['password', 'remember_token'];
public function scopeActive($query) {
return $query->whereStatus('1');
}
public function scopeAdmin($query) {
return $query->whereIsAdmin('1');
}
public function scopeNotify($query) {
return $query->whereNotify('1');
}
public function roles() {
return $this->belongsToMany('App\Role', 'role_user', 'user_id', 'role_id');
}
public function attachRole($role) {
if (is_object($role)) {
$role = $role->getKey();
}
if (is_array($role)) {
$role = $role['id'];
}
$this->roles()->attach($role);
}
public function detachRole($role) {
if (is_object($role)) {
$role = $role->getKey();
}
if (is_array($role)) {
$role = $role['id'];
}
$this->roles()->detach($role);
}
public function attachRoles($roles) {
foreach ($roles as $role) {
$this->attachRole($role);
}
}
public function detachRoles($roles) {
foreach ($roles as $role) {
$this->detachRole($role);
}
}
public function isSuperUser() {
return (bool)$this->is_admin;
}
public function hasAccess($permissions, $all = true) {
if ($this->isSuperUser()) {
return true;
}
return $this->hasPermission($permissions, $all);
}
public function hasPermission($permissions) {
$mergedPermissions = $this->getMergedPermissions();
//dd($mergedPermissions);
if (!is_array($permissions)) {
$permissions = (array)$permissions;
}
foreach ($permissions as $permission) {
$matched = false;
// We will set a flag now for whether this permission was
// matched at all.
$founded_perms = find_in($mergedPermissions, "name", $permission);
if (!empty($founded_perms)) {
$matched = true;
}
}
if ($matched === false) {
return false;
}
return true;
}
public function getMergedPermissions() {
$permissions = array();
foreach ($this->getRoles() as $group) {
$permissions = array_merge($permissions, $group->permissions()->get()->toArray());
}
return $permissions;
}
public function getRoles() {
$roles = [];
if ($this->roles()) {
$roles = $this->roles()->get();
}
return $roles;
}
}
Role Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'role';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'display_name', 'description'];
public function scopeActive($query) {
return $query->whereStatus('1');
}
/**
* Many-to-Many relations with User.
*
* #return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function users() {
return $this->belongsToMany('App\User');
}
public function permissions() {
return $this->belongsToMany("App\Permission");
}
}
Permission Model
namespace App;
use Illuminate\Database\Eloquent\Model;
class Permission extends Model {
protected $table = 'permission';
/**
* The attributes that are mass assignable.
*
* #var array
*/
protected $fillable = ['name', 'pattern', 'target', 'module', 'display_name', 'status'];
public static function displayable() {
$prepared_array = [];
$temp = self::orderBy('module')->get()->toArray();
foreach ($temp as $sin) {
$prepared_array[$sin['module']][] = $sin;
}
return $prepared_array;
}
public function scopeActive($query) {
return $query->whereStatus('1');
}
public function roles() {
return $this->belongsToMany("App\Role");
}
}
Well, thats the basic structure helped to implement basic ACL and Auth with laravel 5.
Let me know if you have any further related question. Or If you need complete implementation I'll provide it to you.
For a one-to-many relationship you don't need a pivot table, so you can delete the user_roles table. Then add a role_id column to your users table, that will reference the id column in for your roles table. Next define the relations as follows for each of your models:
// User.php
public function role()
{
return $this->belongsTo('App\Role');
}
and
// Role.php
public function users()
{
return $this->hasMany('App\User');
}
Now you can access your role via the relation like this:
$user->role->name;
I got the problem, i was having a role column in user table, so when i was doing
$user->role->role_name
it was fetching role column instead of relationship.
i noticed you are not using the laravel default table naming conventions,
you are using user_roles whereass laravel naming conventions state you should use:
role_user (alphabetical and singular)
you could override the belongsToMany by stating your custom table name ofcource.
public function users() {
return $this->belongsToMany('App\User', 'user_roles');
}
on the second node there are also some good libraries to handle these kind of things, take a look at : https://github.com/romanbican/roles

Resources