“尝试获取非对象的属性'id'”

g6ll5ycj  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(296)

我想知道死者的身份 cycles 这样我就可以把 mortalities table。

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();

请求('date\u input')来自VermanityController
凡人控制器.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()
    ]);

}

之后我犯了个错误:
“message”:“sqlstate[22007]:无效的日期时间格式:1366不正确的整数值:'[]”对于第1行的“cycle\u id”列(sql:insert-into) 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'”
有人能帮我吗?

zysjyyx4

zysjyyx41#

您需要使用从$request对象获取值 $request->get('your_id') 或者 $request->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
}

为你工作吧。。。

vcudknz3

vcudknz32#

看看这个!!

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()
    ]);`
bq8i3lrv

bq8i3lrv3#

像那样使用(u正在传递集合以插入)

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

相关问题