我想在我的控制器中制作我的模型的实例,并在我需要的地方使用它
我使用此代码:
public $test = new Access();
但是这个错误是我无法弄清楚为什么我总是收到此错误:
不允许将表达式作为字段默认值
这是我的控制器代码:
<?php
namespace AppHttpControllers;
use AppModelsAccess;
use IlluminateHttpRequest;
classAccessController extends Controller{
private $table = 'accesses';
public $test = new Access();
/**
* @return IlluminateContractsViewFactory|IlluminateViewView
*/
public function index()
{
$table = $this->table;
$accesses = (new Access())->index($table);
return view('index', compact('accesses'));
}
/**
* @param Request $request
* @return IlluminateHttpRedirectResponse
*/
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 IlluminateContractsViewFactory|IlluminateViewView
*/
public function edit($id)
{
$access = Access::find($id);
return view('update', compact('access', 'id'));
}
/**
* @param Request $request
* @param $id
* @return IlluminateHttpRedirectResponse
*/
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 IlluminateHttpRedirectResponse
*/
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');
}
}
我最初以为这是安全问题,但在我看来,这在我的想法中是无关紧要的
我不明白你的用例,但是..你可以使用类的构造器来做到这一点:
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();
}
}
当然你不能在PHP中调用一个方法来实例化一个类成员,即使它是一个静态方法,也不能在构造函数中初始化它
class AccessController extends Controller{
public $test ;
public function __construct()
{
$this->test= new Access();
}
}
或者更好地使用Laravel中的依赖注入模式(容器(以避免创建访问模型的多个实例,请参阅文档:https://laravel.com/docs/5.7/container