mercredi 14 décembre 2016

Insert on duplicate key update with laravel 5.1 eloquent (only one query)

I wan to perform insert on duplicate key update with eloquent model without making two queries ,the problem is when i use updateOrCreate() it does two queries

 /**
 * Create or update a record matching the attributes, and fill it with     values.
 *
 * @param  array  $attributes
 * @param  array  $values
 * @return static
 */
public static function updateOrCreate(array $attributes, array $values = [])
{
    $instance = static::firstOrNew($attributes);
    //seconde querie
    $instance->fill($values)->save();

    return $instance;
}
public static function firstOrNew(array $attributes)
{
    //first query
    if (! is_null($instance = (new static)->newQueryWithoutScopes()->where($attributes)->first())) {
        return $instance;
    }

    return new static($attributes);
}

thanks :)



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire