Home > Software engineering >  Data isn't saved in Database
Data isn't saved in Database

Time:11-01

I am trying to save country name in database.

enter image description here After clicking the button this is showing the filed name is blank.

enter image description here

html:

<input type="text" class="form-control" name="country_name" placeholder="Country name">
                        @error('country_name')
                        <span class="text-danger">{{$message}}</span>
                        @enderror

Controller

public function store(Request $request)
   {
       $this->validate($request,[
        'name'=>'required|unique:divisions',
       ],
       [
           'name.unique' => 'Division Name Already Exists'
       ]);
       $divisions = new Division();
       $divisions->country_name = $request->country_name;
       $divisions->subcontinent_id = $request->subcontinent_id;
       $divisions->save();
        return back();
   }

Model

class Division extends Model
{
   protected $fillable = [
      'country_name', 'subcontinent_id'
  ];
   public function subcontinent()
   {
      return $this->belongsTo(Subcontinent::class);
   }

} 

I triple checked for typos and tried different html format. The data isn't saving. I am confused why this is happening. I am noob at eloquent.

CodePudding user response:

Modify your validation. As I cannot see name in your inputs, change name to country_name:

$this->validate($request,[
        'country_name'=>'required|unique:divisions',
       ],
       [
           'country_name.unique' => 'Division Name Already Exists'
       ]);

Your form has some errors, if you want to see the errors, you have multiple options, one of them is adding below code in your blade to display all errors.

@foreach ($errors->all() as $error)
    <div>{{$error}}</div>
@endforeach

CodePudding user response:

your input name is country_name not name so please change it where you are validating it

'country_name'=>'required|unique:divisions',

instead of

'name'=>'required|unique:divisions',
  • Related