1

I'm making an app that runs like google form using Laravel 5.7. Here is the form table that i already made:

1st page 2nd page I made an eloquent model so that each Name in Personal details table has one record that contains all these table and insert filled table into database after click submit button. The problem is, it didn't successfully inserted into database. I don't know what's the problem, here are my codes:

PersonalDetails.php (eloquent model)

namespace App;

use Illuminate\Database\Eloquent\Model;

class PersonalDetails extends Model
{
    protected $table = 'personal_details';
    protected $guarded = ['id'];

public function accomodation()
{
    return $this->hasOne('App\Accomodation');
}

public function course()
{
    return $this->hasOne('App\Course');
}

public function proposedStudy()
{
    return $this->hasOne('App\ProposedStudy');
}

public function emergencyContact()
{
    return $this->hasOne('App\EmergencyContact');
}

public function englishTestResult()
{
    return $this->hasOne('App\EnglishTestResult');
}

public function homeInstitution()
{
    return $this->hasOne('App\HomeInstitution');
}

public function insurance()
{
    return $this->hasOne('App\Insurance');
}
}

FormController.php

public function submit(Request $request) {

    $request->validate([
        // Personal Details
        'fullname' => 'required|string',
        'nationality' => 'required|string',
        'date_of_birth' => 'required|string',
        'passport_number' => 'required|string',
        'issuing_country' => 'required|string',
        'date_of_issue' => 'required|string',
        'date_of_expiry' => 'required|string',
        'blood_type' => 'required|string',
        'marital_status' => 'required|string',
        'address' => 'required|string',
        'city' => 'required|string',
        'postal_code' => 'required|numeric',
        'province' => 'required|string',
        'country' => 'required|string',
        'phone' => 'required|string',
        'mobile' => 'required|string',
        'email' => 'required|email',
        'address2' => 'nullable|text',
        'city2' => 'nullable|string',
        'postal_code2' => 'nullable|numeric',
        'province2' => 'nullable|string',
        'country2' => 'nullable|string',
        'phone2' => 'nullable|string',
        'contact_name' => 'required|string',

        // Home Institution
        'name' => 'required|string',
        'address' => 'required|string',
        'phone' => 'required|string',
        'email' => 'required|email',
        'website' => 'required|string',
        'faculty_dep' => 'required|string',
        'start_year' => 'required|string',
        'gpa' => 'required|string',

        // Proposed Study
        'semester' => 'required|in:Semester I (Aug-Jan),Semester II (Feb-Jun)',
        'academic_year' => 'required|string',
        'faculty' => 'required|string',
        'department' => 'required|string',
        'study_period' => 'required|string',
        'start_date' => 'required|string',
        'end_date' => 'required|string',
        // Course
        'course_title' => 'required|string',
        'credit' => 'required|string',

        // English Test Result
        'test' => 'required|string',
        'score' => 'required|numeric',
        'test_center' => 'required|string',  
        'date_tested' => 'required|string',

        // Insurance
        'insurance_name' => 'required|string',
        'validity' => 'required|string',
        'cover' => 'required|string',

        // Accomodation
        'accomodation_help' => 'required|in:YES,NO',
        'adress' => 'required|string',
        'contact_person' => 'required|string',

        // Contact of Emergency
        'fullname' => 'required|string',
        'relationship' => 'required|string',
        'address' => 'required|string',
        'phone' => 'required|string',
        'mobile' => 'required|string',
        'email' => 'required|email',


    ]);

    PersonalDetails::create([
        'fullname' => $request->input('name'),
        'nationality' => $request->input('nationality'),
        'date_of_birth' => $request->input('dob'),
        'passport_number' => $request->input('passport'),
        'issuing_country' => $request->input('is_country'),
        'date_of_issue' => $request->input('doi'),
        'date_of_expiry' => $request->input('doe'),
        'blood_type' => $request->input('blood'),
        'marital_status' => $request->input('maritial'),
        'address' => $request->input('address'),
        'city' => $request->input('city'),
        'postal_code' => $request->input('postal'),
        'province' => $request->input('state'),
        'country' => $request->input('country'),
        'phone' => $request->input('phone'),
        'mobile' => $request->input('mobile'),
        'email' => $request->input('email'),
        'address2' => $request->input('address2'),
        'city2' => $request->input('city2'),
        'postal_code2' => $request->input('postal2'),
        'province2' => $request->input('state2'),
        'country2' => $request->input('country2'),
        'phone2' => $request->input('phone2'),
        'contact_name' => $request->input('contact_name'),
    ]);

    HomeInstitution::create([
        'name' =>  $request->input('institution'),
        'address' =>  $request->input('i_address'),
        'phone' =>  $request->input('i_phone'),
        'email' =>  $request->input('i_email'),
        'website' =>  $request->input('web'),
        'faculty_dep' =>  $request->input('faculty_dept'),
        'start_year' =>  $request->input('s_year'),
        'gpa' =>  $request->input('gpa'),
    ]);

    ProposedStudy::create([
        'semester' => $request->input('duration'),
        'academic_year' => $request->input('f_year') . '/' . $request->input('l_year'),
        'faculty' => $request->input('faculty'),
        'department' => $request->input('department'),
        'study_period' => $request->input('spesific_period'),
        'start_date' => $request->input('start_date'),
        'end_date' => $request->input('end_date'),
    ]);

    Course::create([
        'course_title' => $request->input('course_1'),
        'credit' => $request->input('credit_1'),
    ]);

    EnglishTestResult::create([
        'test' => $request->input('toefl'),
        'score' => $request->input('score_toefl'),
        'test_center' => $request->input('place_toefl'),     
        'date_tested' => $request->input('date_toefl'),
    ]);

    Insurance::create([
        'insurance_name' => $request->input('insurance'),
        'validity' => $request->input('valid_date'),
        'cover' => $request->input('cover'),
    ]);

    Accomodation::create([
        'accomodation_help' => $request->input('opt_acc'),
        'adress' => $request->input('adress_acc'),
        'contact_person' => $request->input('cp_acc'),
    ]);

    EmergencyContact::create([
        'fullname' => $request->input('emergency_name'),
        'relationship' => $request->input('relationship'),
        'address' => $request->input('address_emergency'),
        'phone' => $request->input('emergency_phone'),
        'mobile' => $request->input('emergency_mobile'),
        'email' => $request->input('emergency_email'),
    ]);

    // return back()-> with('success', 'Berhasil submit!');
}
7
  • Any error message? Commented Dec 28, 2018 at 3:59
  • No sir, it has no error message. I have no idea what's the problem Commented Dec 28, 2018 at 4:02
  • Can you check the browser console -> network tab for the response code (200 or 404 or 422). My guess some validation failing Commented Dec 28, 2018 at 4:04
  • 1
    I think you have mass-assignment problem. You can add fillable varibale in your model. You can check here laravel.com/docs/5.7/eloquent#mass-assignment and you should put all validation in a separate request file. I know it is not related to your problem but it will make your controller thin. laravel.com/docs/5.7/requests Hope it will help you :) Commented Dec 28, 2018 at 4:26
  • 1
    I guess it could be validation failure. Just check all your Form Fields and fields under validation. There could mismatch in fields. Commented Dec 28, 2018 at 5:36

