I want to get the id of the cycles
table so that I can store to the mortalities
table.
SELECT `id` FROM `cycles`
WHERE `date_start_raise` <= request('date_input')
AND `date_end_raise` >= request('date_input')`
Converted to Laravel Query Builder:
<?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') was from the 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()
]);
}
After that I got an error:
"message": "SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '[]' for column 'cycle_id' at row 1 (SQL: insert into
mortalities
(date_input
,number_of_mortality
,chicken_age
,cause_of_death
,cycle_id
,user_id
,updated_at
,created_at
) values (2018-09-04, 25, 2, , [], 1, 2018-11-28 07:38:01, 2018-11-28 07:38:01))"
I fix the error but new error appeared
"message": "Trying to get property 'id' of non-object"
Can anyone please help me?