【问题标题】:modifing request() array returns error Indirect modification of overloaded property修改 request() 数组返回错误 间接修改重载属性
【发布时间】:2019-06-06 17:11:47
【问题描述】:

我有一个表单,其中包含带有数组的输入名称,示例:

user_profile[birth_day]
user_profile[birth_month]
user_profile[birth_year]
user_profile[birthdate]

现在我想将它们组合起来并创建一个新的请求属性。

我找到了解决方案,但我知道这是不好的做法:

$user_profile['birthdate'] = Carbon::parse(implode("-", [$request->user_profile['birth_day'], $request->user_profile['birth_month'], $request->user_profile['birth_year']]))->format('Y-m-d');
$request->request->add(['user_profile' => $user_profile+$request->user_profile]);

当我得到$request->user_profile['birthdate'] 时,它会返回正确的输入(如果已填写)。但是当我尝试修改$request->user_profile['birthdate'] 时它为空时:

$reuest->user_profile['birthdate'] = Carbon::parse(implode("-", [$request->user_profile['birth_day'], $request->user_profile['birth_month'], 
$request->user_profile['birth_year']]))->format('Y-m-d');

我收到以下错误:

间接修改重载属性 Illuminate\Http\Request::$user_profile 无效

【问题讨论】:

    标签: php laravel-5.8


    【解决方案1】:

    你可以试试这个,我想这会奏效

    $reuest->user_profile->birthdate = Carbon::parse(implode("-", [$request->user_profile['birth_day'], $request->user_profile['birth_month'], $request->user_profile['birth_year']]))->format('Y-m-d');

    【讨论】:

    • $request->user_profile->birthdate = 1;生成以下错误:“ErrorException (E_NOTICE) 对重载属性 Illuminate\Http\Request::$user_profile 的间接修改无效”
    • 好的,那么您需要将请求数据分配到新变量中,您可以在您的过程中访问它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 2018-09-20
    • 1970-01-01
    • 2012-05-16
    • 1970-01-01
    相关资源
    最近更新 更多