Implement the Work Experience form | CV create Web | Code Abe Academy

This tutorial will teach you how to make user work experience form in CV create Web with validation using Laravel. Let’s do it step by step.




Work Experience Form

Now, Let's create. 

  1. Create work-experience.blade.php file.
  2. Add bootstrap.
  3. Add form-container.
  4. Form container 
    <section class="min-vh-100 d-flex flex-column align-items-center justify-content-center py-4">
            <div class="container-fluid  ">
                <div class="row justify-content-md-center">
                    <div class="col-md-10 ">
                        <div class="card px-5 minHeight py-3 mt-3 shadow">
                            <h1 class="text-info text-center mt-2 mb-4">Work Experience</h1>
    
                    </div>
                </div>
            </div>
        </div>
        </section>
    
  5. Add form.
  6. work-experience.blade.php 
    <form class="needs-validation" action="{{route('saveWorkExperience')}}" method="POST" novalidate>
                            @csrf
                            <div class="row">
    
                                <table class="table table-bordered" id="table">
                                    <thead>
                                        <tr>
                                            <th>Designation</th>
                                            <th>Company Name</th>
                                            <th>Description </th>
                                            <th>Started Date</th>
                                            <th>End Date</th>
                                            <th>Action</th>
                                        </tr>
                                    </thead>
                                    <tbody id="tableBody">
                                        
                                        <tr>
                                            <td><input type="text" name="data[0][designation]" class="form-control"></td>
                                            <td><input type="text" name="data[0][company_name]" class="form-control">
                                            </td>
                                            <td><textarea type="text" name="data[0][description]" class="form-control" rows="3"></textarea>
                                            </td>
                                            <td><input type="date" name="data[0][started_date]" class="form-control">
                                            </td>
                                            <td><input type="date" name="data[0][end_date]" class="form-control"></td>
                                            <td><button type="button" name="add" id="add"
                                                    class="btn btn-success">Add Row </button>
                                            </td>
    
                                        </tr>
                                    </tbody>
                                </table>
    
                            </div>
    
                            <div class="mb-2">
                                <div class="d-grid">
                                    <button type="submit" class="btn btn-primary float-right">Next&gt;</button>
                                </div>
                            </div>
                        </form>
    
  7. Add Table Script
  8. work-experience.blade.php
    <script>
        var i = 0;
        $('#add').click(function() {
            ++i;
            $('#table').append(
                `<tr>
                    <td>
                        <input type="text" name="data[` + i + `][designation]" class="form-control"/>
                    </td>
                    <td>
                        <input type="text" name="data[` + i + `][company_name]" class="form-control"/>
                    </td>
                    <td>
                        <textarea type="text" name="data[` + i + `][description]" class="form-control" rows="3"></textarea>
                    </td>
                    <td>
                        <input type="date" name="data[` + i + `][started_date]" class="form-control"/>
                    </td>
                    <td>
                        <input type="date" name="data[` + i + `][end_date]" class="form-control"/>
                    </td>
                    <td>
                        <button type="button" class="btn btn-danger remove-table-row">Remove</button>
                    </td>
                </tr>`);
        });
    
        $(document).on('click', '.remove-table-row', function() {
            $(this).parents('tr').remove();
        });
    </script>
  9. Add Form cloudflare Script
  10. work-experience.blade.php
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"
            integrity="sha512-894YE6QWD5I59HgZOGReFYm4dnWc1Qt5NtvYSaNcOP+u1T9qYdvdihz0PPSiiqn/+/3e7Jo4EaG7TubfWGUrMQ=="
            crossorigin="anonymous" referrerpolicy="no-referrer"></script>
  11. Create a controller.
  12. WorkExperienceController
    php artisan make:controller WorkExperienceController
  13. Create a route.
  14. Web.php
    uuse App\Http\Controllers\WorkExperienceController;
    
    Route::get('/work-experience',[WorkExperienceController::class,'work_experience'])->name('work-experience');
    
  15. Create Work Experience view function.
  16. WorkExperienceController
    public function work_experience()  {
            return view('work-experience');
        }
    
  17. Make WorkExperience Model and Table
  18. WorkExperience Model and WorkExperiences Table
    php artisan make:model WorkExperience -m
  19. Create the WorkExperiencelsTable
  20. WorkExperiencesTable
    Schema::create('work_experiences', function (Blueprint $table) {
                $table->id();
                $table->string('designation')->nullable();
                $table->string('company_name')->nullable();
                $table->string('description')->nullable();
                $table->string('started_date')->nullable();
                $table->string('end_date')->nullable();
                $table->timestamps();
            });
  21. Create the WorkExperience Model
  22. WorkExperience Model
    protected $table = 'work_experiences';
        protected $primarykey = 'id';
        protected $fillable = ['designation','company_name','description','started_date','end_date'];
    
  23. Add ForeignKey
  24. WorkExperiences Table
    $table->unsignedBigInteger('user_id');
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    
    WorkExperience Model
    protected $fillable = ['user_id'];
          
          public function users(){
            return $this->belongsTo(User::class);
        }
    
    User Model
    public function work_experiences()
        {
            return $this->hasMany(WorkExperience::class, 'user_id','id');
        }
    
  25. Migrate the tables
  26. Migrate Tables
    php artisan migrate
    
  27. Add saveWorkExperience route.
  28. Web.php
    use App\Http\Controllers\WorkExperienceController;
    
    Route::post('/saveWorkExperience',[WorkExperienceController::class,'saveWorkExperience'])->name('saveWorkExperience');
    
  29. Create saveWorkExperience function.
  30. WorkExperienceController
    use App\Models\WorkExperience;
          use Illuminate\Support\Facades\Validator;
    
    public function saveWorkExperience(Request $request)
        { {
                $rules = [
                    'data.*.designation' => 'nullable|string',
                    'data.*.company_name' => 'nullable|string',
                    'data.*.description' => 'nullable|string',
                    'data.*.started_date' => 'nullable|string',
                    'data.*.end_date' => 'nullable|string'
                ];
    
                $validator = Validator::make($request->all(), $rules);
    
                if ($validator->fails()) {
                    return redirect()->back()->withErrors($validator)->withInput();
                }
            }
    
            if (auth()->check()) {
    
                $userId = auth()->id();
    
                if (!$userId) {
    
                    return 'Error: User ID is null';
                }
    
                foreach ($request->data as $key => $value) {
                    $work_experience = new WorkExperience();
                    $work_experience->user_id = $userId;
                    $work_experience->designation = $value['designation'];
                    $work_experience->company_name = $value['company_name'];
                    $work_experience->description = $value['description'];
                    $work_experience->started_date = $value['started_date'];
                    $work_experience->end_date = $value['end_date'];
                    $work_experience->save();
                }
    
    
              return 'success';
    
            } else {
    
                return redirect()->route('login');
            }
        }
    

Post a Comment

Previous Post Next Post