Created
November 7, 2017 15:59
-
-
Save alissonsilvas/ffd28b0b83aab293a67df1861141ec63 to your computer and use it in GitHub Desktop.
laravel relacionamentos
This file contains 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
##Model## | |
class Role_User extends Model | |
{ | |
protected $table = 'role_user'; | |
protected $fillable = ['id','user_id','role_id']; | |
public function users() | |
{ | |
return $this->belongsTo('App\User'); | |
} | |
public function roles() | |
{ | |
return $this->belongsTo('App\Role'); | |
} | |
} | |
class Role extends Model | |
{ | |
public function role_user() | |
{ | |
return $this->hasMany('App\Role_User'); | |
} | |
} | |
//abreviei para no fica um monte de linhas | |
class User extends Authenticatable | |
{ | |
public function role_user() | |
{ | |
return $this->hasMany('App\Role_User'); | |
} | |
} | |
##controller## | |
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use App\Role_User; | |
class RoleController extends Controller | |
{ | |
/** | |
* Display a listing of the resource. | |
* | |
* @return \Illuminate\Http\Response | |
*/ | |
public function index(Request $request) | |
{ | |
$perfis = Role_User::with(['users','roles'])->first()->paginate(10); | |
return view('perfil.list',compact('perfis')) | |
->with('i', ($request->input('page', 1) - 1) * 10); | |
} | |
} | |
##view## | |
<div class="panel-body"> | |
<div class="table-responsive"> | |
<table class="table table-hover table-responsive table-bordered table-striped"> | |
<tr class="active"> | |
<th>Usuário</th> | |
<th>Perfil</th> | |
<th class="text-center">Excluir</th> | |
</tr> | |
@foreach ($perfis as $pe) | |
<tr> | |
<td>{{ $pe->users->name }}</td> | |
<td>{{ $pe->roles->perfil }}</td> | |
<td class="text-center"><strong> | |
{{ Form::open(array('url' => 'peril/'.$pe->id, 'class' =>'text-center', 'onclick' => 'return confirm("Deseja realmente excluir esse registro?")')) }} | |
{{ Form::hidden('_method', 'DELETE') }} | |
{{ Form::submit('Excluir', array('class' => 'btn btn-danger btn-xs bold')) }} | |
{{ Form::close() }} | |
</strong></td> | |
</tr> | |
@endforeach | |
</table> | |
{!! $perfis->appends(request()->input())->links() !!} | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Error: Trying to get property of non-object (View: /opt/lampp/htdocs/nsigtur/resources/views/perfil/list.blade.php)