php – Data does not update in Laravel-ThrowExceptions

Exception or error:

I have checked the attributes and all of it matches correctly. I have used this method of updating data in a different Controller and it works fine. It is very weird that only this set of data does not seem to be updated upon running the code. Also, there are no errors popping out, so i don’t exactly know how to troubleshoot.

Code in “HawkerInfoController”:

public function update(Request $request)
    {
        $request->validate([
            'fname' => ['required'],
            'fid' => ['required'],
            'job' => ['required'],
            'marital' => ['required'],
            'income' => ['required'],
        ]);

        $hawkerAdd = HawkerAdd::find(Auth::user()->user_id);

        $hawkerAdd->hawker_id = Auth::user()->user_id;
        $hawkerAdd->job = $request->input('job');
        $hawkerAdd->marital = $request->input('marital');
        $hawkerAdd->income = $request->input('income');
        $hawkerAdd->picture = $request->input('picture');
        $hawkerAdd->utility_bil = $request->input('bil');
        $hawkerAdd->ty2_inject = $request->input('inject');
        $hawkerAdd->father_name = $request->input('fname');
        $hawkerAdd->father_id = $request->input('fid');

        $hawkerAdd->save();

        return redirect()->route('hawkerInfo.index')->with('success',"Additional Information has been updated");
    }

Code in “hawker_info.blade”:

<form method="POST" action="{{ route('hawkerInfo.update',Auth::user()->user_id) }}">
  @csrf
  @method('PUT')
    <div class="form-group mb-4">
      <strong><label for="fname">Father's Name</label></strong>
      <input type="text" class="form-control" id="fname" name="fname" value="{{$hawkeradd->father_name}}" placeholder="Name" readonly>
    </div>
    <div class="form-group mb-4">
      <strong><label for="fid">Father's Identification Card</label></strong>
      <input type="text" class="form-control" id="fid" name="fid" value="{{$hawkeradd->father_id}}" placeholder="Identification Card" readonly>
     </div>
     <div class="form-group mb-4">
       <strong><label for="job">Occupation</label></strong>
       <input type="text" class="form-control" id="job" name="job" value="{{$hawkeradd->job}}" placeholder="Your Occupation">
     </div>
     <div class="form-group mb-4">
       <strong><label>Marital Status</label><br></strong>
       <div class="custom-control custom-radio custom-control-inline">
         <input type="radio" id="marit1" name="marital" class="custom-control-input" value="Single" {{$hawkeradd->marital == 'Single' ? 'checked' : '' }}>
         <label class="custom-control-label" for="marit1">Single</label>
     </div>
     <div class="custom-control custom-radio custom-control-inline">
       <input type="radio" id="marit2" name="marital" class="custom-control-input" value="Married" {{$hawkeradd->marital == 'Married' ? 'checked' : '' }}>
       <label class="custom-control-label" for="marit2">Married</label>
     </div>
     <div class="custom-control custom-radio custom-control-inline">
       <input type="radio" id="marit3" name="marital" class="custom-control-input" value="Divorced" {{$hawkeradd->marital == 'Divorced' ? 'checked' : '' }}>
       <label class="custom-control-label" for="marit3">Divorced</label>
     </div>
     <div class="custom-control custom-radio custom-control-inline">
       <input type="radio" id="marit4" name="marital" class="custom-control-input" value="Widow" {{$hawkeradd->marital == 'Widow' ? 'checked' : '' }}>
       <label class="custom-control-label" for="marit4">Widow</label>
     </div>
     </div>
     <div class="form-group mb-4">
       <strong><label for="income">Household Income</label></strong>
       <select id="income" name="income" class="form-control" readonly>
         <option value="Below 1,500" {{$hawkeradd->income == 'Below 1,500' ? 'selected' : '' }}  {{$hawker->income != 'Below 1,500' ? 'disabled' : '' }}>Below 1,500</option>
         <option value="1501 - 2500" {{$hawkeradd->income == '1501 - 2500' ? 'selected' : '' }}  {{$hawker->income != '1501 - 2500' ? 'disabled' : '' }}>1501 - 2500</option>
         <option value="2501 - 3500" {{$hawkeradd->income == '2501 - 3500' ? 'selected' : '' }}  {{$hawker->income != '2501 - 3500' ? 'disabled' : '' }}>2501 - 3500</option>
         <option value="Above 3,501" {{$hawkeradd->income == 'Above 3,501' ? 'selected' : '' }}  {{$hawker->income != 'Above 3,501' ? 'disabled' : '' }}>Above 3,501</option>
       </select>
     </div>
     <button type="submit" class="btn btn-primary pull-right">Update</button>
  </form>

