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.
Related
Hello i'm trying to display the authenticated user but i'm getting this error :
Undefined variable: users (View:
C:\wamp64\www\Blan\resources\views\users\index.blade.php)
this is my code :
usercontroller :
public function index()
{
return view('users.index')->with('user', Auth::user());
}
the view :
#extends('layouts.app')
#section('content')
<div class="card">
<div class="card-header">
Update Your Profile
</div>
<div class="card-body">
<table class="table table-hover">
<thead>
<th>Image</th>
<th>Name</th>
<th>Update</th>
</thead>
<tbody>
#if( $users -> count() > 0 )
#foreach ($users as $user)
<tr>
<td>
#if(isset($user->profile->avatar))
<img src="{{ asset($user->profile->avatar)}}" width="60px" height="60px" style="border-radius: 50%" alt="">
#else
No image
#endif
</td>
<td>
{{$user->name}}
</td>
<td>
Update
</td>
</tr>
#endforeach
#else
<tr>
<th colspan="5" class="text-center">No Users </th>
</tr>
#endif
</tbody>
</table>
</div>
</div>
#stop
But if i return all the users using this code in the controller :
return view('users.index')->with('users', User::all());
its work fine.
so how i can return only the current authenticated user ?
You don't need to send the auth user via your controller.Auth is the global facade in laravel.Also you don't need foreach because there is only 1 authenticated user. Just type Auth::user()->name in the blade
where you want to display the user
Yea and btw the error ur getting is because you are doing foreach for $users but sending $user to blade but im pretty sure it wont work even if you correct that typo
In your example
public function index()
{
return view('users.index')->with('user', Auth::user());
}
You have used "user"
But tried to access variable as "users"
Unless you just made a typo in your example?
So I have a table on blade view file, I have successfully exported it into the excel file. Now I want to export without exporting the 'Actions" column. How can I do that?
I have attached my code and blade file below, it works perfectly. I just want to export everything except the Actions Column as it contains Buttons for Database Operations
THIS IS MY EXPORT CLASS CODE:
public function view(): View
{
return view ('ims.view_inventory_history_table', [
'data' => inbound_history::all()
]);
}
public function headings(): array
{
return [
'ID',
'Warehouse',
'SKU',
'Child SKU',
'Units',
'Invoice No.',
'Container No.',
'Entry Date'
];}
/**
* #return array
*/
public function registerEvents(): array
{
return [
AfterSheet::class => function(AfterSheet $event) {
$cellRange = 'A1:W1'; // All headers
$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);
},
];
}}
THIS IS MY CONTROLLER FUNCTION:
public function export_view()
{
return Excel::download(new inboundHistory(), 'inboundHistory.xlsx');
}
THIS IS MY ROUTE:
Route::Get('inbound_history/export_view' ,'imsController#export_view')->name('inbound_history.export_view');
THIS IS MY BLADE TABLE VIEW:
<table id="datatables" class="table table-striped table-no-bordered table-hover" cellspacing="0" width="100%" style="width:100%">
<thead>
<tr>
<th style="width:5%">ID</th>
<th>Warehouse</th>
<th>SKU</th>
<th>Child SKU</th>
<th>Cases</th>
<th>Units</th>
<th>Invoice No.</th>
<th>Container No.</th>
<th>Entry Date</th>
<th class="disabled-sorting text-right" style="width:12%">Actions</th>
</tr>
</thead>
<tbody>
#foreach ($data as $row)
<tr>
<td>{{$row['id']}}</td>
<td>{{$row['warehouse']}}</td>
<td>{{$row['sku_parent']}}</td>
<td>{{$row['sku_child']}}</td>
<td>{{$row['total_cases']}}</td>
<td>{{$row['total_units']}}</td>
<td>{{$row['invoice_no']}}</td>
<td>{{$row['container_no']}}</td>
<td>{{$row['date_rec']}}</td>
<td class="td-actions text-right">
{{-- <a rel="tooltip" class="btn btn-success btn-link" href="{{action('imsController#edit',$row['id'])}}">
<i class="material-icons">edit</i></a> --}}
<a rel="tooltip" class="btn btn-danger btn-link" href="{{action('imsController#destroy',$row['id'])}}" onclick = "if (! confirm('Confirm: Press OK to delete the Entry.')) { return false; }"style="color: red;">
<i class="material-icons">close</i></a>
</td>
</tr>
#endforeach
</tbody>
I don't know if I got everything correctly, but why don't you do something like this:
Pass an additional variable to your blade template like $isView, when you want to create a view for the user.
And in your blade.php template you do something like this:
#isset($isView)
<th class="disabled-sorting text-right" style="width:12%">Actions</th>
#endisset
// do the same #isset test for the corresponding <td> element
When you want to render it to excel you just don't pass this variable and the column is not rendered.
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 am using Laravel 5.2.
My question is:
Can variable be classified in view?
For example:
There is a controller ,it can get all of the articles belonging to the current user, like this:
public function index()
{
$user=\Auth::user();
$articles = $user->articles;
return view('user.dashboard.index', compact('articles'));
}
In view,these codes below can show the articles,like this:
<div class="card">
<div class="card-header">
Articles
</div>
#if ($articles!=null)
<table class="table table-sm">
<thead>
<tr>
<th>title</th>
<th>created time</th>
<th>status</th>
</tr>
</thead>
<tbody>
#foreach ($articles as $article)
<tr>
<td>{{$article->title}}</td>
<td>{{$article->created_at}}</td>
<td>{{$article->status}}</td>
</tr>
#endforeach
</tbody>
</table>
#else
<p>Nothing</p>
#endif
</div>
These articles can be classified into two types:
1、“published”,status is 1.
2、“unpublished”,status is 0.
Question:
I would want the published articles to be shown in a card(div class="card"), and, the unpublished articles to be shown in another card.
Can they be classified in view? Thus,it doesn't need to query twice.
Try to use collection method where()
#foreach ($articles->where('published', 1)->all() as $article)
Just change published and 1 to real ones.