Pagination does not change the items on different pages - php

I added pagination in my application. The same results are displayed on each page and according to the URL, the pages are definitely changing. I've tried a ton of solutions but none worked. Please help me find what I did wrong
My Controller:
public function index(Request $request)
{
$competitions = Competition::latest()->paginate(1);
return view('competition.index', compact('competitions'));
}
Inside app/Providers/AppServiceProvider I added use Illuminate\Pagination\Paginator; and my function looks like this:
public function boot()
{
Paginator::useBootstrap();
}
That's how I loop in view:
#foreach ($competitions as $competition)
<tbody >
#switch($competition->status)
#case('active')
<tr>
#break
#case('to_push')
<tr class="bg-secondary">
#break
#case('inactive')
<tr class="bg-warning">
#break
#default
<tr>
#endswitch
<td>{{ $competition->id }}</td>
<td>{{ $competition->name }}</td>
<td>{{ $competition->status }}</td>
<td>{{ $competition->mode }}</td>
<td>{{ $competition->type }}</td>
<td>{{ $competition->frequency }}</td>
<td>{{ $competition->last_push }}</td>
<td>{{ $competition->next_push }}</td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('events.list_by_competition',['competition' => $competition->id]) }}">Events</a></td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('competitions.exports',$competition->id) }}">Exports</a></td>
<td class="text-center">
<form action="{{ route('competitions.destroy',$competition->id) }}" method="POST">
<a class="btn btn-primary btn-sm" href="{{ route('competitions.edit',$competition->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $competitions->links() }}

Related

Laravel Pagination does not change the pages

I wanted to add pagination to my project and followed a tutorial for that. When I click new page the results are not changing.
In my Controller I added this $competitions = Competition::latest()->paginate(1);
Then into app/Providers/AppServiceProvider added this use Illuminate\Pagination\Paginator; and inside the boot function added this: Paginator::useBootstrap();
And the last thing was to add this {{ $competitions->onEachSide(1)->links() }} after I close my table in the blade file.
EDIT:
Controller function:
public function index()
{
$competitions = Competition::latest()->paginate(1);
return view('competition.index', compact('competitions'));
}
Loop in blade:
#foreach ($competitions as $competition)
<tbody >
#switch($competition->status)
#case('active')
<tr>
#break
#case('to_push')
<tr class="bg-secondary">
#break
#case('inactive')
<tr class="bg-warning">
#break
#default
<tr>
#endswitch
<td>{{ $competition->id }}</td>
<td>{{ $competition->name }}</td>
<td>{{ $competition->status }}</td>
<td>{{ $competition->mode }}</td>
<td>{{ $competition->type }}</td>
<td>{{ $competition->frequency }}</td>
<td>{{ $competition->last_push }}</td>
<td>{{ $competition->next_push }}</td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('events.list_by_competition',['competition' => $competition->id]) }}">Events</a></td>
<td class="text-center"><a class="btn btn-info btn-sm" href="{{ route('competitions.exports',$competition->id) }}">Exports</a></td>
<td class="text-center">
<form action="{{ route('competitions.destroy',$competition->id) }}" method="POST">
<a class="btn btn-primary btn-sm" href="{{ route('competitions.edit',$competition->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger btn-sm">Delete</button>
</form>
</td>
</tr>
</tbody>
#endforeach
</table>
{{ $competitions->links('pagination::bootstrap-5') }}
dd(Competition::all()) returns the 4 records I have
Can you please tell me what I did wrong? Any and all help gratefully received.
$competitions = Competition::latest()->paginate(1) this command will return you one result and then pagination will take 1 per page so it won't be shown at all.
Try something like this in your controller
$competitions = Competition::paginate(1)
return view(...., ['competitions' => $competitions]);
and in your blade add this under the foreach loop where you display data
{{ $competitions->links() }}
Read more here
HERE IS HOW TO MAKE PAGINATION
BLADE
#foreach($paginate as $p)
{{ $p->name }} <br>
#endforeach
{{ $paginate->links() }}
CONTROLLER
return view('backend.test', [
'paginate' => Product::paginate(5)
]);

laravel 8 syntax error, unexpected '<' while calling component in View

Error showing on F:\xampp\htdocs\ERP\resources\views/building/summary.blade.php:20
following is code
#foreach($records as $record)
<tr>
<td>{{ $record->id }}</td>
<td>{{ $record->building->name }}</td>
<td>{{ $record->name }}</td>
<td>{{ $record->created_at }}</td>
<td>
<x-summary-action :view_path="{{ $view_path }}" :record="{{ $record }}">
</x-summary-action>
</td>
</tr>
#endforeach
And Component code is below
<a href="{{ route( $view_path . '.edit', $record->id) }}" class="btn btn-outline-secondary btn-sm edit" title="Edit">
<i class="fas fa-pencil-alt"></i>
</a>
remove the curly brackets when you pass the variables to the components
<x-summary-action :view_path="$view_path" :record="$record">
</x-summary-action>

