I have 3 tables which are user->id, name,password, roles->id,role, role_user->role_id, user_id
Now if I want to show the associated data in view page means role related to user ..How i do it in view?
means I want to show if the user has role it will be checked otherwise it will be unchecked
Here is my controller :
$users = User::all();
$roles = Role::all();
return view("Permission::assign_role",compact('users','roles'));
View page
<table class="table">
<thead>
<tr>
<th>SL No</th>
<th>Name</th>
<th>Role</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach($users->roles() as $row)
<tr>
<td>{{$i}}</td>
<td>{{$row->name}}</td>
<td><input type="checkbox" name="role_id" checked> $role->role</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
Eager load the data:
$users = User::with('roles')->get();
return view("Permission::assign_role", compact('users'));
And in the view, you can do something like this if you want to check if user has specific role:
#if ($user->roles->contains($roleId))
If you want to just iterate over user's roles:
#foreach($users->roles as $role)
{{ $role->name }}
This how I am using for my applications :
User Model :
public function roles() {
return $this->belongsToMany(Role::class);
}
Role Model :
public function user() {
return $this->belongsToMany(User::class);
}
In My Controller :
$users = User::all();
In My View
#foreach($users as $user)
#foreach($user->roles() as $role)
{{$role->name}}
#endforeach
#endforeach
Related
I am trying to get all users and their associated (1:n) journals. However I want to add pagination to the associated journals, not the users
My Controller:
public function index()
{
$users = User::with(['journal'])->orderBy('name', 'asc')->get();
return view('journals/journals', ['users' => $users]);
}
My Blade:
#foreach($users as $user)
<a class="list-group-item list-group-item-action" data-toggle="collapse" href="#collapse{{$user->id}}" role="button" aria-expanded="false" aria-controls="collapse{{$user->id}}">
{{$user->name}}
</a>
<div class="collapse mt-1" id="collapse{{$user->id}}">
<div class="card card-body">
<div class="list-group">
<table class="table table-sm table-hover">
<thead>
<tr>
<th scope="col">Kunde</th>
<th scope="col">Betreff</th>
<th scope="col">Leistungsart</th>
<th scope="col">Beginn</th>
<th scope="col">Ende</th>
<th scope="col">Dauer</th>
<th scope="col">Arbeitszeit</th>
</tr>
</thead>
<tbody>
#foreach($user->journal as $journal)
<tr onclick="window.location='{{route('journal.show', [$journal->id])}}'">
<th scope="row">{{$journal->customer->name}}</th>
<td>{{$journal->title}}</td>
<td>{{$journal->type}}</td>
<td>{{$journal->started}}</td>
<td>{{$journal->ended}}</td>
<td>{{$journal->duration}}</td>
<td>{{$journal->worked}}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
</div>
</div>
#endforeach
My Journal Model:
public function user(){
return $this->belongsTo('App\User', 'user_id', 'id');
}
My User Model:
public function journal(){
return $this->hasMany('App\Journal', 'user_id', 'id');
}
Again, what I am trying to achieve is that I can print out every user and paginate their journals, not paginate the users
I really hope someone can help me there
Render in blade
Above your journals for-each loop in your blade, try and put something like this:
#php
$paginated_journals = $user->journal()->paginate(10);
#endphp
And then, your for-each loop should look like this:
#foreach($paginated_journals as $journal)
...
#endforeach
After the for-each loop you can just put:
$paginated_journals->links()
to get the links for pagination
Pre-load data in controller
You can do the same thing server-side. Create a custom array that is empty, go through each user, and add sub array to that custom array:
array_push($custom_array, ['user' => $user, 'journals' => $user->journal()->paginate(10)])
This way you can send the custom array to your blade, loop through it, and render user data and paginated journals.
I have a problem, I made a support system, and when I want to enter the page where I watch the ticket, I will get 404 not found. Basically the route is that of the id in the database.
Routes:
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
Controller:
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate() {
$tickets = Ticket::latest()->get();
return view('updateTicket', compact('tickets'));
}
View:
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<form>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</form>
</tr>
#endforeach
</tbody>
I really don't understand the problem, a way to solve and did not receive error 404? Btw, I read the other topics and I couldn't find a solution.
The link you are setting in the href attribute is only the ticket id, but it should be, for example, "/viewTickets/1"
Try to put this:
href="{{route('updateTicket', ['ticket' => $ticket->id])}}"
In your route :
Route::get('/viewTickets', 'TicketController#view_MyTickets')->name('viewTickets');
Route::get('/viewTickets/{ticket}', 'TicketController#view_MyTicketUpdate')->name('updateTicket');
Route::post('/viewTickets/{ticket}', 'TicketController#update_MyTicket');
In your Controller :
public function view_MyTickets() {
$tickets = Ticket::latest()->get();
return view('viewTickets', compact('tickets'));
}
public function view_MyTicketUpdate(Request $request, $ticket) {
$tickets = Ticket::find($ticket);
return view('updateTicket', compact('tickets'));
}
In your Blade :
<tbody>
#foreach($tickets as $ticket)
<tr>
<td>{{$ticket->id}}</td>
<td>{{$ticket->user_id}}</td>
<td>{{$ticket->title}}</td>
<td>{{$ticket->category}}</td>
<td>{{$ticket->status}}</td>
<td>
<i class="material-icons"></i>
<i class="material-icons"></i>
</td>
</tr>
#endforeach
</tbody>
home blade
<div class="card-header">Total Users: {{ $users->count() }} </div>
<div class="card-body">
#if (session('status'))
<div class="alert alert-success" role="alert">
{{ session('status') }}
</div>
#endif
<table class="table table-dark table-striped">
<tr>
<th>SL</th>
<th>Name</th>
<th>Email</th>
<th>Created at</th>
</tr>
#foreach($users as $user)
<tr>
<td>{{$loop->index +1}}</td>
<td>{{ $user-> name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->created_at->diffForHumans()}}</td>
</tr>
#endforeach
</table>
</div>
HomeController
public function index()
{
$users = User::all();
return view('home', compact('users'));
}
When I am logged in I want to see all the other users information in a table other than the logged-in user in Laravel. How can I accomplish that?
While the accepted answer works, a nicer approach would be to make use of Laravel's except() Collection method:
The except method returns all items in the collection except for those with the specified keys
Your query then just returns all users except the currently logged in user to your view. No logic in your view, no other changes required:
public function index()
{
$users = User::all()->except(Auth::id);
return view('home', compact('users'));
}
If you pass the current user's ID along with the user list you can simply test against that ID.
public function index()
{
$users = User::all();
return View::make('home', array(
'users' => $users,
'user_id' => Auth::id()
));
}
#foreach($users as $user)
#if ($user->id != $user_id)
<tr>
<td>{{$loop->index+1}}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->created_at->diffForHumans()}}</td>
</tr>
#endif
#endforeach
I'm currently learning Laravel 5.6. So my problem is this, I have the following Model:
class Tour extends Model
{
//
protected $fillable = [
'name',
'price',
'category',
'overview',
'activity',
'exclusion',
'inclusion',
'policies',
'guide_id',
'province_id'
];
public function tourImages(){
return $this -> hasMany('App\TourImage');
}
}
class TourImage extends Model
{
//
protected $fillable = [
'name',
'path',
'tour_id'
];
public function tour(){
return $this -> belongsTo('App\Tour');
}
}
So 1 Tour can have multiple TourImages. What i want to do is that i want to print out all the tours on the index page. But i only want to take one TourImage file to display for each tour. I return only array of all tours from controller. Is there a way that i can retrieve image directly on the blade view without returning more variable from the controller?
Here's what I write for my index method:
public function index($province_id = null)
{
$tours = Tour::where('province_id', $province_id)->get();
return view('tours.index', ['tours' => $tours]);
}
Here's my index view:
#extends('layouts.app')
#section('content')
<div class="col-md-8 offset-md-2">
<br/>
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Name</th>
<th scope="col">Picture</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody>
#foreach($tours as $tour)
<tr>
<th scope="row">{{ $tour->id }}</th>
<td>{{ $tour->name }}</td>
<td><img src="/storage/{{ $tour->tourImages->path }}" width="200px"/></td>
<td>{{ $tour->price }}</td>
</tr>
#endforeach
</tbody>
</table>
</div>
#endsection
you can pass one image of each tour by this code :
$tours=Tour::with(['tourImages'=>function($query){
$query->first();
}])->get();
and in view:
#foreach($tours as $tour)
#if ($tour->tourImages->count())
<img src="/storage/{{ $tour->tourImages[0]->path }}" width="200px"/>
#endif
#endforeach
you need to use with() to get records.like this.
$tours = Tour::with('tourImages')->where('province_id', $province_id)->get();
You can add new relation in your Tour model like
public function latestTourImage() {
return $this->hasOne('App\TourImage')->latest();
}
Then you can fetch it using this in your controller file
$tours = Tour::with('latestTourImage')->where('province_id', $province_id)->get();
and then you can print it in your blade file like
$tour->latestTourImage->path
I'm trying to get value from my tables with many to many relationship.
My relation are:
User:
public function roles()
{
return $this->belongsToMany('App\Role',"users_roles","usersid","rolesid");
}
Role:
public function users()
{
return $this->belongsToMany('App\User',"users_roles","usersid","rolesid");
}
I did this in my view:
#foreach($users as $user)
<?php $i++; ?>
<tr>
<td>{{ $i }}</td>
<td>{{$user->name}}</td>
<td>{{$user->email}}</td>
<td>{{$user->roles->role}}</td>
<tr>
#endforeach
I'm getting following error:
Undefined index: role
But when i do {{$user->roles}}, I get following:
[{"id":1,"role":"Administrator","created_at":null,"updated_at":null,"pivot":{"usersid":4,"rolesid":1}}]
Can anyone tell me where did i go wrong?
You can do
<td>
#if($user->roles)
{{$user->roles->first()->role}}
#else
No role for this user
#endif
</td>
You need to use Eager load when you are fetching the users