I'm getting this error while up entering data to my table
class EntryController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function entry(Request $request){
$category = category::create(['cat_name' => $request->input('cat_name')]);
$product= product::create([
'Product_name'=>$request->input('Product_name'),
'Produc_quantity'=>$request->input('Produc_quantity'),
'Product_Desc' =>$request->input('Product_Desc'),
'cat_id'=>$category->cat_id]);
class category extends Model
{
protected $fillable = array('cat_description','cat_name');
public function product()
{
return $this->hasMany(product::class);
}
//
}
these are my models
class product extends Model
{
protected $fillable = array('Product_name','Product_Desc',
'Produc_quantity','sale_price',
'cost_price');
public function category()
{
return $this->belongsTo(category::class);
}
//
}
I've removed the cat_id from $fillables in product model and this is giving this error and I'm not understanding this I've check many forums and questions but I didn't find an answer this is product table
Schema::create('products', function (Blueprint $table) {
$table->increments('product_id');
$table->string('Product_name');
$table->string('Product_Desc');
$table->integer('sale_price');
$table->integer('cost_price');
$table->integer('Produc_quantity');
$table->integer('cat_id')->unsigned();
$table->foreign('cat_id')->references('cat_id')->on('categories');
$table->timestamps();
this is category table
Schema::create('categories', function (Blueprint $table) {
$table->increments('cat_id');
$table->string('cat_name');
$table->string('cat_description');
$table->timestamps();
Are you sure that the category has been created successfully?
If so, maybe you need to add cat_id
to your $fillable
.
Also when using custom id like cat_id
you need to add
public $primaryKey = 'yourkey'
我解决了这个问题,实际上我的product
模型$fillables
有问题,我在类别模型$fillables
cat_id
中添加了cat_id
来解决此问题,谢谢您的帮助
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.