im working on a laravel 5.5 project
and they told me to make role for every page
create edit update delete search softdelete
so i have created user table
this is the migration
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('user_name',40)->unique();
$table->string('user_email',40)->nullable()->unique();
$table->string('user_phone',40)->nullable()->unique();
$table->integer('user_department');
$table->string('password',60);
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
}
and i have created model table this is the migration
public function up()
{
Schema::create('models', function (Blueprint $table) {
$table->increments('id');
$table->string('model_name',40)->unique();
$table->timestamps();
$table->softDeletes();
});
}
and i have created modelroles table
public function up()
{
Schema::create('modelroles', function (Blueprint $table) {
$table->increments('id');
$table->integer('model_id');
$table->integer('user_id');
$table->integer('create');
$table->integer('read');
$table->integer('update');
$table->integer('softdelete');
$table->integer('delete');
$table->integer('restore');
$table->integer('search');
$table->timestamps();
});
}
and i have created middlewaer called roleMiddleware
this is the code
use Closure;
class roleMiddleware
{
public function handle($request, Closure $next)
{
return $next($request);
}
}
so how can i connect them together in the middleware
what i want is to give every user role in every page create read update delete ect
mean check the url the request come from
and check the user id
and check the model id
and check the user_id and the model_id inside modelroles
and if he can create edit update delete on the model
i dont want to use packages like this
https://github.com/laravel-enso/RoleManager
or this
https://github.com/Zizaco/entrust
i dont want to connect users with global role
i want to give every user role for every controller
Related
I am working on an app where the users can have trainings attached to them with a little description.
So, they can have a training in High School and the description they attach is "Rockford High School". However, I want them to be able to put several times the same type, so they could have 3 High School, or University, etc.
However, when I try to attach two times the same model with the same id, one gets overwritten. I thought that using an intermediate model would solve the issue, but it didn't.
Here are my migrations and any relevant code.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Training extends Model
{
protected $fillable = ['name_french', 'name_english'];
public function candidates() {
return $this->belongsToMany('\App\Candidate')->using('App\CandidateTraining')->withPivot('description');
}
}
<?php
namespace App;
use App\Traits\SanitizeIds;
use App\Salary;
use Illuminate\Database\Eloquent\Model;
class Candidate extends Model
{
use SanitizeIds;
protected $fillable = [
'first_name',
'last_name',
'email',
'phone_number',
'address',
'field_of_work',
'formations',
'work_experiences',
'interests'
];
public function trainings() {
return $this->belongsToMany('\App\Training')->using('App\CandidateTraining')->withPivot('description');
}
}
<?php
namespace App;
use Illuminate\Database\Eloquent\Relations\Pivot;
class CandidateTraining extends Pivot
{
//
}
Schema::create('trainings', function (Blueprint $table) {
$table->id();
$table->string('name_french');
$table->string('name_english');
$table->timestamps();
});
Schema::create('candidate_training', function (Blueprint $table) {
$table->id();
$table->string('candidate_id');
$table->string('training_id');
$table->string('description');
$table->timestamps();
});
Schema::create('candidates', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('email');
$table->string('phone_number');
$table->string('address');
$table->string('field_of_work')->default('{}');
$table->string('formations')->default('{}');
$table->string('work_experiences')->default('{}');
$table->string('interests')->default('{}');
$table->string('cv')->default('');
$table->string('video_cv')->default('');
$table->timestamps();
});
I don't get how I could make this work and I haven't found any answer during my research. I also want the user to be able to edit them eventually, so delete one model with x id without deleting all the others with the same id but different description.
Call me an idiot but am thinking something like this would make sense for your situation:
Schema::create('trainings', function (Blueprint $table) {
$table->id();
$table->string('name_french');
$table->string('name_english');
// You should checkout 'spatie/laravel-translatable' package, that
// way you can use one column for 'name' and then translate it in realtime
// depending on your application locale.
$table->timestamps();
});
Schema::create('candidate_practice', function (Blueprint $table) {
$table->id();
$table->string('candidate_id');
$table->string('training_id');
$table->timestamps(); // Probably not needed
});
Schema::create('practices', function (Blueprint $table) {
$table->id();
$table->foreignId('candidate_id')
->constrained()
->onDelete('cascade');
$table->string('description');
$table->timestamps();
});
Schema::create('candidates', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->string('email');
$table->string('phone_number');
$table->string('address');
$table->string('field_of_work')->default('{}');
$table->string('formations')->default('{}');
$table->string('work_experiences')->default('{}');
$table->string('interests')->default('{}');
$table->string('cv')->default('');
$table->string('video_cv')->default('');
$table->timestamps();
});
Now you can create multiple practices related to a user by a one-to-many relationship which is a lot easier to edit individually or delete without stepping on other resources and then relate practices to training.
With this, creating a CandidatePractice.php pivot class is no longer necessary.
I build a referral system Using Laravel and VueJS. i want count referral each users.
I implement count it from User Model
public function getCountReferrerAttribute()
{
$user = User::where('referred_by', auth()->user()->name)->count();
return $user;
}
And i try to get it from Vuejs
Its show Referrer Count for Every User...
User Schema
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('referral_code')->unique()->nullable();
$table->string('referred_by')->nullable();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
Thanks in advance...
Assuming you are calling this function for every user on your table, your query is getting all user that is referred by the currently logged in user because of auth()->user()->name, instead your function should use the name of the user itself.
So it will look something like this
public function getCountReferrerAttribute()
{
$user = User::where('referred_by', $this->name)->count();
return $user;
}
I have this migration:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->timestamps();
});
}
And now I want to add a new column, and looks like this:
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('slug');
$table->string('pathlogo')->default('/');
$table->timestamps();
});
}
How can I make just an 'add column' on Laravel? I don't want to do php artisan migrate:refresh, or restart and then make again seed.
Now I have some data in DB which not exist on seed I just want to make a new column.
You need to change Schema::create to Schema::table (because you are not creating a table, just selecting it), and then your only line in that function should be:
$table->string('pathlogo')->default('/')->after('slug');
after will ensure the column is positioned how you want.
If you are still in development, ie. you don't have data in the table, you should just rollback all your migrations and edit the original.
Create a new migration and Use that as your up function:
public function up()
{
Schema::table('clients', function (Blueprint $table) {
$table->string('pathlogo')->default('/');
});
}
them migrate as normal.
I have a many-to-many relationship between my client and tag tables. A client can have many tags, and each tag can be related to multiple clients.
On the client show view, I'm trying to display the client info plus all tags associated to this client.
How do I change the query below to retrieve the client rows with all its related tags?
public function show($id)
{
$client = Client::findOrFail($id);
return view('clients.show')->with(['client' => $client]);
}
Client model
public function clienttag()
{
return $this->belongsToMany('App\Clienttag');
}
Clienttag model
public function client()
{
return $this->belongsToMany('App\Client');
}
Client_clientags table migration
public function up()
{
Schema::create('client_clienttag', function(Blueprint $table)
{
$table->integer('client_id')->unsigned();
$table->foreign('client_id')->references('id')->on('clients')->onDelete('cascade');
$table->integer('clienttag_id')->unsigned();
$table->foreign('clienttag_id')->references('id')->on('clienttags')->onDelete('cascade');
$table->timestamps();
});
}
Clients table migration
public function up()
{
Schema::create('clients', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->rememberToken();
$table->timestamps();
});
}
Clienttags table migration
public function up()
{
Schema::create('clienttags', function(Blueprint $table)
{
$table->increments('id');
$table->string('tag');
$table->text('description');
$table->rememberToken();
$table->timestamps();
});
}
You can use "Eager Loading" methods like the following
public function show($id)
{
$client = Client::with('clienttag')->findOrFail($id);
return view('clients.show')->with(['client' => $client]);
}
Check documentation at http://laravel.com/docs/5.1/eloquent-relationships#eager-loading
Then at your view you can print your tags
#foreach ($client->clienttag as $tag)
{!! $tag->tagname!!} (or whatever your field in clienttags table name is)
#endforeach
I currently have a Users to Groups Relationship (ManyToMany) with a pivot table group_user. I want the user to be able to create a group but once creating the group, how do I make it, that the creator becomes member of this group?
Currently I have
My Pivot Table (group_user):
Schema::create('group_user', function(Blueprint $table)
{
$table->integer('group_id')->unsigned()->index();
$table->foreign('group_id')->references('id')->on('groups')->onDelete('cascade');
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
My Groups table (groups):
Schema::create('groups', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->timestamps();
});
My Users table (users):
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('name');
$table->string('lastname');
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
My models ofcourse have the following: User.php
public function groups()
{
return $this->belongsToMany('App\Group');
}
Group.php
public function users()
{
return $this->belongsToMany('App\User');
}
What create function should I write in my controller so that when a User creates a Group, that he automaticly becomes member of this group (automaticlly make the pivot relationship)?
This should work, make sure you implement validation, ect.
public function store(Request $request)
{
$group = Group::create([ // <-- if names are unique. if not, then create is fine
'name' => $request->get('name')
]);
auth()->user()->groups()->attach([$group->id]);
return view('your.view');
}
Also make sure to add:
use App\Group;
See attach() and detach().
$user = User::find(1);
$user->groups()->attach(10); // pivot relationship of this user to group of id 1.
OR
$group = Group::find(10);
$user->groups()->save($group);
For many groups of this user:
$user->groups()->sync(array(1, 2, 3));