Give role acess in php?

Is it right approach to write Laravel PHP code to give a role to hr to view certain column? Because after hosting the website stops automatically and start automatically. Code is working fine..
I am new to PHP, I have no idea if i am going wrong in this code can anyone help me out yrr...
<table class="table table-striped mb-0 dataTable">
<thead>
<tr>
#role('company')
<th>{{__('Employee Name')}}</th>
#endrole
#role('hr')
<th>{{__('Employee Name')}}</th>
#endrole
<th>{{__('Designation')}}</th>
<th>{{__('Promotion Title')}}</th>
<th>{{__('Promotion Date')}}</th>
<th>{{__('Description')}}</th>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<th width="200px">{{__('Action')}}</th>
#endif
</tr>
</thead>
<tbody class="font-style">
#foreach ($promotions as $promotion)
<tr>
#role('company')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
#role('hr')
<td>{{ !empty($promotion->employee())?$promotion->employee()->name:'' }}</td>
#endrole
<td>{{ !empty($promotion->designation())?$promotion->designation()->name:'' }}</td>
<td>{{ $promotion->promotion_title }}</td>
<td>{{ \Auth::user()->dateFormat($promotion->promotion_date) }}</td>
<td>{{ $promotion->description }}</td>
#if(Gate::check('Edit Promotion') || Gate::check('Delete Promotion'))
<td>
#can('Edit Promotion')
<i class="fas fa-pencil-alt"></i>
#endcan
#can('Delete Promotion')
<i class="fas fa-trash"></i>
{!! Form::open(['method' => 'DELETE', 'route' => ['promotion.destroy', $promotion->id],'id'=>'delete-form-'.$promotion->id]) !!}
{!! Form::close() !!}
#endif
</td>
#endif
</tr>
#endforeach
</tbody>
</table>
You can find what you are asking in the documentation here (version select on the left):
https://spatie.be/docs/laravel-permission/v5/basic-usage/blade-directives
#role('hr')
I am from hr!
#else
I am not from hr...
#endrole
It further reads #hasrole('hr') is an alias of #role('hr'), so no worries there.
Similarly, the same goes for permissions:
#can('edit articles')
//
#endcan
For more information, see link mentioned above.

ErrorException Undefined variable: data

i have this problem that you see from the title , it says undefined variable data
this is my post controller function :
public function index()
{
$data = Post::latest()->paginate(5);
return view('posts.index', compact('data'))
->with('i', (request()->input('page', 1) - 1) * 5);
}
and this is my index.blade.php file :
#foreach ($data as $key => $value)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $value->title }}</td>
<td>{{ \Str::limit($value->description, 100) }}</td>
<td>
<form action="{{ route('posts.destroy',$value->id) }}" method="POST">
<a class="btn btn-info" href="{{ route('posts.show',$value->id) }}">Show</a>
<a class="btn btn-primary" href="{{ route('posts.edit',$value->id) }}">Edit</a>
#csrf
#method('DELETE')
<button type="submit" class="btn btn-danger">Delete</button>
</form>
</td>
</tr>
this is where i used the data .
but as you see an error comes up .
what can i do ?

Route [student.delete] not defined using Laravel 7

i am a beginner of laravel. i ran into the problem with Route [student.delete] not defined. using laravel 7. when run the laravel project. what i tried so far i attached below. i attached the controller and view and route file below i don't what was a problem.
Controller.
public function destroy(Student $student)
{
$student->delete();
return redirect()->route('student.index')->with('success', 'Student has been Deleteddddd');
}
index.blade.php
#foreach ($students as $student)
<tr>
<td>{{ $student->id }}</td>
<td>{{ $student->name }}</td>
<td>{{ $student->phone }}</td>
<td>{{ $student->address }}</td>
<td>
<a class="btn btn-primary" href="{{ route('student.edit',$student->id) }}">Edit</a>
<a class="btn btn-primary" href="{{ route('student.delete',$student->id) }}">Delete</a>
</form>
</td>
</tr>
#endforeach
Routes
Route::get('/', 'StudentController#index');
Route::get('/students', 'StudentController#create')->name('student.create');
Route::post('/students', 'StudentController#store')->name('student.store');
Route::get('/students/{student}', 'StudentController#edit')->name('student.edit');
Route::post('/students/{student}', 'StudentController#update')->name('student.update');
//Route::delete('/students/{student}', 'StudentController#destroy')->name('student.destroy');
Route::get('/students/{student}', 'StudentController#destroy')->name('student.destroy');

Categories