Приложение Laravel не сохраняет данные и не отображает никаких ошибок - PullRequest
0 голосов
/ 22 мая 2019

Я использую метод хранилища контроллеров для сохранения данных, у меня есть модель, представления и контроллеры на месте.Я использую маршрутизацию ресурсов Laravel и намереваюсь использовать контроллер для выполнения операции CRUD.Однако, похоже, ничего не работает.

Это метод сохранения контроллера

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');
}

Моя модель

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'
    ];
}

Моя форма

<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. janedoe@yahoo.com ">
    </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> 

Ответы [ 3 ]

2 голосов
/ 22 мая 2019

Может быть, вы могли бы попробовать с методом заливки быть более конкретным

попробуй:

$std = new Student;
$std->fill($request->all());
$std->save();
1 голос
/ 22 мая 2019

попробуйте сделать

$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') 
   ]);

или более простой способ, если ваши поля $ request совпадают с полями для заполнения

$input=$request->all();
$std = Student::create($input);
1 голос
/ 22 мая 2019

Вы проверили данные запроса, которые вы получаете?Попробуйте выгрузить его и проверить, правильно ли это dd($request->all())

Проверьте свои журналы, laravel.log, php-errors.log и т. Д.

Кроме того, ваш валидатор должен вернуть проверенные данные,например:

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

Тогда, если все поля находятся в вашем заполняемом массиве, вы можете просто сделать: Student::create($validatedData)

В любом случае, я бы порекомендовал вамиспользуйте Запросы форм для проверки, чтобы ваш контроллер не был настолько раздутым.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...