【问题标题】:"Trying to get property 'id' of non-object"“试图获取非对象的属性 'id'”
【发布时间】:2019-04-30 01:52:18
【问题描述】:

我想获取cycles 表的ID,以便我可以存储到mortalities 表中。

SELECT `id` FROM `cycles` 
WHERE `date_start_raise` <= request('date_input') 
AND `date_end_raise` >= request('date_input')`

转换为 Laravel 查询生成器:

<?php

$cycle = DB::table('cycles')->select('id')
    ->where('date_start_raise', '<=', request('date_input'))
    ->where('date_end_raise', '>=', request('date_input'))
    ->get();

request('date_input') 来自 MortalityController

MortalityController.php

    public function store(Request $request)
{



    $this->validate($request, array(
    'date_input' => 'required|date',
    'number_of_mortality' => 'required|numeric',
    'chicken_age' => 'required|numeric'
    ) );

    $cycle = DB::table('cycles')->select('id')
     ->where('date_start_raise', '<=', request('date_input'))
      ->where('date_end_raise', '>=', request('date_input')) ->get(); 

    return Mortality::create([
        'date_input' => request('date_input'),
        'number_of_mortality' => request('number_of_mortality'),
        'chicken_age' => request('chicken_age'),
        'cause_of_death' => request('cause_of_death'),
        'cycle_id'  => $cycle->first()->id,
        'user_id'  => Auth::id()
    ]);

}

然后我得到一个错误:

“消息”:“SQLSTATE [22007]:无效的日期时间格式:1366 不正确 整数值:第 1 行的列“cycle_id”的“[]”(SQL:插入 mortalities (date_input, number_of_mortality, chicken_age, cause_of_death, cycle_id, user_id, updated_at, created_at) 值 (2018-09-04, 25, 2, , [], 1, 2018-11-28 07:38:01, 2018-11-28 07:38:01))"


我修复了错误,但出现了新错误

"message": "尝试获取非对象的属性 'id'"

谁能帮帮我?

【问题讨论】:

  • 请求中的$在哪里?这是一个变量,而不是一个函数
  • 先生在哪里?在 cycle_id 处?
  • 调用request('number_of_mortality')时,应该是$request->get('number_of_mortality'),

标签: php mysql laravel eloquent laravel-query-builder


【解决方案1】:

您需要使用$request-&gt;get('your_id')

从 $request 对象中获取值

$request-&gt;input('your_id')

试试这个..!!

$cycle = DB::table('cycles')->select('id')
            ->where('date_start_raise','<=',$request->get('date_input'))
            ->where('date_end_raise','>=',$request->get('date_input'))
            ->first();

$cycle = DB::table('cycles')->select('id')
                ->where('date_start_raise','<=',$request->get('date_input'))
                ->where('date_end_raise','>=',$request->get('date_input'))
                ->get();

然后

foreach($cycle as $value){
  //extract data from $value
}

跳它为你工作...

【讨论】:

  • 我应该把 foreach 放在哪里
  • foreach($cycle as $value){ $id = $value-&gt;id; }
  • 使用foreach循环在返回前从对象中提取id;那么这个$id你可以使用你的返回查询......确保你的数据库应该工作正常。
  • 代码的最终格式是什么?不好意思问太多了
  • 错误显示Invalid argument supplied for foreach()我可以得到代码的最终格式吗?
【解决方案2】:

这样使用(你正在将集合传递给插入)

$cycle = DB::table('cycles')->select('id')
        ->where('date_start_raise','<=',request('date_input'))
        ->where('date_end_raise','>=',request('date_input'))
        ->first()->id;

【讨论】:

  • 出现新错误"message": "Trying to get property 'id' of non-object"
  • 请在您的答案中添加一些解释,以便其他人可以从中学习——关键部分是什么?
【解决方案3】:

检查一下!!

return Mortality::create([
        'date_input' => request('date_input'),
        'number_of_mortality' => request('number_of_mortality'),
        'chicken_age' => request('chicken_age'),
        'cause_of_death' => request('cause_of_death'),
        'cycle_id'  => $cycle->first()->id,
        'user_id'  => Auth::id()
    ]);`

【讨论】:

  • 出现新错误"message": "Trying to get property 'id' of non-object"
  • $cycle = DB::table('cycles')-&gt;select('id') -&gt;where('date_start_raise', '&lt;=', request('date_input')) -&gt;where('date_end_raise', '&gt;=', request('date_input')) -&gt;get(); dd($cycle-&gt;count()) // if this is zero check your database
  • 它是 0 但数据库是空的
  • 我检查了我的数据库,但它是空的
  • 请在您的答案中添加一些解释,以便其他人可以从中学习——关键部分是什么?
猜你喜欢
  • 1970-01-01
  • 2021-05-26
  • 2019-03-10
  • 1970-01-01
  • 2013-07-24
  • 2020-06-26
  • 2018-07-03
  • 1970-01-01
  • 2019-03-27
相关资源
最近更新 更多