从表中获取日期数据,以便我可以用于碳diffInDays()

时间:2018-11-29 05:58:58

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

我想获取cycles表的date_start_raise,以便可以通过使用碳diffInDays()来使鸡变老

MoralalityController.php:

public function store(Request $request)
{
    $this->validate($request, array(
    'date_input' => 'required|date',
    'number_of_mortality' => 'required|numeric',
    ) );

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

        $id = 0;
        foreach($cycle as $value){
            $id = $value->id;
        }

        $dateStart = Cycle::select('date_start_raise')
        ->where('id','=',$id)
        ->get();

        $start = Carbon::now() ;
        $input = new Carbon($request->get('date_input'));
        foreach($dateStart as $value){
            $start = $value->start;
        }

        $chickenAge = $start->diffInDays($input) ;

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

但是我有一个错误,它说

  

“在null上调用成员函数diffInDays()”

$ start为空。

如何获取cycles表中的date_start_raise

3 个答案:

答案 0 :(得分:1)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (9, 9)) dilated = cv2.dilate(image, kernel) eroded=cv2.erode(dilated,kernel) 应该是$start = $value->start;

我不知道您的框架是什么,但是通常如果只需要一行就可以尝试first(),getFirst()或get('first'),我认为您可以使用{{1} }并按条件添加顺序,然后可以保存第二个查询。

我不知道什么是Carbon :: now(),但是我认为您的逻辑像这样:

$start = $value->data_start_raise;

第一次,您定义了$ start = Carbon :: now();但是在foreach中,您将$ start作为一个日期字符串覆盖,因此diffInDays()不能是一个字符串的方法。

已更新

Cycle::select('id', 'data_start_raise')

答案 1 :(得分:0)

尝试此代码。它将在foreach自身中保存死亡率

  public function store(Request $request)
     {
                $this->validate($request, array(
                'date_input' => 'required|date',
                'number_of_mortality' => 'required|numeric',
                ) );

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

                    $id = 0;
                    foreach($cycle as $value){
                        $id = $value->id;
                        $dateStart = Cycle::select('date_start_raise')
                                          ->where('id','=',$id)
                                          ->first();   
                        $start = Carbon::now() ;
                        $input = new Carbon($request->get('date_input'));
                        $a = $dateStart->date_start_raise;
                        $start = Carbon::parse($a);
                        $chickenAge = $start->diffInDays($input) ;
                        return Mortality::create([
                         'date_input' => request('date_input'),
                         'number_of_mortality' => request('number_of_mortality'),
                         'chicken_age' => $chickenAge,
                         'cause_of_death' => request('cause_of_death'),
                         'cycle_id'  => $id,
                         'user_id'  => Auth::id()
                       ]);
                  }
   }

答案 2 :(得分:0)

 public function store(Request $request)
    {
        $this->validate($request, array(
        'date_input' => 'required|date',
        'number_of_mortality' => 'required|numeric',
        ) );

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

        $id = 0;
        $chickenAge = 0;
        $input= Carbon::parse($request->get('date_input'));
        foreach($cycle as $value){
            $id = $value->id;
        }
        if ($id) {
            $start = Carbon::parse($value->date_start_raise);
            $chickenAge = $start->diffInDays($input) ;
        }

这是代码