Created
April 1, 2015 15:17
-
-
Save rodrigopedra/71e2defa0534ec4a567d to your computer and use it in GitHub Desktop.
Laravel: Manage many-to-many relationship - with view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App; | |
use Illuminate\Database\Eloquent\Model; | |
class Role extends Model | |
{ | |
protected $table = 'roles'; | |
protected $fillable = [ 'name' ]; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Roles</title> | |
</head> | |
<body> | |
<h1>Roles for {{ $user->name }}</h1> | |
<form action="{{ route('user.update', [$user]) }}" method="POST" accept-charset="UTF-8"> | |
<input type="hidden" name="_token" value="{{ csrf_token() }}"> | |
<ul> | |
@forelse($roles as $role) | |
<li> | |
<label> | |
<input type="checkbox" name="roles[]" | |
value="{{ $role->id }}" {{ $user->hasRole($role) ? 'checked' : '' }}> | |
{{ $role->name }} | |
</label> | |
</li> | |
@empty | |
<li>no roles</li> | |
@endforelse | |
</ul> | |
<p> | |
<button type="submit">Save</button> | |
</p> | |
</form> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** @var Illuminate\Routing\Router $router */ | |
$router->get( '/user/{id}/roles', ['as' => 'user.edit', 'uses' => 'UserController@edit' ] ); | |
$router->post( '/user/{id}/roles', ['as' => 'user.update', 'uses' => 'UserController@update' ] ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php 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 = 'users'; | |
protected $fillable = [ 'name', 'email', 'password' ]; | |
protected $hidden = [ 'password', 'remember_token' ]; | |
public function roles() | |
{ | |
return $this->belongsToMany( 'App\Role', 'role_user' ); | |
} | |
public function hasRole( Role $role ) | |
{ | |
return $this->roles->contains( $role ); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\User; | |
use App\Role; | |
class UserController extends Controller | |
{ | |
public function edit( $id ) | |
{ | |
// TODO: use route model binding | |
$user = User::with('roles')->findOrFail( $id ); | |
$roles = Role::all(); | |
return view( 'roles', compact( 'user', 'roles' ) ); | |
} | |
public function update( $id, Request $request ) | |
{ | |
$roles = $request->get( 'roles', [] ); | |
$user = User::findOrFail( $id ); | |
$user->roles()->sync( $roles ); // this does the trick | |
return redirect()->back()->with( 'info', 'success' ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment