0

I am using controller store method to save data, I have the model,views and controllers in place. i m using Laravel resource routing and intend to use the controller to perform CRUD operation. However, nothing seems to work.

This is the Controller save method

public function store(Request $request)
{
    //

    $request->validate([
        'adm'=>'required|unique:student',  
        'fullname'=>'required|alpha', 
        'dept'=>'required|alpha', 
        'course'=>'required|alpha',  
        'level'=>'required|alpha',  
        'feyear'=>'required|numeric', 
        'feser'=>'required|alpha', 
        'idnum'=>'unique:student|required|numeric|max:8', 
        'current_address'=>'required',  
        'permanent_address'=>'required',
        'email'=>'unique:student|required|email',
        'mobile'=>'unique:student|required|numeric', 
        'occupation'=>'nullable', 
        'occupation_place'=>'nullable',
        'otherphone'=>'nullable:numeric|max:13', 
        'nextofkin'=>'nullable|alpha', 
        'nextofkinadd'=>'required', 
        'nextofkinphone'=>'required|numeric', 
        'placeofworkadd'=>'nullable', 
        'supervisoradd'=>'nullable'
    ]);


    $std = new Student([

       'adm'=> $request->get('adm'),  
       'fullname'=> $request->get('fullname'), 
       'dept'=> $request->get('dept'), 
       'course'=> $request->get('course'),  
       'level'=> $request->get('level'),  
       'feyear'=> $request->get('feyear'), 
       'feser'=> $request->get('feser'), 
       'idnum'=> $request->get('idnum'), 
       'current_address'=> $request->get('current_address'),  
       'permanent_address'=> $request->get('permanent_address'),
       'email'=> $request->get('email'),
       'mobile'=> $request->get('mobile'), 
       'occupation'=> $request->get('occupation'), 
       'occupation_place'=> $request->get('occupation_place'),
       'otherphone'=> $request->get('otherphone'), 
       'nextofkin'=> $request->get('nextofkin'), 
       'nextofkinadd'=> $request->get('nextofkinadd'), 
       'nextofkinphone'=> $request->get('nextofkinphone'), 
       'placeofworkadd'=> $request->get('placeofworkadd'), 
       'supervisoradd'=> $request->get('supervisoradd') 
   ]);

    $std->save();
    return redirect('/')->with('success', 'You have been added as an Student');
}

My Model

class Student extends Model
{
    protected $fillable = [
        'adm',  
        'fullname', 
        'dept', 
        'course',  
        'level',  
        'feyear', 
        'feser', 
        'idnum', 
        'current_address',  
        'permanent_address',
        'email',
        'mobile', 
        'occupation', 
        'occupation_place',
        'otherphone', 
        'nextofkin', 
        'nextofkinadd', 
        'nextofkinphone', 
        'placeofworkadd', 
        'supervisoradd'
    ];
}

My form

<form name="frmRegistration" method="post" action="{{ route('student.store') }}">
    @csrf
    <br> <p class="text-white">Fill this form if you have not yet Registered as student</p>
    <div class="form-group">
        <label class="text-white" for="inputEmail">Enter Full Names</label>
        <input type="text" class="form-control" name="fullname" id="inputName" placeholder="e.g. Miss. Jane Doe">
    </div>
    <div class="form-group">
        <label class="text-white" for="inputEmail">Enter National ID/Passport Number</label>
        <input type="text" class="form-control" name="idnum" id="inputID" placeholder="e.g. 21436587">
    </div>
    <div class="form-group">
        <label class="text-white" for="no">Current Mobile Phone Number</label>
        <input type="text" class="form-control" name="mobile" id="inputphone" placeholder="e.g. 0734567890">
    </div>
    <div class="form-group">
        <label class="text-white" for="otherno">Other Mobile Phone Number</label>
        <input type="text" class="form-control" name="othermobile" id="altmobile" placeholder="e.g. 0734567890">
    </div>
    <div class="form-group">
        <label class="text-white" for="inputEmail">Enter email Address</label>
        <input type="email" class="form-control" name="email" id="inputName" placeholder="e.g. [email protected] ">
    </div>
    <div class="form-group">
        <label class="text-white" for="inputEmail">Enter Current Address</label>
        <input type="text" class="form-control" name="current_address" id="inputName" placeholder="e.g. P.O. Box 123456789 -00200- Nairobi ">
    </div>
    <div class="form-group">
        <label class="text-white" for="inputEmail">Enter Permanent Address (can be similar to the above) </label>
        <input type="text" class="form-control" name="permanent_address" id="inputName" placeholder="e.g. P.O. Box 123456 -30108- Timboroa ">
    </div>
    <div class="form-group">
        <label class="text-white" for="admissionnumber">Next of Kin</label>
        <input type="text" class="form-control" id="nextofkin" name ="nextofkin" placeholder="e.g. Jane Doe">
    </div>
    <div class="form-group">
        <label class="text-white" for="admissionnumber">Next of Kin Address</label>
        <input type="text" class="form-control" id="nextofkinadd" name ="nextofkinadd" placeholder="e.g. P.O. Box 234 -00100- Nairobi">
    </div>
    <div class="form-group">
        <label class="text-white" for="admissionnumber">Next of Kin Mobile Phone</label>
        <input type="text" class="form-control" id="nextofkinphone" name ="nextofkinphone" placeholder="e.g. 0734567890">
    </div>
    <div class="form-group">
        <label class="text-white" for="admissionnumber">Current Occupation (if None use N/A) </label>
        <input type="text" class="form-control" id="inputAdmissionnumber" name ="occupation" placeholder="e.g. Network Administrator">
    </div>

    <div class="form-group">
        <label class="text-white" for="admissionnumber">Place of Current Occupation ( if None use N/A)</label>
        <input type="text" class="form-control" id="inputAdmissionnumber" name ="occupation_place" placeholder="e.g. Nairobi">
    </div>
    <div class="form-group">
        <label class="text-white" for="inputsupname">Supervisor Telephone Number ( if None use N/A)</label>
        <input type="text" class="form-control" name="supervisoradd" id="supphone" placeholder="e.g. 0734567890">
    </div>
    <div class="form-group">
        <label class="text-white" for="inputsupname">Address of Current Place of Work ( if None use N/A)</label>
        <input type="text" class="form-control" name="placeofworkadd" id="occuadd" placeholder="e.g. Rift Valley Technical Training Institute, P.O. Box 244 -30100-,Eldoret">
    </div>
    <div class="form-group">
        <label class="text-white" for="admissionnumber">Your Student Registration Number</label>
        <input type="text" class="form-control" id="inputAdmissionnumber" name ="adm" placeholder="e.g. 117R0008">
    </div>
    <div class="form-group">
        <label class="text-white" for="department">Select your Course's Department</label>
        <select class="form-control" id="dept" name="dept">

            <option>Automotive Engineering</option>
            <option>Building & Civil Engineering</option>
            <option>Business and Development Studies</option>
            <option>Electrical & Electronic Engineering</option>
            <option>Hospitality & Dietetics Management</option>
            <option>Information Communication Technology</option>
            <option>Mechanical and Automotive Engineering</option>
            <option>Pharmacy & Chemical Science</option>
            <option>Medical & Biological Sciences</option>

        </select>
    </div>
    <div class="form-group">
        <label class="text-white" for="admissionnumber">Course (in full as per registration)</label>
        <input type="text" class="form-control" id="inputcourse" name="course" placeholder="e.g. Diploma in Information Communication Technology">
    </div>
    <div class="form-group">
        <label class="text-white" for="courselevel">Select your current Course's Level</label>
        <select class="form-control" id="select" name="level">

            <option>Artisan</option>
            <option>Craft</option>
            <option>Diploma</option>
            <option>Higher Diploma</option>


        </select>
    </div>
    <div class="form-group">
        <label class="text-white" for="year"> Final Examination Year</label>
        <input type="number" class="form-control" id="datepicker" name="feyear" placeholder="e.g. 1994" min="1980" max="2018" value="2018">
    </div>
    <div class="form-group">

        <label class="text-white" for="year"> Series</label>
        <Select class="form-control" id="selectSeries" name="feser">
            <option>July</option>
            <option>November</option>
        </select>
    </div>
    <div class="form-group">
        <!--<a class="btn btn-primary btnNext" >Next Tab</a> -->  <button type="submit" class="btn bg-primary btn-outline btn-xl js-scroll-trigger">Add</button></div>
