Laravel和存储库/网关模式。我是否因未来的助手而失去了可填写的财产?

问题描述 投票:0回答:1

我正在按照他的模式回答这个问题:

How to make a REST API first web application in Laravel

最后,我的应用程序使用这些方法:

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;
        }

    }

这是由存储库create方法实现的接口

<?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;
    }

你可以看到我“丢失”了可填写的辅助方法,最后我只使用“needle / haystack”。

是否有另一种实现可以让我的createRecord方法尽可能地使用Laravel默认方法/帮助器,还是我处于最正确的方向?

非常感谢你

laravel repository-pattern
1个回答
0
投票

正如您在documentation中看到的那样,$ fillable属性仅适用于您使用批量赋值语句时。

现在,如果if (in_array($key,$this->fillable)) {条件检查只是检查是否只从API保存了一些允许的列,您可以创建另一个属性,让我们说protected $allowedToInsert = ['column1', 'column2],..;然后更新条件:

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

这样你可以使用fillable,因为它应该被使用,你拥有的方法可能是最简单的方法,但你可以改善点亮的东西。请参阅this以供参考

© www.soinside.com 2019 - 2024. All rights reserved.