简体   繁体   中英

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)

Here are my codes:

for 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. Note that I'm using the static create() method instead of instantiating new models and filling in the properties separately.

$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.

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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