cannot find id to delete data laravel 8 - php

i try to delete data using destroy() method of resource controller, but can't delete data, when i use dump and die, id can't be found, what should i do?
this is my controller:
public function destroy(User $user)
{
User::destroy($user->id);
return redirect('/dashboard/administrators')->with('success', 'Admin has been deleted');
}
this is my route:
Route::resource('/dashboard/administrators', DashboardAdministratorController::class)->middleware('auth');
and this is my delete form:
#foreach($users as $user)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->username }}</td>
<td>{{ $user->email }}</td>
<td>
<form action="/dashboard/administrators/{{ $user->id }}" method="POST" class="d-inline">
#method('delete')
#csrf
<button class="badge bg-danger border-0" onclick="return confirm('Are You Sure?')">Deactive</button>
</form>
</td>
</tr>
#endforeach

You using route model binding. Then you can use the Laravel delete()function.
public function destroy(User $user)
{
$user->delete();
return redirect('/dashboard/administrators')->with('success', 'Admin has been deleted');
}

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

laravel id value changed after adding a nested loop

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>

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

ErrorException (E_ERROR) rawurlencode() expects parameter 1 to be string, object given '

ErrorException (E_ERROR) rawurlencode() expects parameter 1 to be string, object given '
I am trying to display data from two tables and am getting the above error message, can somebody help translate this error message?
Here is the Controller
public function index()
{
$maintenances = DB::table('tenants')->select('tenants.lastname','tenants.firstname','maintenances.m_status','tenants.designation', 'maintenances.description','maintenances.building_section','maintenances.category','maintenances.reported_date')
->join('maintenances','maintenances.tenants_id','=','tenants.id')
->get();
//dd($maintenances);
return view('agent/maintenance_list', compact('maintenances', 'assetTenants', 'tenants'));
}
And view
#foreach($maintenances as $maintenance)
<tr>
<td class="text-center">
<div class="checkbox-custom">
<input id="product-01" type="checkbox" value="01">
<label for="product-01" class="pl-0"> </label>
</div>
</td>
<td>{{ $maintenance->designation }} {{ $maintenance->firstname }} {{ $maintenance->lastname }}</td>
<td>{{ $maintenance->category }}</td>
<td>{{ $maintenance->building_section }}</td>
<td>{{ $maintenance->description }}</td>
<td>{{ $maintenance->reported_date }}</td>
<td>{{ $maintenance->m_status }}</td>
<td class="text-center">
<div role="group" aria-label="Basic example" class="btn-group btn-group-sm">
<i class="ti-pencil"></i>
</div>
</td>
</tr>
#endforeach
With the route
Route::get('maintenance_list', 'MaintenanceController#index')->name('/maintenance_list');
This error message indicate that something is route with your url, in this case from the view. To solve this problem, this code
<i class="ti-pencil"></i>
as to change to
<i class="ti-pencil"></i>

Categories