how to calculate data by category when creating ne

2019-02-21 03:52发布

问题:

Model

public static function findOrCreate($plan_id, $data)
{
    $fromDate = Carbon::now()->subDay()->startOfWeek();
    $nowDate = Carbon::now()->today();

    $spent_time = static::where('plan_id', $plan_id)->first();

    if (is_null($spent_time)) {
        return static::create($data);
    }else{
        $new_spent_time = SpentTime::find($plan_id);
        $task_category = $new_spent_time->task_category;

        $new_spent_time->task_category = (['{task_category}' => $task_category, 
                                        '{daily_spent_time}' => $new_spent_time->daily_spent_time,
                                        '{daily_percentage}' => $new_spent_time->daily_percentage,
                                        '{spent_time}' => $new_spent_time->spent_time,
                                        '{percentage}' => $new_spent_time->percentage, $new_spent_time->task_category]);

        $new_spent_time->spent_time = $new_spent_time::where('task_category',$task_category)
                                    ->sum('daily_spent_time', $new_spent_time->daily_spent_time , $fromDate);
        $new_spent_time['spent_time'] = (int)$new_spent_time->spent_time + $spent_time->daily_spent_time;

        $new_spent_time->percentage = $new_spent_time::where('task_category',$task_category)
                                    ->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
        $new_spent_time['percentage'] = (int)$new_spent_time->percentage  + $spent_time->daily_percentage;

        return $spent_time->update($data);
    }
}

Controller

public function store(Request $request)
{      
    $spent_time = SpentTime::findOrCreate($request->get('plan_id'), [
        'plan_id' => $request->get ('plan_id'),
        'daily_spent_time' => $request->get ('daily_spent_time'),
        'daily_percentage' => $request->get ('daily_percentage'),
        'reason' => $request->get ('reason'),
    ]);

    return redirect()->route('real.index', compact( 'spent_time'));
}

View

with problems, when saving create new data then an error "Trying to get property of non-object"

but there is one data that can save when creating new data, but cannot yet calculate the data, and while the other categories cannot be like that, instead the error

what should be corrected from this problem?

回答1:

In your SpentTime model, you are able to create accessors which are functions that can be used here to query the sum a day of all relating records:

public function getDailySpentTimeAttribute()
{
    return self::where('task_category_id', $this->task_category_id)
        ->get()
        ->sum('daily_spent_time');
}

public function getDailyPercentageAttribute()
{
    return self::where('task_category_id', $this->task_category_id)
        ->get()
        ->sum('daily_percentage');
}

Here, we create two accessors, one to get the daily spent time and another to get the daily percentage, for all records based on relating task_category.

There can be called using the following:

$dailySpentTime = SpentTime::find($id)->dailySpentTime;

// or within your blade template

{{ $spentTime->dailySpentTime }}

Update

Within your controller, as you no longer have to run any calculations upon saving, you can do the following:

public function store(Request $request)
{      
    $spent_time = SpentTime::findOrCreate($request->get('plan_id'), [
        'task_category' => $request->get('task_category'),
        'reason'        => $request->get('reason'),
    ]);

    return redirect()->route('real.index', compact('spent_time'));
}

Make sure to delete your custom findOrCreate() method which is currently overriding the laravel version.

Hopefully this helps.



回答2:

update Model

public static function findOrCreate($plan_id, $data)
{
    $fromDate = Carbon::now()->subDay()->startOfWeek();
    $nowDate = Carbon::now()->today();

    $spent_time = static::where('plan_id', $plan_id)->first();

    if (is_null($spent_time)) {
        return static::create($data);
    }else{

        $new_spent_time = SpentTime::first();
        $task_category = $new_spent_time->task_category;

        $new_spent_time->task_category = (['{task_category}' => $task_category, 
                                        '{daily_spent_time}' => $new_spent_time->daily_spent_time,
                                        '{daily_percentage}' => $new_spent_time->daily_percentage,
                                        '{spent_time}' => $new_spent_time->spent_time,
                                        '{percentage}' => $new_spent_time->percentage, $new_spent_time->task_category]);

        $new_spent_time->spent_time = $new_spent_time::where('task_category',$task_category)
                                    ->sum('daily_spent_time', $new_spent_time->daily_spent_time , $fromDate);
        $new_spent_time['spent_time'] = (int)$new_spent_time->spent_time + $spent_time->daily_spent_time;

        $new_spent_time->percentage = $new_spent_time::where('task_category',$task_category)
                                    ->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
        $new_spent_time['percentage'] = (int)$new_spent_time->percentage  + $spent_time->daily_percentage;

        return $spent_time->update($data);
    }
}


回答3:

Update Model V.2

and now new problem, can save create new data, but can't calculated data by same category

public static function findOrCreate($plan_id, $data)
{
    $fromDate = Carbon::now()->subDay()->startOfWeek();
    $nowDate = Carbon::now()->today();

    $spent_time = static::where('plan_id', $plan_id)->first();

    if (is_null($spent_time)) {
        return static::create($data);
    }else{
        $new_spent_time = SpentTime::first();
        $task_category = $new_spent_time->task_category;

        $new_spent_time->spent_time = $new_spent_time::where('task_category',$task_category)
                                    ->sum('daily_spent_time', $new_spent_time->daily_spent_time , $fromDate);
        $new_spent_time['spent_time'] = $new_spent_time->spent_time + $spent_time->daily_spent_time;

        $new_spent_time->percentage = $new_spent_time::where('task_category',$task_category)
                                    ->sum('daily_percentage', $new_spent_time->daily_percentage, $fromDate);
        $new_spent_time['percentage'] = $new_spent_time->percentage  + $spent_time->daily_percentage;

        $new_spent_time->save();

        return $spent_time->update($data);
    }


回答4:

the data is there, when dd($spent_time);

dd($spent_time);



回答5:

Update Answer

Model

public static function findOrCreate($plan_id, $data)
{
    $spent_time = static::where('plan_id', $plan_id)->first();
    $task_category = $spent_time->task_category;

    if (is_null($spent_time)) {
        return static::create($data);
    }else{

        $spent_time['spent_time'] = $spent_time->spent_time + $spent_time->daily_spent_time;

        $spent_time['percentage'] = $spent_time->percentage  + $spent_time->daily_percentage;
        return $spent_time->update($data);
    }
}

Controller

public function index()
{
    $spent_times = SpentTime::orderBy('task_category')->where('created_at', '>=', Carbon::today()->toDateString())->paginate(10);
    $user_stories = Plan::pluck('user_story', 'id');
    $real = new SpentTime;

    return view('reals.index', compact('spent_times', 'user_stories', 'real'));
}

public function store(Request $request)
{    
    $spent_time = SpentTime::findOrCreate($request->get('plan_id'), [
        'plan_id' => $request->get ('plan_id'),
        'daily_spent_time' => $request->get ('daily_spent_time'),
        'daily_percentage' => $request->get ('daily_percentage'),
        'reason' => $request->get ('reason'),
    ]);
    return redirect()->route('real.index', compact( 'spent_time'));
}