Laravel Eloquent无法插入数据库

时间:2018-12-28 03:55:45

标签: php laravel eloquent

我正在制作一个使用Laravel 5.7像Google窗体一样运行的应用程序。这是我已经制作的表格:

1st page 2nd page 我做了一个雄辩的模型,以便“个人详细信息”表中的每个“名称”都有一个包含所有这些表的记录,并在单击“提交”按钮后将填充的表插入数据库中。问题是,它没有成功插入数据库。我不知道出了什么问题,这是我的代码:

PersonalDetails.php(雄辩的模型)     

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

4 个答案:

答案 0 :(得分:0)

尝试使用模型路径,如果模型文件夹位于App内,则App \ model;

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

答案 1 :(得分:0)

在模型中添加$fillalbe

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

您将需要在模型上指定fillalbe属性。在https://laravel.com/docs/5.7/eloquent#mass-assignment

中了解更多信息

答案 2 :(得分:0)

您可以检查表迁移或模型关系,然后修订表单控制器。

我正在根据您的代码创建示例,并将其成功插入数据库中。

个人详细信息迁移

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

家庭机构迁移

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

    });

个人详细资料模型关系

<?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

}

表单控制器

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

也许它可以帮助您。

答案 3 :(得分:0)

最后,在经过所有这些努力以找出问题之后,我才意识到自己在验证规则中输入了错误的名称。验证规则左侧的名称与html中的名称不同,因此我重新定义了html和验证规则中的名称。

重要的是,提交后我没有放置任何验证错误消息,因此不会将其插入数据库。在我输入一些错误消息之后,所有字段都返回了错误消息,指出“:attribute是必需的”。仅仅是因为验证中的名称与html名称不同:D

谢谢你们帮我解决了这个问题,我解决了:))