为什么我的控制器没有保存受保护的属性



我有一个名为Property的模型,它有很多可批量分配的属性,这些属性运行良好但我的防护属性不起作用,我不知道为什么

例如,这些属性是user_id和statususer_id被假定为Auth::user((->id和status必须是我在那里设置的任何值相反,当我发送表单并检查我的数据库user_id是1并且状态总是"0"时;挂起";

我的控制器功能存储:`

public function store(PropertyRequest $request) { 
$newProperty = Property::create($request->validated());    
$newProperty->user_id = Auth::user()->id;
$newProperty->status = 'whatever';
return redirect()->route('frontend.properties.thanks')->with(['property' => $newProperty]);
}`

我的模型:`

class Property extends Model { use Mediable; use HasSlug; use PropertyMutators; use PropertyScopes;
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = ['id', 'user_id', 'status'];
// vincular la propiedad a un usuario
public function user()
{
return $this->belongsTo(User::class);
}
// quienes viven en una propiedad
public function residents() 
{
return $this->hasMany(User::class);
}
}

PropertyRequest:

class PropertyRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'title' => ['required', 'max:255'],
'property_type' => ['required', 'in:casa,duplex,departamento,monoambiente'],
'city_id' => ['required'],
'neighborhood_id' => ['sometimes'],
'address' => ['required'],
'lat' => ['required', 'regex:/^-?[0-9]{1,3}(?:.[0-9]{1,15})?$/'],
'long' => ['required', 'regex:/^-?[0-9]{1,3}(?:.[0-9]{1,15})?$/'],
'description' => ['required', 'max:1000'],
'condition' => [
'required',
'in:a estrenar,excelente estado,buen estado,aceptable'
],
'living' => ['required_without:men_living,women_living,others_living'],
'men_living' => ['required_without:living'],
'women_living' => ['required_without:living'],
'others_living' => ['required_without:living'],
'monthly_price_gs' => ['min:0'],
'monthly_price_usd' => ['min:0'],
'includes_expenses' => ['sometimes'],
'expenses' => ['required_with:includes_expenses'],
'featured_image' => [
'required',
'file',
],
'images' => ['sometimes'],
'furnished' => ['sometimes'],
'pet_friendly' => ['sometimes'],
'smokers_allowed' => ['sometimes'],
'rooms' => ['required', 'min:1', 'max:8'],
'bathrooms'=> ['required', 'min:1', 'max:8'],
'garage' => ['required', 'min:0', 'max:8'],
'area_m2' => ['sometimes'],
'has_hot_water' => ['sometimes'],
'has_wifi' => ['sometimes'],
'has_tv_cable' => ['sometimes'],
'has_air_conditioning' => ['sometimes'],
'has_cctv' => ['sometimes'],
'has_alarm' => ['sometimes'],
'has_automatic_gate' => ['sometimes'],
'has_24hs_doorman' => ['sometimes'],
'has_railings' => ['sometimes'],
'has_security' => ['sometimes'],
'has_garden' => ['sometimes'],
'has_balcony' => ['sometimes'],
'has_laundry' => ['sometimes'],
'has_grill' => ['sometimes'],
'has_lift' => ['sometimes'],
'has_placard_on_room' => ['sometimes'],
'has_furnished_kitchen' => ['sometimes'],
'has_rooftop' => ['sometimes'],
'has_pool' => ['sometimes'],
'has_common_room' => ['sometimes'],
'has_gym' => ['sometimes'],
'has_deposit' => ['sometimes'],
'has_playroom' => ['sometimes'],
'has_spa' => ['sometimes'],
'has_quincho' => ['sometimes'],
'has_service_room' => ['sometimes'],
];
}
public function withValidator($validator) 
{
$validator->after(function($validator) {
if($this->stock == 0 && $this->status == 'available') {
$validator->errors()->add('stock', 'Status can't be available if stock is 0');
}
});
}
}

设置值后不会保存数据。

public function store(PropertyRequest $request)
{ 
$newProperty = Property::create($request->validated());    
$newProperty->user_id = Auth::user()->id;
$newProperty->status = 'whatever';
--> $newProperty->save(); <--
return redirect()->route('frontend.properties.thanks')->with(['property' => $newProperty]);
}

最新更新