in my database I am trying to save my data into database, table fish, but only my bear_id is being saved and not the fish_location or fish_type. Can anyone help?
Information: one bear have many fish (one to many relationship)
Contoller:
public function submit(Request $request)
{
$bear= Session::get('data');
$test = array();
$test['fish_location'] = $request->fish_location;
$test['fish_type'] = $request->fish_type;
$fish = new fish;
$fish = fish::create([$test]);
$fish->bears()->associate($bear);
$fish->save();
return ('thank you');
}
fish model:
class fish extends Eloquent
{
protected $fillable = array('fish_type', 'fish_location', 'bear_id');
public function bears() {
return $this->belongsTo('App\bear');
}
fish table:
Schema::create('fishs', function (Blueprint $table) {
$table->increments('id');
$table->engine = 'InnoDB';
$table->string('fish_location');
$table->string('fish_type');
$table->integer('bear_id');
$table->timestamps();
});
The create()
method expects an array of data to set. What you've actually passed in is an array that contains the array of data. Remove the surrounding []
and you should be good:
$fish = fish::create($test);
call $fish->save(); before $fish->bears()->associate($bear);
$fish->save();
$fish->bears()->associate($bear);
Or try
$bear= Session::get('data');
$fish = new fish;
$fish->fish_location=$request->fish_location
$fish->fish_type =$request->fish_type;
$fish->save();
$fish->bears()->associate($bear);
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.