-1

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?

3 Answers3

0

use like that (u are passing collection to insert)

$cycle = DB::table('cycles')->select('id')
        ->where('date_start_raise','<=',request('date_input'))
        ->where('date_end_raise','>=',request('date_input'))
        ->first()->id;
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
  • new error appeared `"message": "Trying to get property 'id' of non-object"` –  Nov 28 '18 at 08:09
  • Please add some explanation to your answer such that others can learn from it - what are the crucial parts? – Nico Haase Nov 28 '18 at 08:56
0

You need to get values from $request object by using $request->get('your_id') or

$request->input('your_id')

Try this..!!

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

then

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

Hop its work for you ...

-1

Check this !!

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()
    ]);`
Ambalan
  • 9
  • 2
  • new error appeared `"message": "Trying to get property 'id' of non-object"` –  Nov 28 '18 at 08:11
  • `$cycle = DB::table('cycles')->select('id') ->where('date_start_raise', '<=', request('date_input')) ->where('date_end_raise', '>=', request('date_input')) ->get(); dd($cycle->count()) // if this is zero check your database` – Ambalan Nov 28 '18 at 08:14
  • it was 0 but the database is empty –  Nov 28 '18 at 08:16
  • i check my database but it was empty –  Nov 28 '18 at 08:26
  • Please add some explanation to your answer such that others can learn from it - what are the crucial parts? – Nico Haase Nov 28 '18 at 08:55