【问题标题】:trying to access array offset on value of type null laravel试图访问 null laravel 类型值的数组偏移量
【发布时间】:2020-10-22 08:08:10
【问题描述】:

我使用了所有五种方法,但都没有奏效。并且不存储在数据库中。

我在educationals 表中有一行

而我的表格就是这样的demo

我的刀

<input  name="study[field]">
<input  name="study[branch]">
<input  name="study[grade_id]">
<input  name="study[institution_id]">
<input  name="study[institution_education]">
<input  name="study[gpa]">
<input  name="study[nation_id]">
<input  name="study[province_id]">
<input  name="study[town_id]">
<input  name="study[branch]">
<input  name="study[province_name]">
<input  name="study[town_name]">
<input  name="study[entrance]">
<input  name="study[graduate]">
<input  name="study[currently_studying]">

我的控制器

    foreach ($request->field as $key => $value) {
        Educational::firstOrCreate(['user_id' => auth()->id()], [
            'grade_id' => $request->grade_id[$key],
            'field' => $request->field[$key],
            'institution_id' => $request->institution_id[$key],
            'branch' => $request->branch[$key],
            'institution_education' => $request->institution_education[$key],
            'gpa' => $request->gpa[$key],
            'nation_id' => $request->nation_id[$key],
            'province_id' => $request->province_id[$key],
            'town_id' => $request->town_id[$key],
            'province_name' => $request->province_name[$key],
            'town_name' => $request->town_name[$key],
            'entrance' => $request->entrance[$key],
            'graduate' => $request->graduate[$key],
            'currently_studying' => $request->has("currently_studying.$key"),
        ]);
    }

我收到此错误。

得到dd后:

【问题讨论】:

  • 请尝试dd($request-&gt;all())并将屏幕截图上传到问题中。
  • 我看到了这个dd i.stack.imgur.com/z9yqB.png
  • 我认为您应该在field 之前添加study,因为field 和其他键是学习数组的一部分。如果你想在fields 中有多个选项,你的名字必须看起来像这样study[field][]
  • 我转学前学习,但我的问题还没有解决。我看到这个错误。再次。 i.stack.imgur.com/wYzHf.png

标签: php laravel


【解决方案1】:

您使用错误的变量来获取预期值。在您的示例中,grade_id 不是数组而是 null,因为您的请求正文的根级别中没有 grade_id,而是在 study 属性中。

如果$request-&gt;study 已经是一个包含这些所需值的数组,您只需通过$request-&gt;study[property] 检索它们。

考虑:

Educational::firstOrCreate(['user_id' => auth()->id()], [
    'grade_id' => $request->study['grade_id'],
    'field' => $request->study['field'],
    'institution_id' => $request->study['institution_id'],
    'branch' => $request->study['branch'],
    'institution_education' => $request->study['institution_education'],
    'gpa' => $request->study['gpa'],
    'nation_id' => $request->study['nation_id'],
    'province_id' => $request->study['province_id'],
    'town_id' => $request->study['town_id'],
    'province_name' => $request->study['province_name'],
    'town_name' => $request->study['town_name'],
    'entrance' => $request->study['entrance'],
    'graduate' => $request->study['graduate'],
    'currently_studying' => $request->study['currently_studying'] ? true : false,
]);

你看到只是通过研究检索所有内容。

事实上,这很容易,因为您的所有请求字段都与您的模型属性具有相同的名称,请执行以下操作:

Educational::firstOrCreate(['user_id' => auth()->id()], $request->study);

【讨论】:

  • 只有一行,Educational::firstOrCreate(['user_id' =&gt; auth()-&gt;id()], $request-&gt;study);
  • 我测试过了。但它没有保存在数据库中。
  • 现在这是棘手的部分。 FirstOrCreate(search, data) 使用搜索来查找模型,如果找到它会检索它,如果没有找到它使用 search 和 data 创建它。您是否已经在数据库中记录了该user_id?如果是这种情况,您可能需要updateOrCreate()。
  • 您是否使用auth()-&gt;id() 而不是$user-&gt;id?
  • 现在正确吗? i.stack.imgur.com/C8gH1.png
猜你喜欢
  • 2021-02-02
  • 1970-01-01
  • 2021-01-09
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
  • 2020-09-10
  • 1970-01-01
相关资源
最近更新 更多