4 Answers 4

1

Finally after all these struggles to figured out the problem, i just realized that i put wrong name in validation rules. The name on the left side of validation rules is different with name in html so i reformed the name in html and validation rules.

The big deal is i didn't put any validation error message after submit so that it won't inserted into database. After i put some error message, all of the field returned error that says "The :attribute is required". Simply because the name in validation is different with the html name :D

Thank you guys for helped me to figured out the problem, i worked it out :))

Sign up to request clarification or add additional context in comments.

Comments

0

try with model path,if model folder is inside App then App\model;

eg:-
App\Insurance::create([
        'insurance_name' => $request->input('insurance'),
        'validity' => $request->input('valid_date'),
        'cover' => $request->input('cover'),
    ]);

Comments

0

add $fillalbe in Model:

protected $fillable=['full_name','nationality','...']

you will need to specify fillalbe attribute on the model. read more at https://laravel.com/docs/5.7/eloquent#mass-assignment

8 Comments

This is not necessary if a $guarded attribute is specified, which it is. While $fillable serves as a "white list" of attributes that should be mass assignable, you may also choose to use $guarded. The $guarded property should contain an array of attributes that you do not want to be mass assignable. All other attributes not in the array will be mass assignable.
Agreed with Jeremy, i think it won't be necessary if i already attach guarded array excluding the $guarded it will be the $fillable lists
try by insterting static value.
then try by adding $fillable and remove $guarded
what you get when you dump request
|
0

Can you check your table migration or model relation, then revision your form controller.

I am create example from your code and successfull insert into database.

Personal Details Migration

Schema::create('personal_details', function (Blueprint $table) {
        $table->increments('id');
        $table->string('fullname')->nullable();
        $table->string('nationality')->nullable();
        $table->timestamps();
    });

Home Institution Migration

Schema::create('home_institution', function (Blueprint $table) {
        $table->integer('personal_details_id')->unsigned()->primary();
        $table->string('name')->nullable();
        $table->string('address')->nullable();
        $table->timestamps();

        #Foreign to Table Personal Details.
        $table->foreign('personal_details_id')->references('id')->on('personal_details')->onUpdate('cascade')
            ->onDelete('cascade');

    });

Personal Detail Model Relation

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class PersonalDetails extends Model
{
protected $guarded = ['id'];


public function homeInstitution()
{
    return $this->hasOne('App\HomeInstitution');
}

# Other your Relation

}

Form Controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\PersonalDetails;

class FormController extends Controller
{
public function index()
{
    return view('form.index');
}

public function submit(Request $request)
{
    $request->validate([
        // Personal Details
        'fullname' => 'required|string',
        'nationality' => 'required|string',
        // Home Institution
        'name' => 'required|string',
        'address' => 'required|string',
    ]);

    $pd = PersonalDetails::create(
        ['fullname' => $request->get('fullname'), 'nationality' => $request->get('nationality')]
    );

    # Insert into HomeInstitution => base on Model Relation from PersonalDetails
    $pd->homeinstitution()->create(
        ['name' => $request->get('name'), 'address' => $request->get('address')]
    );

    # Insert into other youR relation
    /*$pd->example()->create(
        []
    );*/


    return dd('Successfull Insert');
}
}

Maybe its can help you.

Comments

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.