Php 在laravel中创建新的facade实例时调用函数
基本上,假设我有一个名为“窗口”的模型。我知道laravel为模型的新实例提供了created event,问题是我并不总是先创建新的数据库记录以便调用该方法,但有时我需要先创建facade的实例。我的意思是:Php 在laravel中创建新的facade实例时调用函数,php,laravel,laravel-facade,Php,Laravel,Laravel Facade,基本上,假设我有一个名为“窗口”的模型。我知道laravel为模型的新实例提供了created event,问题是我并不总是先创建新的数据库记录以便调用该方法,但有时我需要先创建facade的实例。我的意思是: $window = App\Window::create(); //this creates a new record in the //database and the 'created' event is being called in laravel, //so I can a
$window = App\Window::create(); //this creates a new record in the
//database and the 'created' event is being called in laravel,
//so I can assign some properties. Everything is ok
$window = new App\Window;//but this only creates an instance of the facade
//which is not being saved until `$window->save();` is called.
问题是,到目前为止,在我的代码中,我避免了在数据库中直接创建新的空记录,所以我使用了第二种方法。但是现在我想处理
newapp\Window
的创建,以便以编程方式为每个窗口分配自定义默认属性。有没有办法做到这一点?这应该行得通。只有在尚未传入默认值时,才会应用默认值。我还将默认值设置为常量,因此如果以后需要修改它们,很容易找到它们
const COLUMN_DEFAULT = 'some default';
const SOME_OTHER_COLUMN_DEFAULT = 'some other default';
public function __construct(array $attributes = [])
{
if (! array_key_exists('your_column', $attributes)) {
$attributes['your_column'] = static::COLUMN_DEFAULT;
}
if (! array_key_exists('some_other_column_you_want_to_default', $attributes)) {
$attributes['some_other_column_you_want_to_default'] = static::SOME_OTHER_COLUMN_DEFAULT;
}
parent::__construct($attributes);
}
这应该行得通。只有在尚未传入默认值时,才会应用默认值。我还将默认值设置为常量,因此如果以后需要修改它们,很容易找到它们
const COLUMN_DEFAULT = 'some default';
const SOME_OTHER_COLUMN_DEFAULT = 'some other default';
public function __construct(array $attributes = [])
{
if (! array_key_exists('your_column', $attributes)) {
$attributes['your_column'] = static::COLUMN_DEFAULT;
}
if (! array_key_exists('some_other_column_you_want_to_default', $attributes)) {
$attributes['some_other_column_you_want_to_default'] = static::SOME_OTHER_COLUMN_DEFAULT;
}
parent::__construct($attributes);
}
你能做到
new Window($request->all());
// Or
new Window($request->only(['foo', 'bar']));
// Or immediately create the instance
Window::create($request->all());
但是您应该首先将所需的属性添加到窗口
模型中的$filleble
属性中
class Window extends Model {
protected $fillable = ['foo', 'bar'];
}
你能做到
new Window($request->all());
// Or
new Window($request->only(['foo', 'bar']));
// Or immediately create the instance
Window::create($request->all());
但是您应该首先将所需的属性添加到窗口
模型中的$filleble
属性中
class Window extends Model {
protected $fillable = ['foo', 'bar'];
}
我想这正是我需要的。你能详细说明一下吗?我只是作为答复提交的我想这正是我需要的。你能详细解释一下吗?我只是把它作为一个答案提交的哦,上帝,谢谢!我只是在整个文档中迷路了。。。我想我应该更擅长搜索。事实上这是个坏主意,因为我认为如果使用
Window::create()
,它将忽略您的属性并转到默认值。在快速测试之后,我会想出一个更好的解决方案。哦,那将是一种不必要的行为。。。基本上,优先级应该如下:如果只创建了一个未保存的实例,请指定一些默认值,然后如果在保存之前已经更改了,请保持更改。。。创建方法也不应被忽略。提前谢谢你的帮助^^我刚刚更新了答案。我不认为这类事情会出现在文档中。这个解决方案没有什么真正的“Laravel”,它只是基本的OO编程。这看起来很神奇,但有一点是我使用了很多关系,所以使用列键并不方便。但是我也不能将这些关系作为参数传递给create
方法,所以我想这不会是一个问题。哦,天哪,谢谢!我只是在整个文档中迷路了。。。我想我应该更擅长搜索。事实上这是个坏主意,因为我认为如果使用Window::create()
,它将忽略您的属性并转到默认值。在快速测试之后,我会想出一个更好的解决方案。哦,那将是一种不必要的行为。。。基本上,优先级应该如下:如果只创建了一个未保存的实例,请指定一些默认值,然后如果在保存之前已经更改了,请保持更改。。。创建方法也不应被忽略。提前谢谢你的帮助^^我刚刚更新了答案。我不认为这类事情会出现在文档中。这个解决方案没有什么真正的“Laravel”,它只是基本的OO编程。这看起来很神奇,但有一点是我使用了很多关系,所以使用列键并不方便。但是我也不能将这些关系作为参数传递给create
方法,所以我想这不会是一个问题。