laravel控制器新方法全局变量

时间:2018-10-24 18:02:13

标签: php laravel eloquent object-oriented-analysis

我想在控制器中创建模型实例,并在需要的地方使用它

我使用以下代码:

public $test = new Access();

但是这个错误使我无法弄清楚为什么我一直收到此错误:

  

不允许将表达式作为字段默认值

这是我的控制器代码,在这里:

<?php
namespace App\Http\Controllers;
 use App\Models\Access;
use Illuminate\Http\Request;
classAccessController extends Controller{
private $table = 'accesses';
public $test = new Access();
/**
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function index()
{
    $table = $this->table;
    $accesses = (new  Access())->index($table);
    return view('index', compact('accesses'));
}


/**
 * @param Request $request
 * @return \Illuminate\Http\RedirectResponse
 */
public function store(Request $request)
{

    $access = new Access();
    $request->validate([
        'can_select' => 'required|boolean',
        'can_delete' => 'required|boolean',
        'can_edit' => 'required|boolean',
        'can_insert' => 'required|boolean',
        'role_id' => 'required|integer|max:2',
        'module_id' => 'required|integer|max:3',
    ]);

    $access->can_select = $request->get('can_select');
    $access->can_delete = $request->get('can_delete');
    $access->can_edit = $request->get('can_edit');
    $access->can_insert = $request->get('can_insert');
    $access->role_id = $request->get('role_id');
    $access->module_id = $request->get('module_id');


    $attributes = array('can_select', 'can_delete', 'can_edit', 'can_insert', 'role_id', 'module_id');
    $options = array($access->can_select, $access->can_delete, $access->can_edit, $access->can_insert, $access->role_id, $access->module_id);
    $table = $this->table;
    (new Access())->store($table, $attributes, $options);
    return redirect('accesses')->with('success', 'Information has been  inserted');
}

/**
 * @param $id
 * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
 */
public function edit($id)
{
    $access = Access::find($id);
    return view('update', compact('access', 'id'));
}

/**
 * @param Request $request
 * @param $id
 * @return \Illuminate\Http\RedirectResponse
 */
public function update(Request $request, $id)
{
    $access = Access::find($id);
    $request->validate([
        'can_select' => 'required|boolean',
        'can_delete' => 'required|boolean',
        'can_edit' => 'required|boolean',
        'can_insert' => 'required|boolean',
        'role_id' => 'required|integer|max:2',
        'module_id' => 'required|integer|max:3',
    ]);
    $access->can_select = $request->get('can_select');
    $access->can_delete = $request->get('can_delete');
    $access->can_edit = $request->get('can_edit');
    $access->can_insert = $request->get('can_insert');
    $access->role_id = $request->get('role_id');
    $access->module_id = $request->get('module_id');

    $attributes = array('can_select', 'can_delete', 'can_edit', 'can_insert', 'role_id', 'module_id');
    $options = array($access->can_select, $access->can_delete, $access->can_edit, $access->can_insert, $access->role_id, $access->module_id);
    $object = $access;
    $table = $this->table;
    (new  Access())->updates($table, $object, $attributes, $options);
    return redirect('accesses')->with('success', 'Information has been updated successfully!!');
}

/**
 * @param $id
 * @return \Illuminate\Http\RedirectResponse
 */
public function destroy($id)
{
    $access = Access::find($id);
    $object = $access;
    $table = $this->table;
    (new Access())->erase($table, $object);
    return redirect('accesses')->with('success', 'Information has been  deleted');
}

}

我首先以为这是安全问题,但在我看来,这是我的主意

2 个答案:

答案 0 :(得分:2)

我不了解您的用例,但是..您可以使用类的构造函数来这样做:

class ACoolController extends Controller {

    protected $access;

    /**
     * ACoolController constructor.
     *
     */
    public function __construct()
    {
        $this->access = new Access();
    }

    public function aCoolFunction()
    {
        // do something with your variable
        $this->access->someMethodOfYourModel();
    }
}

答案 1 :(得分:0)

当然,在PHP中,您不能调用方法来实例化类成员,即使它是静态方法,也可以在构造函数中对其进行初始化

class AccessController extends Controller{
    public $test ;
    public function __construct()
    {
      $this->test= new Access();
    }
}

或者更好地使用Laravel中的依赖项注入模式(Container)来避免创建访问模型的多个实例,请参阅文档:https://laravel.com/docs/5.7/container