Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
127 views
in Technique[技术] by (71.8m points)

php - i have a problem with storing data into database, anyone can help me?

below the code to make input form.

<!--Form for lecturer-->
          @if (Auth::user() && Auth::user()->role == 'lecturer')
          <form action="/registerLecturerPersonalData" method="POST" enctype="multipart/form-data">
            {{csrf_field()}}
            @if (session('status'))
            <div class="alert alert-success">
                <strong>{{ session('status') }}</strong>
            </div>
            @endif
            <div class="personal-form">
              <div class="personal-margin-row row">
                <label class="col-sm-2 personal-margin-label">Lecturer ID</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" name="lecid" id="lectid">
                </div>
              </div>

                <div class="personal-margin-row row">
                  <label class="col-sm-2 personal-margin-label">Faculty Lecturing</label>
                  <div class="col-sm-8">
                    <select name="facultylecturing" id="facultylecturing" class="col-sm-12 form-control">
                      <option selected disabled>Choose...</option>
                      <option value="FITE">Faculty of Informatics and Electrical Engineering</option>
                      <option value="FTI">Faculty of Industrial Engineering</option>
                      <option value="FTB">Faculty of Biotechnology</option>
                    </select>
                  </div>
                </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Major Lecturing</label>
                <div class="col-sm-8">
                  <select name="majorlecturing" id="majorlecturing" class="col-sm-12 form-control">
                    <option selected disabled>Choose...</option>
                    <option value="Informatics Engineering">Informatics Engineering</option>
                    <option value="Information System">Information System</option>
                    <option value="Electrical Engineering">Electrical Engineering</option>
                    <option value="Engineering Management">Engineering Management</option>
                    <option value="Bioprocess Engineering">Bioprocess Engineering</option>
                    <option value="Information System">Information System</option>
                  </select>
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Bachelor Campus</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="bachelorcampus" name="bachelorcampus">
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Master Campus</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="mastercampus" name="mastercampus">
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Phd Campus</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="phdcampus" name="phdcampus">
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Interest Field</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="interest" name="interest">
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Date Start</label>
                <div class="col-sm-8">
                  <input type="date" class="form-control" id="datestart" name="datestart">
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Gender</label>
                <div class="col-sm-8">
                  <select id="gender" name="gender" class="col-sm-4 form-control">
                    <option selected disabled>Choose...</option>
                    <option value="laki-laki">Male</option>
                    <option value="perempuan">Female</option>
                  </select>
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Place of birth</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="placeofbirth" name="placeofbirth">
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Date of Birth</label>
                <div class="col-sm-8">
                  <input type="date" class="form-control" id="dateofbirth" name="dateofbirth">
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Address</label>
                <div class="col-sm-8">
                  <input type="text" class="form-control" id="address" name="address">
                </div>
              </div>


              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Phone Number</label>
                <div class="col-sm-8">
                  <input type="tel" class="form-control" id="phonenumber" name="phonenumber" pattern="[0-9]{4}-[0-9]{4}-[0-9]{4}" placeholder="ex : 0888-9899-7877">
                </div>
              </div>

              <div class="personal-margin-row  row">
                <label class="col-sm-2 personal-margin-label">Photo</label>
                <div class="col-sm-8">
                  <div class="custom-file">
                    <input type="file" class="form-control custom-file-input" id="customFile" name="photo">
                    <label class="custom-file-label" for="customFile">Choose file</label>
                  </div>
                </div>
              </div>

            </div> 
            <div class="submit-container container">
              <input type="submit" placeholder="submit" class="btn btn-info" name="submit" id="submit" style="width: 100px;">                                         
            </div> 
          </form>
        @endif

below the model, I used to define every tuple in this part, so it can be easily process

class lecturer_model extends Model
{
    protected $table = 'lecturer_data';
    public $timestamps = true;
    protected $fillable = [
        'lecturer_id','id_user_lec','faculty_lecturing','major_lecturing','bachelor_campus','master_campus','phd_major',
        'interest_field','start_date','gender','place_of_birth','date_of_birth','address','phone_number','profile_image'
    ];
}

Below the controller, contains the rule and exceptions handling used to store data

public function storeLecturerPersonalData(Request $request){        
        $rules = [
            'lectid' => 'required|min:2|max:10',
            'facultylec' => 'required|max:255',
            'majorlec' => 'required|max:255',
            'bachelorcamp' => 'required|max:255',
            'mastercamp' =>  'required|max:255',
            'phdcamp' =>  'required|max:255',
            'startdate' => 'required',
            'interest' => 'required|max:255',
            'gender' => 'required|max:255',
            'placeofbirth' => 'required|max:255',
            'dateofbirth' => 'required',
            'phonenumber' => 'required',
            'address' => 'required|min:2|max:255',
            'photo' => 'required|mimes:jpeg,jpg,png,gif,svg'
        ];
        $validator = Validator::make($request->all(),$rules);
        if ($validator->fails()) {
            return redirect('/home')
            ->withInput()
            ->withErrors($validator);
        }
        else{
            $request_input = $request->input();
            $photoName = $request->photo->getClientOriginalName() . '-lecturer-' . time()
            . '.' . $request->photo->extension();
    
            $request->photo->move(public_path('user_image'),$photoName);
            try{
                $data = new lecturer_model;
                $data->lecturer_id = $request_input['lectid'];
                $data->id_user_lec = Auth::user()->id;
                $data->faculty_lecturing = $request_input['facultylec'];
                $data->major_lecturing = $request_input['majorlec'];
                $data->bachelor_campus = $request_input['bachelorcamp'];
                $data->master_campus = $request_input['mastercamp'];
                $data->phd_major = $request_input['phdcamp'];
                $data->interest_field = $request_input['startdate'];
                $data->start_date = $request_input['interest'];
                $data->gender = $request_input['gender'];
                $data->place_of_birth =$request_input['placeofbirth'];
                $data->date_of_birth = $request_input['dateofbirth'];
                $data->address = $request_input['address'];
                $data->phone_number = $request_input['phonenumber'];
                $data->profile_image = $photoName;
                return red

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should call the save() method, after you made the object, to store it into the database.

like this:$data->save();

So you can have :

            $data = new lecturer_model;
            $data->lecturer_id = $request_input['lectid'];
            $data->id_user_lec = Auth::user()->id;
            $data->faculty_lecturing = $request_input['facultylec'];
            $data->major_lecturing = $request_input['majorlec'];
            $data->bachelor_campus = $request_input['bachelorcamp'];
            $data->master_campus = $request_input['mastercamp'];
            $data->phd_major = $request_input['phdcamp'];
            $data->interest_field = $request_input['startdate'];
            $data->start_date = $request_input['interest'];
            $data->gender = $request_input['gender'];
            $data->place_of_birth =$request_input['placeofbirth'];
            $data->date_of_birth = $request_input['dateofbirth'];
            $data->address = $request_input['address'];
            $data->phone_number = $request_input['phonenumber'];
            $data->profile_image = $photoName;

            $data->save();

            return redirect('/home')->with('status',"Insert successfully");

Notice that it's recommended to use Capital letters for the first character of the class name. And it's better to use CamelCase instead of snack_case

like: class Lecturer extends Model


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...