Adding profile data - crud of profiles

Step 01: Adding profile add button just above of table element in list of profiles

<h2 class="float-start">All Profiles</h2>
<div class="float-end">
	<a class="btn btn-sm btn-primary" href="{{ route('profiles.create') }}"> Add Profile</a>
</div>  

Step 02: In ProfileController, adding create() method

public function create( ) {
	$meta_title = 'Add Profile';
	return view( 'profiles.create', compact( 'meta_title' ) );
} 

Step 03: Adding create.blade.php under resources/view/profiles folder and putting following snippet inside blade section directive extending app layout

<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <h2 class="float-start">Add 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.store') }}" id="updatefrm" method="post"> 
                    @csrf 
                    <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="ProductName" value="{{ old('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="Add">
                    </form>
                </div>
            </div>
    </div>
</div>
</div>
</div> 

Step 04: Creating ProfileRequest.php using artisan command for validation of form fields

php artisan make:request ProfileRequest

Step 05: Adding validation rules like below in ProfileRequest.php making sure authorize function returns true

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ProfileRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [                         
            'name' => 'required'             
        ];
    }
}

Step 06: Changing Profile.php model like below adding fillable with saving fields

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Profile extends Model
{
    use HasFactory;

    protected $fillable = [
        'name', 'sequence'
    ];     
}
?>

Step 07: Back in ProfileController adding store() method like below and redirecting to list page after addition

//at the top 
use App\Http\Requests\ProfileRequest;
use App\Models\Profile;

public function store( ProfileRequest $request ) {
	Profile::create([
		'name' => $request->name,
		'sequence' => ( Profile::latest('sequence')->first()->id + 1 ) // fetching last sequence value from table and adding one to it
	]);     
	
	return redirect()->route('profiles.index');
} 

Now if we fillup the create form and submit it, information with all its fields will be saved to table in the database.

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