Table structure:

    Schema::create('hawker_adds', function (Blueprint $table) {
        $table->string('hawker_id', 12);
        $table->string('job', 100);
        $table->string('marital', 20);
        $table->string('income', 20);
        $table->string('picture', 50)->nullable();
        $table->string('utility_bil', 50)->nullable();
        $table->string('ty2_inject', 50)->nullable();
        $table->string('father_name', 100);
        $table->string('father_id', 12);

        $table->primary('hawker_id');
        $table->foreign('hawker_id')->references('hawker_id')->on('hawkers')->onDelete('cascade')->onUpdate('cascade');
    });

HawkerAdd Model:

protected $primaryKey = "hawker_id";
    public $incrementing = false;
    protected $keyType = 'string';
    public $timestamps = false;

    protected $fillable = [
        'picture', 'job', 'marital', 'income', 'utility_bil', 'ty2_inject', 'father_name', 'father_id',
    ];
How to solve:

To get the update error when $hawkerAdd->save();, you can try to catch the exception like this:

try{
   $hawkerAdd = HawkerAdd::findOrFail(Auth::user()->user_id);

    $hawkerAdd->hawker_id = Auth::user()->user_id;
    $hawkerAdd->job = $request->input('job');
    $hawkerAdd->marital = $request->input('marital');
    $hawkerAdd->income = $request->input('income');
    $hawkerAdd->picture = $request->input('picture');
    $hawkerAdd->utility_bil = $request->input('bil');
    $hawkerAdd->ty2_inject = $request->input('inject');
    $hawkerAdd->father_name = $request->input('fname');
    $hawkerAdd->father_id = $request->input('fid');

    $hawkerAdd->save();
}
catch(\Exception $e){
   // do task when error
   echo $e->getMessage();   // update query
}

Hope this helps 🙂

Answer:

Try to name your formfields exactly as the construction of your table. As you haven’t done this, we have to work around that. What you try to do is to save an entry that already exists. You want to update.

A bit weird is that your hawker_id has exactly the same value as the table id. Unless you have renamed the added protected $primaryKey = 'hawker_id'; in your HawkerAdd model.

public function update(Request $request)
    {
        $request->validate([
            'fname' => ['required'],
            'fid' => ['required'],
            'job' => ['required'],
            'marital' => ['required'],
            'income' => ['required'],
        ]);

        $hawkerAdd = HawkerAdd::find(Auth::user()->user_id);

        $hawkerAdd->update([
            'hawker_id' => Auth::user()->user_id,
            'job' => $request->input('job'),
            'marital' => $request->input('marital'),
            'income' => $request->input('income'),
            'picture' => $request->input('picture'),
            'utility_bil' => $request->input('bil')
            'ty2_inject' => $request->input('inject'),
            'father_name' => $request->input('fname'),
            'father_id' => $request->input('fid'),
        ]);

        return redirect()->route('hawkerInfo.index')->with('success',"Additional Information has been updated");
    }

If you call your form input fields exactly like your table fields, you could simply do this:

$hawkerAdd = HawkerAdd::find(Auth::user()->user_id);
$hawkerAdd->update($request->all());

and that would it be.

Leave a Reply

Your email address will not be published. Required fields are marked *