简体   繁体   English

使用laravel将一对多关系保存到数据库中

[英]Saving a one to many relationship into a database using laravel

I need some help saving data into my database using a one to many relationship (one bear many fish). 我需要一些帮助,使用一对多关系(将一条鱼养一条鱼)将数据保存到数据库中。 Would be great if you could show me some example on how to do it. 如果您可以向我展示一些有关如何执行此操作的示例,那将很好。 Because I can't seem to get the data because my bear_id is 0. (eg: bear_id = 1 can retrieve fish_id of 1 and 2) 因为我的bear_id为0,所以似乎无法获取数据。(例如:bear_id = 1可以检索1和2的fish_id)

Here are my codes: 这是我的代码:

for blade.php: 对于blade.php:

{{Form::text('name_of_bear, '', ['class' => 'form-control'])}} --> first page, once user has entered bear name it will redirect it to the fish page to enter the checkbox


<input type="checkbox" id="inlineCheckbox1" name="type_of_fish[]" value="Salmon"> Salmon <input type="checkbox" id="inlineCheckbox1" name="type_of_fish[]" value="Sardine"> Sardine --> second page after user has entered the data for bear they will click next and be redirected to here

for my tables: 对于我的桌子:

Schema::create('bears, function (Blueprint $table) {
            $table->increments('id');
            $table->engine = 'InnoDB';  
            $table->string(name); 
            $table->timestamps();
});

Schema::create(fishs, function (Blueprint $table) {
            $table->increments('id');
            $table->engine = 'InnoDB';  
            $table->string(name); 
            $table->integer(bear_id);
            $table->timestamps();
});

Fish model: 鱼模型:

class fish extends Eloquent
{
        protected $fillable = array('name', 'bear_id');

    // DEFINE RELATIONSHIPS 
    public function bears() {
        return $this->belongsTo('App\bear);
    }
}

bear model: 熊模型:

class bear extends Eloquent
{
    protected $primaryKey = 'id';
    public function fishs() {
        return $this->hasMany('App\fish,'bear_id');
    }
}

For the controller part, I am still learning so I don't really know how to use it 对于控制器部分,我仍在学习,所以我真的不知道如何使用它

Controller: 控制器:

public function view(Request $request)
{
        $fish= new fish();

        $fishType= $request->input('type_of_fish');
        $fish->type_of_fish= json_encode($fishType);

         $fish->save();

$bear= new bear();
        $bear->Name = $request->input('name_of_bear');
$bear->save();
$fish->bear_id = $bear->id;    
$fish->save();

Rather than manually setting the bear_id of the fish model, Eloquent provides a way for you to associate the models. Eloquent无需手动设置鱼类模型的bear_id,而是为您提供了一种关联模型的方法。 Note that I'm using the static create() method instead of instantiating new models and filling in the properties separately. 请注意,我使用的是静态create()方法,而不是实例化新模型并分别填充属性。

$fish = fish::create(['type_of_fish' => json_encode($fishType)]);
$bear = bear::create(['Name' => $request->input('name_of_bear');

$fish->bears()->associate($bear);
$fish->save();

However, since you're not dealing with existing resources at this point, you can use the Eloquent's create method for relationships. 但是,由于此时您不处理现有资源,因此可以将Eloquent的create方法用于关系。

$bear = Bear::create(['Name' => $request->input('name_of_bear')]);
$bear->fishes()->create(['type_of_fish' => $fishType);

This will create a new fish and then auto associate it with the bear created in the line above. 这将创建一条新的鱼,然后将其与上一行中创建的熊自动关联。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM