我的数据库中有 2 个包含城市和州的表,如果我要提交城市,那么我想将 state_id 保存在我的城市表中。城市表的主键是 ctId 城市表的主键是 stId
这是我的状态模型... State.php 文件
<?php
namespace App;
use App\City;
use Illuminate\Database\Eloquent\Model;
class State extends Model
{
protected $primaryKey = 'stId';
// protected $gaurded = [];
public function cities(){
return $this->hasMany(City::class,'ctId','ctId');
}
}
这是我的城市模型... City.php 文件
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\State;
class City extends Model
{
protected $primaryKey = 'ctId';
protected $gaurded = [];
public function state(){
return $this->belongsTo(State::class);
}
}
这是我的 CityController.php
public function store(Request $r){
//dd($r->all());
$cities=new City;
$cities->name=$r->cityname;
$cities->slug=$r->cityslug;
$cities->save();
$this->return()->back();
}
但是当我提交这个时,我收到了这个错误
SQLSTATE[HY000]: General error: 1364 Field 'stId' doesn't have a default value