Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

300
Views
Laravel Controller/Ajax not saving in my database

It seems like my save(); in my categories does not function as intended below. I will show the necessary codes first:

my table name is hms_bbr_category which is also connectec to my .env locally:

DB_CONNECTION=pgsql
DB_HOST=localhost
DB_PORT=5432
DB_DATABASE=jhs
DB_USERNAME=postgres
DB_PASSWORD=pa55wor0

my model: HmsBbrCategory

class HmsBbrCategory extends Model

{
    protected $table = 'hms_bbr_category';
    protected $fillable = [
        "category_name", "category_description"
    ];
}

my controller: BBRCategoryConfigurationController

class BBRCategoryConfigurationController extends Controller
{
    public function index(){
        return view('frontend.bbr-settings.bbr-category-configuration');
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'category_name'=>'required|max:191',
            'category_description'=>'required|max:191',
        ]);
        if($validator->fails())
        {
            return response()->json([
                'status'=>400,
                'errors'=>$validator->messages(),
            ]);
        }
        else {
            $category = new HmsBbrCategory;
            $category->category_name = $request->input('category_name');
            $category->category_description = $request->input('category_description');
            $category->save();
            return response()->json([
                'status'=>200,
                'message'=>'Category Added!',
               
            ]);
        }
        
    }

The ajax and modal fields

<div class="form-group">
<input type="text" class="form-control form-group w-100 category_name" placeholder="Category Name">
</div> 
<div class="form-group">
<textarea class="form-control w-100 category_description" placeholder="Category Description" cols="50" rows="10"></textarea>
</div>

<script>
    $(document).ready(function (){
        $(document).on('click', '.add_category', function(e){
            e.preventDefault();
            var category_data = {
                'category_name': $('.category_name').val(),
                'category_description': $('.category_description').val(),
            }
            
            //token taken from laravel documentation
            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });
            console.log(category_data);
            $.ajax({
                type: "POST",
                url: "/clinical/bbr-category-configuration",
                data: "category_data",
                dataType: "json",
                success: function (response){
                    // console.log(response);
                if(response.status == 400)
                    {
                    $('#saveform_errList').html("");
                    $('#saveform_errList').addClass('alert alert-danger');
                    $.each(response.errors, function (key, err_values) {
                        $('#saveform_errList').append('<li>'+err_values+'</li>');
                    });
                    }
                else 
                    {
                     $('#saveform_errList').html("");
                     $('#success_message').addClass('alert alert-success');
                     $('#success_message').text(response.message);
                     $.('#createCategory').modal('hide');
                     $.('#createCategory').find('input').val("");
                     console.log(category_data);
                    }
                }
            });
        });
    });
</script>

my routes at web.php

Route::get('/bbr-category-configuration', [BBRCategoryConfigurationController::class,'index']);
Route::post('/bbr-category-configuration', [BBRCategoryConfigurationController::class,'store']);

Things to note:

my hunch is that my store function does not connect properly at $category = new HmsBbrCategory; However I have checked that my table name and the fields taken are the same, as seen at $category->category_name = $request->input('category_name');

I have also tested in ajax with the values by simply adding console.log(response) as seen in the screenshot, I cannot get past my validator to get to the save(). I am not sure how but There should not be an error since my text fields are filled.

1

I can elaborate more if needed, I am asking what can I change to fix my validation/save. thanks for any help.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

As the error shows, The validation is failing (empty value i guess) and returning the code you programmed (400).

i'm guessing it is because you are using a string instead of the variable at the attribute data: "category_data",

update the code to send the variable instead

 $.ajax({
                type: "POST",
                url: "/clinical/bbr-category-configuration",
                data: category_data, //change here
                dataType: "json",
                success: function (response){
//...
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!