Illuminate \ Database \ QueryException (HY000) SQLSTATE[HY000]: General error: 1364 Field 'p_id' doesn't have a default value (SQL: insert into `prod_cats` (`updated_at`, `created_at`) values (2019-09-03 05:18:24, 2019-09-03 05:18:24))
ProdCatController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\prod_cat;
use App\product;
use App\category;
class ProdCatController extends ProductController
{
public function store1(Request $req)
{
//dd($req->all());
prod_cat::create([
'p_id'=>request('p_id'),
'c_id'=>request('c_id'),
]);
$product=product::get();
$category=category::get();
$prod_cat=prod_cat::get();
array($request->get('c_id'));
return view('pcview',compact('product','category','prod_cat'));
}
public function show1()
{
$product=product::get();
$category=category::get();
$prod_cat=prod_cat::get();
return view('pcview',compact('product','category','prod_cat'));
//return redirect('prodview');
}
}
And remove the $guarded because $guarded means don't allow someone else to set the value of the fields in this array. You should either have $guarded as an empty array to allow all, or $fillable to allow specific fields to be set. So you are guarding at the moment from someone to set the p_id and c_id and yet you want to create a resource of those values in the controller, hence the exception.
@kanchan186 so the error says it all. In your view you don't have an item with name="c_id" and you are using request('c_id') in your controller which returns null so trying to store null is not allowed.
Your using in the function as $req but in the create function your calling request change as like below and check,
public function store1(Request $request)
{
//dd($req->all());
prod_cat::create([
'p_id'=>$request('p_id'),
'c_id'=>$request('c_id'),
]);
// other codes
}
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\prod_cat;
use App\product;
use App\category;
class ProdCatController extends ProductController
{
public function store1(Request $request)
{
if ($request->p_id && $request->p_id ) {
if ($categor = prod_cat::create([
'p_id'=>$request->p_id,
'c_id'=>$request->c_id
]);) {
// remove the rest for brevity sake
return response('Product category created successfully', 200);
}
// or whatever you want to do when it fails
return response('Failed to find ids', 422);
}
}
@kanchan186 please don't look at what @munazzil said, because that's completely the same, you were using the request helper function the error is not there.. Look at what I have said above, the problem is because you don't pass c_id to your controller which is null that's why you get the error.
This code:
@foreach($category as $cat)
<input type="checkbox" name="cat[{{$cat->c_id}}]" value ="{{$cat->c_id}}">{{$cat->c_name}}<br>
@endforeach</td>