Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通用模型方法的正确用法_Php_Laravel_Laravel 5.2 - Fatal编程技术网

Php 通用模型方法的正确用法

Php 通用模型方法的正确用法,php,laravel,laravel-5.2,Php,Laravel,Laravel 5.2,我是拉雷维尔的新手,正在努力了解基本用法 目前,我的控制器中有以下与术语/分类模型相关的代码 $taxonomy = Taxonomy::where('slug', '=', str_slug('brand'))->first(); if ( isset($taxonomy->id) ) { $term = $taxonomy->term()->firstOrCreate(array('slug' => str_slug($request->name)

我是拉雷维尔的新手,正在努力了解基本用法

目前,我的控制器中有以下与术语/分类模型相关的代码

$taxonomy = Taxonomy::where('slug', '=', str_slug('brand'))->first();

if ( isset($taxonomy->id) ) {

  $term = $taxonomy->term()->firstOrCreate(array('slug' => str_slug($request->name)));

  $term->name = $request->name;

  $term->save();
}
我想让其他控制器使用它,但不确定正确的位置

我曾考虑将它放在“addTerm”方法下的分类模型中,但Taxonomy::where()感觉不太合适


正确的方法是什么?它应该放在分类模型中还是放在方法的帮助文件中?

在我看来,这不是唯一的方法。我喜欢使用repository模式,并将与sql相关的方法存储在那里,以便在整个应用程序中重用它们


例如,这里对它进行了解释,我发现这篇文章非常有用/有趣:

当我自己解决这个问题时,我使用了helper类。我想让我的模型专注于数据结构,并在多个控制器之间共享某些活动

下面的代码是处理邮件地址的示例。用例是其他几个实体将与一个或多个地址关联。所有权通过多对多关系的中间表进行管理

(*这是我第一次接触到存储库模式,它看起来非常吸引人。我将进一步探讨它。)

app/Helpers/Models/Address.php
我见过存储库名称好几次,所以我会看看这些名称,看看我是否能自己使用它。非常感谢!将提供最新信息。
<?php

namespace App\Helpers\Models;

use Illuminate\Validation\Factory as Validator;
use App\Models\Address as AddressModel;
use App\Exceptions\ValidatorException;

/**
 * Class Address
 *
 * @todo descriptive error messages
 * @package App\Helpers\Models
 */
class Address
{
    static $validation_rules = [
        'street_number' => 'required|string|max:255',
        'street_name' => 'string|max:255',
        'unit' => 'string|max:255',
        'city' => 'required|string|max:255',
        'state' => 'required|string|max:255',
        'postal_code' => 'required|string|max:10'
    ];

    static $validation_messages = [
        'street_number.required' => 'A street address is required.',
        'street_number.string' => 'A street address is required.',
        'street_number.max' => 'A street address is required.',
        'street_name.string' => 'A street address is required.',
        'street_name.max' => 'A street address is required.',
        'unit.string' => 'A street address is required.',
        'unit.max' => 'A street address is required.',
        'city.required' => 'A city is required.',
        'city.string' => 'A street address is required.',
        'city.max' => 'A street address is required.',
        'state.required' => 'A state is required.',
        'state.string' => 'A state is required.',
        'state.max' => 'A state is required.',
        'postal_code.required' => 'A zip code is required.',
        'postal_code.string' => 'A zip code is required.',
        'postal_code.max' => 'A zip code is required.'
    ];


    public static function store(array $data, array $options = array())
    {
        $validator = Validator::make($data, self::$validation_rules, self::$validation_messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator);
        }

        $address = new AddressModel();

        $address->fill($data);

        return $address;
    }

    public static function update(array $data, array $options = array())
    {
        // TODO: Implement update() method.
    }

    public static function destroy($id, array $options = array())
    {
        // TODO: Implement destroy() method.
    }
}
<?php

namespace App\Exceptions;

use \Exception;

class ValidatorException extends Exception
{
    protected $validator;

    // Redefine the exception so validator is captured
    public function __construct($validator, $message = null, $code = 0, Exception $previous = null) {
        // some code
        $this->validator = $validator;

        if (!$message) {
            $message = join(' ', $validator->getMessageBag()->all());
        }

        // make sure everything is assigned properly
        parent::__construct($message, $code, $previous);
    }

    public function getValidator()
    {
        return $this->validator;
    }
}