如何将请求数据存储到3个不同的表中?

时间:2018-02-14 19:56:35

标签: php laravel eloquent

我有一个表单,允许经过身份验证的用户创建新会议。

此表单将请求用户信息,用户介绍的部分信息应存储在Conference表中,一些存储在“Conference_Category”表中,一些存储在“RegistrationType”表中。

因此,当用户提交表单“dd($request->all());”时显示:

array:20 [▼
  "_token" => "DbDKu3ryk4NdxKLr3aO132iHrE0O5b0XqfENk1"
  "conference_name" => "Conference Name"
  "conference_categories" => "1"
  "conference_description" => "description"

  "conference_creator_name" => "John"
  "conference_creator_email" => "email@testemail.email"
  "conference_creator_description" => "description"

  "registration_type_name" => "registration type name"
  "registration_type_description" => "registration type desc"
  "registration_type_capacity" => "100"
]

DOUBT

我怀疑如何正确存储此请求信息。因为必须将请求数据存储到3个不同的表中以创建新的会议:

  • 在“会议”表中需要存储:conference_name,conference_description,conference_creator_id,conference_creator_name,conference_creator_email,conference_creator_description

  • 在“conference_category”表中需要存储:会议类别ID和会议ID

  • 在“RegistrationType”表中需要存储:registration_type_name,registration_type_description和registration_type_capacity

我有一个ConferenceController,我有逻辑在ConferenceController的store()方法中创建一个新的会议。但是现在我只是将请求信息存储在会议桌上。

您是否知道如何将用户引入的类别和注册类型存储在相应的表格中(conference_category和RegistrationType)?

ConferenceController创建方法:

<?php

namespace App\Http\Controllers;

use App\Category;
use App\Conference;
use Illuminate\Http\Request;

class ConferenceController extends Controller
{
        public function store(Request $request)
    {
        dd($request->all());

        $this->validate($request, [
            'conference_name' => 'required|max:255',
            'conference_categories' => 'required|array|between:1,3',
            'conference_startDate' => 'required',

            'conference_creator_name' => 'required',
            'conference_creator_email' => 'required|email|unique:users',
            'conference_creator_description' => '',

            'conference_registration_type_name' => 'required',
            'confenrece_registration_type_description' => '',
            'conference_registration_type_capacity' => 'required',

        ]);
        $conference = Conference::create([
            'name' => $request->conference_name,
            'description' => $request->conference_description,
            'startDate' => $request->conference_startDate,

            'conference_creator_id' => Auth::user()->id,
            'conference_creator_name' => $request->conference_creator_name,
            'conference_creator_email' => $request->conference_creator_email,
            'conference_creator_description' => $request->conference_creator_description
        ]);
    }
}

此问题的相关关系为:

  • 会议和类别之间的多对多(会议可以有很多类别,所以还有第三个表conference_category)
  • 用户和会议之间的一对多(用户可以创建多个会议)
  • 会议和注册类型之间的一对多(会议可以有多种注册类型)

会议模式

class Conference extends Model
{
    protected $fillable = [
        'name', 'description', 'startDate', '...'
    ];

    public function categories(){
        return $this->belongsToMany('App\Category');
    }
    public function conference_creator(){
        return $this->belongsTo('App\User', 'conference_creator_id');
    }
    public function registrationTypes(){
        return $this->hasMany('App\RegistrationType', 'conference_id');
    }
}

类别型号:

class Category extends Model
{
    public function conferences(){
        return $this->belongsToMany('App\Conference');
    }
}

RegistrationType型号:

class RegistrationType extends Model
{
    public function conference(){
        return $this->belongsTo('App\Conference');
    }
}

用户模型:

class User extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'email', 'password'
    ];
    protected $hidden = [
        'password', 'remember_token',
    ];

    public function conferences(){
        return $this->hasMany('App\Conference');
    }
}

1 个答案:

答案 0 :(得分:2)

对于您想要多对多关系的类别。因为会议可以有很多类别,而类别可以有很多会议。您已在模型中正确设置了此项。

创建会议后,您可以将类别添加到会议中,这可以使用Attach方法完成(假设确实存在给定的ID,如果不存在,则必须首先检查):

$conference->categories()->attach($request->conference_categories);

有关附加的更多信息:Laravel Eloquent

对于RegistrationType,我假设您的模型中会议可以有多个RegistrationTypes,而RegistrationType只能有1个会议。

您必须将以下$ fillables添加到RegistrationType模型:

protected $fillable = ['name', 'description', 'capacity', 'conference_id'];

创建会议后,您可以创建RegistrationType:

RegistrationType::create([
    'conference_id' => $conference->id,
    'name' => $request->conference_registration_type_name,
    'description' => $request->confenrece_registration_type_description,
    'capacity' => $request->conference_registration_type_capacity
]);

最终结果:

$conference = Conference::create([
    'name' => $request->conference_name,
    'description' => $request->conference_description,
    'startDate' => $request->conference_startDate,

    'conference_creator_id' => Auth::user()->id,
    'conference_creator_name' => $request->conference_creator_name,
    'conference_creator_email' => $request->conference_creator_email,
    'conference_creator_description' => $request->conference_creator_description
]);

$conference->categories()->attach($request->conference_categories);

RegistrationType::create([
    'conference_id' => $conference->id,
    'name' => $request->conference_registration_type_name,
    'description' => $request->confenrece_registration_type_description,
    'capacity' => $request->conference_registration_type_capacity
]);