如何使用单个表单将数据保存到两个表?

时间:2017-09-29 12:14:18

标签: php forms laravel-5

如何使用单一表单将数据保存到两个表?

这是我的喜欢型号:

class Like extends Model
{
    protected $fillable = [
        'user_id','post_id','value',
    ];

    public function user()
    {
        return $this->belongsTo('App\User');


        //$this->belongsTo(User::class);
        //return $this->belongsTo('App\User');
        //   return $this->belongsTo('App\User','id','user_id');

    }
    public function post()
    {
        return $this->belongsTo('App\Post');


        //$this->belongsTo(User::class);
        //return $this->belongsTo('App\User');
        //   return $this->belongsTo('App\User','id','user_id');

    }

post.php中

 protected $fillable = [
        'user_id','content', 'live','post_on','likes'
    ];
    protected $dates = ['post_on'];

    public function SetLiveAttribute($value)
    {
        $this->attributes['live'] = (boolean)($value);

    }


    public function getshortContAttribute(){
        return substr($this->content, 0, random_int(60,150)). '...';
    }

    public function SetPostOnAttribute($value){
        $this->attributes['post_on'] = Carbon::parse($value);
    }


    public function user()
    {
        //$this->belongsTo(User::class);
        //return $this->belongsTo('App\User');
     //   return $this->belongsTo('App\User','id','user_id');
        return $this->belongsTo('App\User');


    }
    public function comment(){

        return $this->hasMany('App\Comment');
    }
    public function like()
    {
        return $this->hasMany('App\Like');


        //$this->belongsTo(User::class);
        //return $this->belongsTo('App\User');
        //   return $this->belongsTo('App\User','id','user_id');

    }

}

我的表单使用method="PUT"

0 个答案:

没有答案