updateOrCreate Laravel Eloquent在foreach问题中?

时间:2018-05-19 22:04:17

标签: php laravel foreach eloquent

所以我试图在updateOrCreate语句中使用foreach Eloquent方法,但它正在创建重复的新模型,而不是更新现有模型。

这是我的控制器的一部分:

$user = Auth::user()->id;

    $providers = Providers::where('user_id', $user)->where('provider_status', 0)->get(['provider_id']);

    //dd($providers);

    //Months

    $now = Carbon::now()->setTimezone('America/Costa_Rica');;

    $currentYear = $now->copy()->year;

    foreach($providers as $provider) {

        //January

        //Month
        $janFrom = $now->copy()->month(1)->startOfMonth();
        $janTo = $now->copy()->month(1)->endOfMonth();

        $janPriceMonthTotal = Planillas::where('user_id', $user)->where('status', 0)->where('deleted', 0)->where('provider', $provider->provider_id)->whereBetween('date', [$janFrom, $janTo])->sum('price');

        //Fortnights
        //First
        $janFromFortnightFirst = $janFrom->copy();
        $janToFortnightFirst = $janFromFortnightFirst->copy()->addDays(14);

        $janPriceFortnightFirst = Planillas::where('user_id', $user)->where('status', 0)->where('deleted', 0)->where('provider', $provider->provider_id)->whereBetween('date', [$janFromFortnightFirst, $janToFortnightFirst])->sum('price');
        $janExpensesFortnightFirst = Planillas::where('user_id', $user)->where('status', 0)->where('deleted', 0)->where('provider', $provider->provider_id)->whereBetween('date', [$janFromFortnightFirst, $janToFortnightFirst])->sum('travel_expenses');

        //Last
        $janFromFortnightLast = $janFromFortnightFirst->copy()->addDays(15);
        $janToFortnightLast = $janTo->copy();

        $janPriceFortnightLast = Planillas::where('user_id', $user)->where('status', 0)->where('deleted', 0)->where('provider', $provider->provider_id)->whereBetween('date', [$janFromFortnightLast, $janToFortnightLast])->sum('price');
        $janExpensesFortnightLast = Planillas::where('user_id', $user)->where('status', 0)->where('deleted', 0)->where('provider', $provider->provider_id)->whereBetween('date', [$janFromFortnightLast, $janToFortnightLast])->sum('travel_expenses');

        $janEventCount = Planillas::where('user_id', $user)->where('status', 0)->where('deleted', 0)->where('provider', $provider->provider_id)->whereBetween('date', [$janFrom, $janTo])->count();

        //dd($janEventCount);
        //Update if exists or create if doesn't
        $janMonth = ProviderReport::updateOrCreate([
            'user_id' => $user,
            'status' => 1,
            'month' => 'jan',
            'year' => $currentYear,
            'provider_id' => $provider->provider_id,
        ],
            [
                'monthly_total' => $janPriceMonthTotal,
                'first_fortnight' => $janPriceFortnightFirst,
                'second_fortnight' => $janPriceFortnightLast,
                'expenses_first' => $janExpensesFortnightFirst,
                'expenses_last' => $janExpensesFortnightLast,
                'event_count' => $janEventCount,
            ]);
    }

我的模特:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProviderReport extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'month',
        'year',
        'first_fortnight',
        'second_fortnight',
        'monthly_total',
        'expenses_first',
        'expenses_last',
        'provider_id',
        'event_count',
    ];
}

问题是代码第一次运行,而且我的表中没有数据,那么它创建得很好,但是当我再次运行代码时(假设我改变了event_count之类的东西,那么创建重复项而不是更新我的行。

提前感谢您的时间。

1 个答案:

答案 0 :(得分:0)

好的,所以我发现了错误,并且在我的模型上...对不起我问,但无论如何,对于有这个问题的任何人。

我的模特是:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProviderReport extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'user_id',
        'month',
        'year',
        'first_fortnight',
        'second_fortnight',
        'monthly_total',
        'expenses_first',
        'expenses_last',
        'provider_id',
        'event_count',
    ];
}

修正:

<?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class ProviderReport extends Model
    {
        /**
         * The attributes that are mass assignable.
         *
         * @var array
         */
        protected $fillable = [
            'user_id',
            'month',
            'year',
            'first_fortnight',
            'second_fortnight',
            'monthly_total',
            'expenses_first',
            'expenses_last',
            'provider_id',
            'event_count',
            'status', // i forgot to add this
        ];
    }

但我忘记将'status'添加到我的$ fillable中,状态在我的表中的默认值为0,所以当updateOrCreate检查我的模型是否存在时,从未找到,因为默认情况下,新创建的行将'status'设置为零,并且该列不在模型中的$ fillable中列出。