<?php

namespace App\Classes\General\Eloquent;

use App\Classes\Exceptions\MalformedRequestException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\QueryException;

abstract class AbstractUpdateRelationshipRecord
{


    /**
     * @param HasOneOrMany $relation
     * @param Model $model
     * @return Model
     * @throws MalformedRequestException
     */
    public function handler(HasOneOrMany $relation, Model $model){
        try{
            if($relation->save($model)){
                return $model;
            }

        } catch (QueryException $exception){
            throw new MalformedRequestException($exception->getMessage());
        }
    }



}