Skip to content
Advertisement

All the migrations are working inappropriately

This is the error I get after trying to open the form. Below are the codes. It takes me all day so please kindly help me out.

SQLSTATE[42S22]: Column not found: 1054 Unknown column ‘id’ in ‘field list’ (SQL: select max(id) as aggregate from admissions where admissions.deleted_at is null)

         $admissions = Admission::all();
            $admissions = Admission::max('id');
            $roll_id = Roll::max('roll_id');
            $faculties = Faculty::all();
            $departments = Department::all();
            $batches = Batch::all();
            $rand_username_password = mt_rand(111609300011 .$student_id, 111609300011 .$student_id );
    
            return view('admissions.index', compact('student_id', 
           'admissions', 'student_id', 'faculties', 'departments', 'batches', 'roll_id', 'rand_username_password'));
                
        }
    
        /**
         * Show the form for creating a new Admission.
         *
         * @return Response
         */
        public function create()
        {
            return view('admissions.create');
        }
    
        /**
         * Store a newly created Admission in storage.
         *
         * @param CreateAdmissionRequest $request`enter code here`
         *
         * @return Response
         */
        public function store(CreateAdmissionRequest $request)
        {
            $input = $request->all();
    
            $file = $request->file('image');
            $extension = $file->getClientOriginalExtension();
            $new_image_name = time(). '.' .$extension;
            $file->move(public_path('student_images'), $new_image_name);
    
    
    
            $student = new Admission;
            $student->first_name = $request->first_name;
            $student->last_name = $request->last_name;
            $student->father_name = $request->father_name;
            $student->father_phone = $request->father_phone;
            $student->mother_name = $request->mother_name;
            $student->gender = $request->gender;
            $student->phone = $request->phone;
            $student->dob = $request->dob;
            $student->email = $request->email;
            $student->status = $request->status;
            $student->nationality = $request->nationality;
            $student->passport = $request->passport;
            $student->address = $request->address;
            $student->current_address = $request->current_address;
            $student->department_id = $request->department_id;
            $student->faculty_id = $request->faculty_id;
            $student->dateregistered = date('Y-m-d');
            $student->batch_id = $request->batch_id;
            $student->user_id = Auth::id();
            $student->image = $new_image_name;
    
             //conditions to check if insert, proceed to next level
    
            if($student->save()){
                $student_id = $student->id;
                $username = $student->username;
                $password = $student->password;
    
                Roll::insert(['student_id' => $student_id, 'username' => 
                $request->username, 'password' => $request->password ]);

Here is the migration

Schema::create('admissions', function (Blueprint $table) {
   $table->bigIncrements('student_id');
   $table->string('roll_no');
   $table->string('last_name');
   $table->string('father_name');
   $table->string('father_phone');
   $table->string('mother_name');
   $table->string('gender');
   $table->string('email')->unique();
   $table->date('dob');
   $table->string('phone');
   $table->longText('address');
   $table->longText('current_address');
   $table->string('nationality');
   $table->string('passport');
   $table->tinyInteger('status');
   $table->date('dateregistered');
   $table->integer('user_id');
   $table->integer('class_id');
   $table->string('image')->nullable();
   $table->softDeletes();
   $table->timestamps();
}

Advertisement

Answer

Schema::create('admissions', function (Blueprint $table) {
                $table->bigIncrements('student_id');
         ....

This represents that your primary key of this table is student_id not id.

Replace 
$admissions = Admission::max('id');

to 
$student_id = Admission::max('student_id');
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement