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 :)
Related
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)
]);
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.
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>
I used the following code to display the role of the user
however, when I want to edit or delete it leads me to the deleted ids number 2/3 it return the same thing even when I change the for else with foreach Attempt to read property "id" on null (View: C:\laravel\gauto\resources\views\dashboard\user\edit.blade.php)!
when I delete the nested foreach loop everything back to normal
thank you in advance
#forelse($users as $item)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $item->name }}</td>
<td>{{ $item->email }}</td>
<td>
#if($item->email_verified_at)
{{ "verified" }}
#else
{{ "not verified" }}
#endif
</td>
<td>
#foreach($item->roles as $item)
{{ $item['name'] }}
#endforeach
</td>
<td>
Edit
<form action="{{ route('user.destroy', $item->id) }}" method="POST">
#csrf
#method('delete')
<button onclick="return confirm('Are you sure ??')" type="submit">Delete</button>
</form>
</td>
</tr>
#empty
<tr>
blank data
</tr>
#endforelse
Your users are also saved in $item and in the loop you are allocating $item a different value every time.
Try changing the code to
<td>
#foreach($item->roles as $role)
{{ $role['name'] }}
#endforeach
</td>
I have a little problem with passing some data from my view to my controller.
Please have a look at this code snippet:
{!! Form::open(['action' => 'DomainController#detach', 'method' => 'post']) !!}
#foreach($domains as $domain)
<tr>
<td>{{ $domain->name }}</td>
<td>{{ $domain->tld }}</td>
<td id="hello">
#foreach($domain->tags as $tag)
{{ $tag->name }},<br>
#endforeach
</td>
<td>
#foreach($domain->tags as $tag)
{!! Form::hidden('tag_id[]', $tag->id) !!}
<button name="domain_id" value="{{ $domain->id }}" class="glyphicon glyphicon-trash"></button>
<br>
#endforeach
</td>
</tr>
#endforeach
{!! Form::close() !!}
In my Controller is a :
$input = Input::all();
return $input;
In my code ( in the last ) is a button. If I press teh button, I'm getting directed to my controller action. I'm returning the die data in my $input variable and it allways shows me the same tag_id. Allways the very last tag_id of this domain. I don't know why and couldn't figure it out.
It should be:
{!! Form::hidden('tag_id[]', $domain->pivot->id) !!}
you're missing the [] in input
You have 2 fields with the same name:
{!! Form::hidden('tag_id', $domain->pivot->id) !!}
<button name="tag_id" value="{{ $domain->id }}" class="glyphicon glyphicon-trash"></button>