array_map():参数2应该是一个数组 [英] array_map(): Argument #2 should be an array
问题描述
当用户尝试创建产品时出现此错误array_map(): Argument #2 should be an array
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
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
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'];
}
产品表
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');
}
}
更新代码后,现在出现此错误SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into
产品(
pro_name ,
pro_price ,
库存,
pro_info ,
i`
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`
推荐答案
更改验证功能.代替使用$this->validate()
,
使用$request->validate()
方法:
Change your validate function. Instead of use $this->validate()
,
use $request->validate()
method:
$request->validate([
'pro_name'=> 'required',
'pro_price'=> 'required',
'pro_info'=> 'required',
'user_id' => 'required|integer',
'image'=>'image|mimes:png,jpg,jpeg|max:10000'
]);
如果验证规则通过,则您的代码将继续正常执行;但是,如果验证失败,则会引发异常,并且适当的错误响应将自动发送回用户.
If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user.
另一种解决方案:
添加
use Validator;
去上课.
$validator = Validator::make($request->all(), [
'pro_name'=> 'required',
'pro_price'=> 'required',
'pro_info'=> 'required',
'user_id' => 'required|integer',
'image'=>'image|mimes:png,jpg,jpeg|max:10000'
]);
if($validator->fails()){
//Validation does not pass logic here
}else{
//
}
更多: 使用
php artisan make:request RequestName
php artisan make:request RequestName
该文件将在app\Http\Requests
目录中创建.
The file will be created in app\Http\Requests
directory.
在文件中,将您的规则添加到rules
方法:
Within the file, add your rules to the rules
method:
public function rules()
{
return [
'pro_name'=> 'required',
'pro_price'=> 'required',
'pro_info'=> 'required',
'user_id' => 'required|integer',
];
}
更改authorize
方法,以返回true
:
public function authorize()
{
return true;
}
在您的store
方法中,将Request $request
与RequestName $request
交换.
现在,您无需验证store
方法中的$request
.仅当验证成功后,它才会存储;
In your store
method, swap the Request $request
with RequestName $request
.
Now you don't need to validate the $request
inside store
method. It will go to store only if the validation succeed;
您的store
方法现在应该看起来像
Your store
method now should looks like
public function store(RequestName $request)
{
$formInput=$request->except('image');
$image=$request->image;
if($image){
$imageName=$image->getClientOriginalName();
$image->move('images', $imageName);
$formInput['image']=$imageName;
}
products_model::create(array_merge(
$formInput, ['user_id' => Auth::user()->id]
));
return redirect()->back();
}
不要忘记
use
App \ Http \ Requests \ RequestName
Dont forget to
use
App\Http\Requests\RequestName
如果验证失败,将生成重定向响应以将用户发送回其先前的位置.错误也将闪现到会话中,以便显示.如果请求是AJAX请求,则将向用户返回带有422状态代码的HTTP响应,其中包括验证错误的JSON表示形式. 您可以在此处了解更多信息.
If validation fails, a redirect response will be generated to send the user back to their previous location. The errors will also be flashed to the session so they are available for display. If the request was an AJAX request, a HTTP response with a 422 status code will be returned to the user including a JSON representation of the validation errors. You can learn more about request validation here.
我将users_id
规则更改为user_id
,以与您的外键名称匹配.
我想您在问问题时在这里打错了文字.
I change the users_id
rule to user_id
, to match with your foreign key name.
I think you made a typo here when you asked the question.
希望有帮助.
这篇关于array_map():参数2应该是一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!