Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Reusing a Controller method for multiple actions in Laravel - SimplestWeb (https://simplestweb.in/blog/reusing-a-controller-method-for-multiple-actions-in-laravel)
<div class="modal" id="modalEditUser" tabindex="-1" role="dialog" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="mb-0">Edit User - {{ $edit->name }}</h5>
</div>
<div class="modal-body">
<form method="POST" action="{{ route('users.update', ['users' => $edit->id]) }}">
@method("POST")
@csrf
{{-- Your usual form to edit user's detail --}}
</form>
</div>
</div>
</div>
</div>
@push('scripts')
<script>
window.addEventListener('DOMContentLoaded', function (e) {
$('#modalEditUser').modal('show');
});
</script>
@endpush
@extends('admin')
@section('content')
{{-- All your other markup to display users --}}
{{-- Below shows a way to add the parameter to the url --}}
<a href="{{ route('users.index', ['edit' => $user->id]) }}">Edit User</a>
{{-- The template the model is included only when the veriable is set --}}
@includeWhen($edit, 'modals.users.edit')
@endsection
class UsersController
{
// ......
public function index(Request $request) {
$edit = null;
// We check if the edit parameter is set, if so we find the user with that ID
// This $edit variable can be used in our template
if ($request->has('edit')) $edit = User::where('id', $request->query('edit'))->first();
$users = User::all();
return view('users.index', compact('users', 'edit'));
}
// ......
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.