</form> 
2
  • You'll want to do some watching and logging here. Open up your browser's developer console and watch the network tab to make sure the request is going through and if there's anything being returned. Use Log::info(); inside of your controller to verify that it's hitting the function, the validation is working, and the student is being saved (Log::info($std); after the save). All data from Log::info will be put in a log file inside of /storage/logs Commented May 22, 2019 at 16:02
  • Thanks. Let me try that. Thanks also for assisting format the post Commented May 22, 2019 at 16:04

3 Answers 3

2

Maybe, you could try with fill method to be more specific

try:

$std = new Student;
$std->fill($request->all());
$std->save();
Sign up to request clarification or add additional context in comments.

Comments

1

Have you checked the Request data you are receiving? Try dumping it and checking if it's correct dd($request->all())

Check your logs too, laravel.log, php-errors.log, etc.

Also, your validator should return the validated data, like:

$validatedData = $request->validate([
    'adm'=>'required|unique:student',  
    'fullname'=>'required|alpha',
    etc....
]);

Then, if all the fields are in your fillable array, you can just do: Student::create($validatedData)

Any way, I would recommend you use Form Requests for validation, so your controller isn't so bloated.

5 Comments

Thanks Thiago. I have dd'd just before the validator and i got the dump of the information typed in the form,
however dumping just before the $std->save(); does not return anything. likewise saving logs at this line returns empty logs
still no success.
So, you tried dd($std) and nothing printed? I know it's sounds lame, but have you imported 'App\Student' or whatever your namespace is? Have you checked php error log?
Actualy I suspect that the validator is the issue since when I remove it, the other part works, that is data is inserted into the database
1

try doing

$std = Student::create([

       'adm'=> $request->get('adm'),  
       'fullname'=> $request->get('fullname'), 
       'dept'=> $request->get('dept'), 
       'course'=> $request->get('course'),  
       'level'=> $request->get('level'),  
       'feyear'=> $request->get('feyear'), 
       'feser'=> $request->get('feser'), 
       'idnum'=> $request->get('idnum'), 
       'current_address'=> $request->get('current_address'),  
       'permanent_address'=> $request->get('permanent_address'),
       'email'=> $request->get('email'),
       'mobile'=> $request->get('mobile'), 
       'occupation'=> $request->get('occupation'), 
       'occupation_place'=> $request->get('occupation_place'),
       'otherphone'=> $request->get('otherphone'), 
       'nextofkin'=> $request->get('nextofkin'), 
       'nextofkinadd'=> $request->get('nextofkinadd'), 
       'nextofkinphone'=> $request->get('nextofkinphone'), 
       'placeofworkadd'=> $request->get('placeofworkadd'), 
       'supervisoradd'=> $request->get('supervisoradd') 
   ]);

or the easier way if your $request fields are same as in fillable

$input=$request->all();
$std = Student::create($input);

1 Comment

Thanks that worked. though validator isn't included

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.