Seeker1337
4 months ago
316
1
Laravel

Laravel 6+ CRUD update all text fields except image upload one

Posted 4 months ago by Seeker1337

Hello there. In short, here is my problem: I have a CRUD system for students information. It's taking a few text fields and an image upload field. All good so far unless we go to the update method. If i update any field without touching the image upload one, I get the following error: " SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'studimage' cannot be null".

I've tried setting the "studimage" field to NULL by default but this way if I update any text field, the query runs through, however the current image basically gets set to NULL and there is nothing to display. Any help would be appreciated.

StudController.php

use App\StudModel ;

public function store(Request $request)
    {
        
        $studmodel = new StudModel ;
        $studmodel->studnum = $request->studnum ;
        $studmodel->studname = $request->studname ;
        $studmodel->studf = $request->studf ;
        $studmodel->studsp = $request->studsp ;
        $studmodel->studimage = $request->studimage ;
        
        $file = $studmodel->studimage = $request->studimage ;
        $filename = $file->getClientOriginalName() ;
        $destinationPath = 'storage/stud-images' ;
        $studmodel->studimage = $filename; 
        $uploadSuccess = $file->move($destinationPath, $filename);
        
        $studmodel->save() ;
        
        return redirect('mypage')->with('success', 'etc..') ;
    }

    public function edit($id)
    {
        
        $studmodel = StudModel::findOrFail($id) ;
        
        return view('mypage-edit', compact('studmodel')) ;
        
    }

    public function update(Request $request, $id)
    {
        
        $studmodel = StudModel::findOrFail($id) ;
        
        $studmodel->update([
            
        $studmodel->studnum = $request->studnum ;
        $studmodel->studname = $request->studname ;
        $studmodel->studf = $request->studf ;
        $studmodel->studsp = $request->studsp ;
        $studmodel->studimage = $request->studimage ;
            
        ]) ;
        
        return redirect('mypage')->with('success', 'etc..') ;
        
    }

mypage-edit.blade.php

use App\StudModel ;

<h1> Edit a record </h1><br>
  
<form action="{{ route('mypage.update', $studmodel->id) }}" method="post">
    @CSRF
    @method('PUT')
  
    <div class="form-group">
    <label for="text">Unique number:</label>
    <input type="number" class="form-control" name="studnum"  value={{ $studmodel->studnum }} >
    </div>

    <div class="form-group">
    <label for="text">Name:</label>
    <input type="text" class="form-control" name="studname"  value={{ $studmodel->studname }} >
    </div>

    <div class="form-group">
    <label for="text">Faculty:</label>
    <input type="text" class="form-control" name="studf" value={{ $studmodel->studf }} >
    </div>

    <div class="form-group">
    <label for="text">Stud spec:</label>
    <input type="text" class="form-control" name="studsp" value={{ $studmodel->studsp}}>
    </div>

    <div class="form-group">
    <label for="pic">Stud image:</label><br>
    <input type="file" name="studimage" id="ImageToUpload" value={{ $studmodel->studimage }}>
    </div>

    <button type="submit" class="btn btn-primary"> Edit the record </button>
  </form>   

Please sign in or create an account to participate in this conversation.