Editing and deleting profile data - crud of profiles

Step 01: In ProfileController, adding edit() method

public function edit( Profile $profile ) {
	$meta_title = 'Edit Profile ' . $profile->name;
	return view( 'products.edit', compact( 'meta_title', 'profile' ) );
}

Step 02: Adding edit.blade.php under resources/view/profiles folder inside section blade directive extending app layout

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
        <h2 class="float-start">Edit Profile</h2>
        <div class="float-end">
            <a class="btn btn-sm btn-primary" href="{{ route('profiles.index') }}"> Back to profiles</a>
        </div>
        <div class="clearfix"></div>
        <div class="card ">
            <div class="card-body">
            <form method="post" action="{{ route('profiles.update', $profile->id) }}" id="updatefrm"> 
                @csrf
                @method('PUT')
                <div class="form-group mb-2">
                    <label for="title">Name</label>
                    <br>
                    <input name="name" class="form-control" placeholder="profile name" title="Profile Name" type="text" id="ProfileName" value="{{ $profile->name }}" /> 
                    @if ($errors->has('name')) 
                        <span class="text-danger">{{ $errors->first('name') }}</span> 
                    @endif
                </div>           
                <input class="btn btn-sm btn-primary" class="form-control" type="submit" name="action" value="Edit">
            </form>
            </div>
        </div>
        </div>
    </div>
    </div>
</div>    

Step 03: Back in ProfileController adding update() method like below and keeping product to edit page after editing with flash message at the top

public function update( Profile $profile, ProfileRequest $request ) {

	$data = [
		'name' => $request->name,
	];

	$profile->update($data);
	
	return redirect()->route('profiles.edit',$profile->id)->with('success', 'Profile edited successfully');
}  

Please note we have kept ProfileRequest as paramater for update() method like it was in store() method so that same validation rule applies on both of the methods.

Step 04: Deleting profile data

    public function destroy( Profile $profile ) {
        $profile->delete();
        return redirect()->route( 'profiles.index' )->with( 'success', 'Profile deleted successfully' );
    } 

Related Posts


Building mini ecommerce in Laravel

Listing rows of users - crud

Adding user data - crud of users

Editing user data - crud of users

Deleting user data - crud of users

Listing rows of products - crud

Listing rows of profiles - crud

Listing rows of orders - crud

Listing rows of order items - crud