Give role acess in php? - 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.

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)
]);

Pagination does not change the items on different pages

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() }}

Missing Bootstrap pagination page numbers in Laravel 8

I copied the table from blade file B which pagination is working fine to blade file A. But the pagination is not working on Blade file A. It does have 10 results shown, but the page number navigation is missing.
Here's a screenshot for reference:
Here is my code:
Blade file A
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<table class="table table-bordered">
<tr>
<th></th>
<th>No</th>
<th>Zone</th>
<th>Code</th>
<th>Description</th>
</tr>
#foreach ($items as $key => $item)
<tr>
<td>{{ Form::checkbox('item[]', $item->id, false, array('class' => 'name')) }}</td>
<td>{{ $loop->index+1 }}</td>
<td>{{ $item->zone }}</td>
<td>{{ $item->code }}</td>
<td>{{ $item->description }}</td>
</tr>
#endforeach
</table>
</div>
</div>
Blade file B:
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Zone</th>
<th>Code</th>
<th>Description</th>
<th width="280px">Action</th>
</tr>
#foreach ($items as $key => $item)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $item->zone }}</td>
<td>{{ $item->code }}</td>
<td>{{ $item->description }}</td>
<td>
{{-- <a class="btn btn-info" href="{{ route('items.show',$item->id) }}">Show</a> --}}
#can('item-edit')
<a class="btn btn-primary" href="{{ route('items.edit',$item->id) }}">Edit</a>
#endcan
#can('item-delete')
{!! Form::open(['method' => 'DELETE','route' => ['items.destroy', $item->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
#endcan
</td>
</tr>
#endforeach
</table>
Controller A:
public function create(Request $request)
{
$items = Item::paginate(10);
$sites = Site::get(["name", "id"])->all();
// $states = State::all();
// $cities = City::where("state_id",14)->get(["name", "id"]);
return view('services.create', compact('sites', 'items'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
Controller B:
public function index(Request $request)
{
$items = Item::orderBy('id', 'DESC')->paginate(10);
return view('items.index', compact('items'))
->with('i', ($request->input('page', 1) - 1) * 5);
}
This is the problem with the new Version laravel.
In your App\Providers\AppServiceProvider class, you need to add the code below inside the boot() function to support the bootstrap paginator.
use Illuminate\Pagination\Paginator;
public function boot()
{
Paginator::useBootstrap();
}
At the end of Blade </table> please add this.
<div class="d-felx justify-content-center">
{{ $items->links() }}
</div>

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');

unable to delete subcategory with laravel

I know it sounds stupid but I've got strange issue with deleting my subcategories, when I try to delete my sub it gives me error that name and slug is required! seems I try to add input in database!
here is some images to make it clear for you:
Here is my controller destroy function:
public function destroy($id)
{
$subcategory = Subcategory::find($id);
$subcategory->delete();
Session::flash('success', 'Your Sub-Category Deleted successfully!');
return redirect()->route('subcategories.index');
}
And this is my form which you see in images above:
<table class="table table-bordered table-striped">
<thead>
<tr>
<th>ID</th>
<th>Subcategory Name</th>
<th>Slug</th>
<th>Parent Category</th>
<th>Operation</th>
</tr>
</thead>
<tbody>
#foreach ($categories as $category)
#foreach($category->subcategories as $sub)
<tr>
<td>{{ $sub->id }}</td>
<td>{{ $sub->name }}</td>
<td>{{ $sub->slug }}</td>
<td>{{ $sub->category->name }}</td>
<td>
Edit
{!! Form::model(['route' => ['subcategories.destroy', $sub->id], 'method' => "DELETE"]) !!}
{{ Form::submit('Delete', ['class' => 'btn btn-sm btn-danger']) }}
{!! Form::close() !!}
</td>
</tr>
#endforeach
#endforeach
</tbody>
</table>
I think it caused the Form::open return:
<form method="DELETE" action="{{route('subcategories.destroy', ['id' => $sub->id])}}">
I think that is the problem, I have same probleme here and I have read about this in Laracast.
So in my case I change not use Form::open(). I use manual form, and my code looked like this:
<form method="POST" action="{{route('subcategories.destroy', ['id' => $sub->id])}}">
{{ method_field('DELETE') }}
.
.
.
</form>
I hope it can help you :)

Categories