I'm getting this errorarray_map(): Argument #2 should be an array
when a user trying to create a product
I changed my code from this solutions How to make each authenticated user only see their own product, and now it gives me that error.
ProductController
class productController extends Controller
{
public function index(Request $request)
{
$userId = $request->user()->id;
$products = products_model::where('user_id', $userId)->get();
return view('seller.product.index',compact('products'));
}
public function create()
{
return view('seller.product.create');
}
public function seller()
{
$products=products_model::all();
return view('seller.product.index',compact('products'));
}
public function store(Request $request)
{
$formInput=$request->except('image');
$this->validate($request, [
'pro_name'=> 'required',
'pro_price'=> 'required',
'pro_info'=> 'required',
'user_id' => \Auth::id(),
'image'=>'image|mimes:png,jpg,jpeg|max:10000'
]);
$image=$request->image;
if($image){
$imageName=$image->getClientOriginalName();
$image->move('images', $imageName);
$formInput['image']=$imageName;
}
products_model::create($formInput);
return redirect()->back();
}
public function show($id)
{
//
}
public function edit($id)
{
//
}
public function update(Request $request, $id)
{
//
}
public function destroy($id)
{
$userId = $request->user()->id();
$deleteData=products_model::where('user_id', $userId)->findOrFail($id);
$deleteData->delete();
return redirect()->back();
}
}
Products_model
class products_model extends Model
{
protected $table='products';
protected $primaryKey='id';
protected $fillable= ['user_id','pro_name','pro_price','pro_info','image','stock','category_ id'];
}
Products table
class CreateProductsTable extends Migration
{
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('pro_name');
$table->integer('pro_price');
$table->text('pro_info');
$table->integer('stock');
$table->string('image')->nullable();
$table->timestamps();
$table->bigInteger('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
public function down()
{
Schema::dropIfExists('products');
}
}
After updating my code now am getting this errorSQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into
products(
pro_name,
pro_price,
stock,
pro_info,
i`