1

I'm following this answer with his pattern:

How to make a REST API first web application in Laravel

At the end my application works with these methods:

LanController

/**
     *
     * Store the data in database
     *
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|void
     */
    public function store(Request $request)
    {

        $status = $this->lan_gateway->create($request->all());
        /**
         * Status is true if insert in database is OK
         * or an array of errors
         */
        if ($status===true) {
            return redirect(route('lan'))
                ->with('success',trans('common.operation_completed_successfully'));
            // validation ok
        } else {
            return redirect(route('lan-create'))
                ->withInput()
                ->withErrors($status);
            // validation fails
        }
    }

LanGateway

/**
     * Validate input and create the record into database
     *
     * @param array $data the values to insert
     * @return array $validator errors on fails
     * @return bool $status true on success
     */
    public function create(array $data)
    {
        $validator = Validator::make($data, [
            'ip_address' => 'required|string'
        ]);

        if ($validator->fails()) {
            return $validator->errors()->all();
        } else {
            $status = $this->lan_interface->createRecord($data);
            return $status;
        }

    }

And this is the interface implemented by repository create method

<?php
/**
 * Repository for LAN object.
 * PRG paradigma, instead of "User"-like class Model
 *
 * @see https://stackoverflow.com/questions/23115291/how-to-make-a-rest-api-first-web-application-in-laravel
 */

namespace App\Repositories;
use App\Interfaces\LanInterface;
use Illuminate\Database\Eloquent\Model;


class LanRepository extends Model implements LanInterface
{

    /**
     * The name of table on database
     * @var string The table name on database
     */
    protected $table = "lans";

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = ['ip_address'];

    public function getAllRecords()
    {
        $lan = $this->all();
        return $lan;
    }

    /**
     *
     * Insert record inside database.
     *
     * @param array $data
     * @return bool true on success, false on failure
     */
    public function createRecord(array $data)
    {
        foreach ($data as $key => $value) {
            if (in_array($key,$this->fillable)) {
                $this->$key = $value;
            }
        }
        $status = $this->save();
        return $status;
    }

You can see that I "lost" the fillable helper methods, at the end I use only as "needle/haystack".

Is there another implementation possible for my createRecord method to use, as much as possible, Laravel default methods/helpers or am I in the most right direction?

Thank you very much

sineverba
  • 5,059
  • 7
  • 39
  • 84

1 Answers1

0

As you can see in documentation, $fillable property only applies when you are using mass assignment statements.

Now if if (in_array($key,$this->fillable)) { conditional check is just to check if only some allowed set of columns are saved from API, you can create another property lets say protected $allowedToInsert = ['column1', 'column2],..; and then update the conditional :

public function createRecord(array $data)
    {
        foreach ($data as $key => $value) {
            if (in_array($key,$this->allowedToInsert)) {
                $this->$key = $value;
            }
        }
        $status = $this->save();
        return $status;
    }

This way you can use fillable as it is supposed to be used, The approach you have can be the simplest one but you can improve lit of things. See this for your reference

Mihir Bhende
  • 8,677
  • 1
  • 30
  • 37