Home > Software engineering >  SQLSTATE[23000]: Integrity constraint violation: 4025 CONSTRAINT `notes.file` failed for `classroom`
SQLSTATE[23000]: Integrity constraint violation: 4025 CONSTRAINT `notes.file` failed for `classroom`

Time:02-17

I am getting this error in laravel 8

SQLSTATE[23000]: Integrity constraint violation: 4025 CONSTRAINT notes.file failed for classroom.notes (SQL: insert into notes (classroom_id, user_id, title, description, file, status, code, updated_at, created_at) values (3, 3, tes, teset, G:\xampp\tmp\php87A.tmp|G:\xampp\tmp\php87B.tmp|G:\xampp\tmp\php88B.tmp|G:\xampp\tmp\php88C.tmp|notes/270584134_3084881908463864_7790143535087104689_n.jpg_d645920e395fedad7bbbed0eca3fe2e0.jpg|notes/271593123_1376415469456519_8005701171651680985_n.jpg_812b4ba287f5ee0bc9d43bbf5bbe87fb.jpg|notes/272092182_232722372382525_5526411493206144373_n.jpg_f457c545a9ded88f18ecee47145a72c0.jpg|notes/AAYUAQR3AAgAAQAAAAAAADzjqItE3P0yRQGwRg-HnEHXKQ.png_f457c545a9ded88f18ecee47145a72c0.png, 1, ZkF7yZhckH, 2022-02-16 09:40:08, 2022-02-16 09:40:08))

This is the form

                          <form method="POST" action="{{url('class/note/')}}" enctype="multipart/form-data">
                          {{ csrf_field() }}
                            <div >
                              <textarea id="title" name="title" ></textarea>
                              <label for="title">Note title</label>
                            </div>
                            <div >
                              <textarea id="desc" name="description" ></textarea>
                              <label for="desc">Note description</label>
                            </div>
                            <div >
                            <p>Attach Files *</p>
                            <input type="file" 
                            name="files[]" multiple />
                            </div>
                            <div >
                              <input type="hidden" name="cid" value="{{ $classroom->id }}">
                              <button onclick="submitBtn()" id="working"  type="submit" name="action">Submit
                                <i >send</i>
                              </button>
                            <script>
                              function submitBtn() {
                                document.getElementById('working').innerText = 'Please wait...';
                              };
                            </script>
                          </form>

This is the controller

    <?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use App\Models\classroom;
use App\Models\post;
use App\Models\comment;
use App\Models\note;
use Auth;
class noteController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }
    public function store(Request $request)
    {
        $this->validate($request,[
            'files' => 'required',
        ]);
        $note = new note;
        $user = Auth::user();
        $randoml = Str::random(10);
        $note->classroom_id = $request->input('cid');
        $note->user_id = $user->id;
        $note->title = $request->input('title');
        $note->description = $request->input('description');
        $files = array();
        if($files = $request->file('files')){
            foreach($files as $file){
                $filename = $file->getClientOriginalname().'_' . md5(rand(10, 100)) .'.'. $file->getClientOriginalExtension();
                $path = 'notes/';
                $url = $path.$filename;
                $file->move($path, $filename);
                $files[] = $url;
                $note->file = implode('|', $files);
            }
        }
        $note->status = 1;
        $note->code = $randoml;
        $note->save();
        return redirect()->back()->with('success', 'Note posted');
    }
}

CodePudding user response:

i think your file name length more than database column's valid length

CodePudding user response:

You are trying to store file on every iteration. you should store the paths in an array as you have done but only store the file after every iteration is performed and path is stored in your $files[] array but laravel has simple image upload api here you can check this

  • Related