HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/stafa/werkenbijstafa.nl/app/Komma/Forms/FormService.php
<?php
/**
 * Created by PhpStorm.
 * User: mike
 * Date: 28/09/17
 * Time: 15:58
 */

namespace App\Komma\Forms;


use App\Http\Requests\Request;
use Carbon\Carbon;
use Illuminate\Foundation\Http\FormRequest;

final class FormService
{
    /**
     * Origin from where the request was done
     * f.e. 'contact','offer'
     *
     * @var
     */
    protected $origin;

    /**
     * Set the origin of the form request
     *
     * @param $origin
     */
    public function setOrigin($origin)
    {
        $this->origin = $origin;
    }

    /**
     * Store the request in the database
     *
     * @param FormRequest $request
     */
    public function storeRequest(FormRequest $request)
    {
        $storeRequest = $request->duplicate();
        $storeRequest = $storeRequest->except('_token', '_willie');

        foreach ($storeRequest as $name => $value) {
            if ( ! \Schema::hasColumn('requests', $name)) {
                \Schema::table('requests', function ($table) use ($name) {
                    $table->text($name)->nullable();
                });
            }
        }

        $dbRequest = new \App\Komma\Forms\Models\Request();
        $dbRequest->origin = $this->origin;
        $dbRequest->created_at = Carbon::now()->toDateTimeString();
        $dbRequest->updated_at = Carbon::now()->toDateTimeString();
        foreach ($storeRequest as $name => $value) {
            $dbRequest->$name = $value;
        }
        $dbRequest->save();
    }
}