我想获取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();
请求(“ 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))“
我已修复错误,但出现了新错误
“消息”:“试图获取非对象的属性'id'”
有人可以帮助我吗?
答案 0 :(得分:1)
您需要使用$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();
OR
$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
}
为您忙碌工作...
答案 1 :(得分:0)
这样使用(u正在传递要插入的集合)
$cycle = DB::table('cycles')->select('id')
->where('date_start_raise','<=',request('date_input'))
->where('date_end_raise','>=',request('date_input'))
->first()->id;
答案 2 :(得分:-1)
检查这个!!
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()
]);`