我有一个名为complains的类和一个名为users的类。 这些类之间存在一种关系,可以像这样变形:
class User extends Authenticatable
{
//many to many
public function complains()
{
return $this->morphToMany('App\Complain', 'complainable');
}
}
我在抱怨控制器类中有这样的功能:
public function selectUserComplain(Request $request){
//validate inputs before create
$response['success'] = [false];
$valid = Validator::make($request->all(), [
'complain_id' => 'required|numeric',
'user_id' =>'required|numeric',
'bike_id' => 'required|numeric'
]);
if (!$valid->fails()) {
$complain_id = $request->input('complain_id');
$user_id = $request->input('user_id');
$bike_id = $request->input('bike_id');
$complain = Complain::find($complain_id);
$user=User::find($user_id);
if($user && $complain) {
$user->complains()->bike_id = $bike_id;
if($user->complains()->save($complain)) {
$response['success'] = true;
}
}
}
return response()->json($response,200,[],JSON_PRETTY_PRINT)->header('Access-Control-Allow-Origin','*')
->header('Content-Type','application/json');
}
我的问题是当我插入“抱怨”表时,如何在表格中同时插入名为“bike_id”的值