Php \u save()之前的Laravel管理员未启动
我正在从sentry获取用户id,在创建或更新记录时,我也需要将用户id更新到帖子中。我正在配置上使用before_save()回调。Php \u save()之前的Laravel管理员未启动,php,laravel-4,Php,Laravel 4,我正在从sentry获取用户id,在创建或更新记录时,我也需要将用户id更新到帖子中。我正在配置上使用before_save()回调。Sentry::getUser()->id工作正常(我检查了routes上的函数,它正确地提供了user_id)。通过管理员创建帖子时,未更新用户id,因此出现错误。 /** * app/config/administrator/posts.php */ return array( 'title' => 'Posts', 'sing
Sentry::getUser()->id
工作正常(我检查了routes上的函数,它正确地提供了user_id)。通过管理员创建帖子时,未更新用户id,因此出现错误。
/**
* app/config/administrator/posts.php
*/
return array(
'title' => 'Posts',
'single' => 'post',
'model' => 'Post',
/**
* The display columns
*/
'columns' => array(
'user' => array(
'title' => 'User',
'relationship' => 'user',
'select' => "(:table).email",
),
'category' => array(
'title' => 'Category',
'relationship' => 'category',
'select' => "(:table).name",
),
'pricing' => array(
'title' => 'Pricing',
'relationship' => 'pricing',
'select' => "(:table).name",
),
'name' => array(
'title' => 'Name',
'select' => "name",
),
'title' => array(
'title' => 'Title',
'select' => "title",
),
'description' => array(
'title' => 'Description',
'select' => "description",
),
'order' => array(
'title' => 'Order',
'select' => "order_value",
),
'active_date' => array(
'title' => 'Start',
'select' => "active_date",
),
'expiry_date' => array(
'title' => 'End',
'select' => "expiry_date",
),
'status' => array(
'title' => 'Status',
'select' => "status",
)
),
/**
* The filter set
*/
// 'filters' => array(
// 'name' => array(
// 'title' => 'Name',
// ),
// 'description' => array(
// 'title' => 'Description',
// 'type' => "text",
// )
// ),
'before_save' => function(&$data)
{
$data['user_id'] = Sentry::getUser()->id;
// return $data;
},
/**
* The editable fields
*/
'edit_fields' => array(
'category' => array(
'title' => 'Category',
'type' => 'relationship',
'name_field' => "name",
),
'pricing' => array(
'title' => 'Pricing',
'type' => 'relationship',
'name_field' => "name",
),
'name' => array(
'title' => 'Name',
'type' => "text",
),
'title' => array(
'title' => 'Title',
'type' => "text",
),
'description' => array(
'title' => 'Description',
'type' => "textarea",
),
'active_date' => array(
'title' => 'Start',
'type' => "date",
),
'expiry_date' => array(
'title' => 'End',
'type' => "date",
),
'status' => array(
'title' => 'Status',
'type' => "bool",
)
),
);
假设这是管理员的错误或我的无知,我用另一种方法解决了这个问题。在posts模型中,我创建了用于创建和更新posts的事件:
public static function boot()
{
parent::boot();
static::creating(function($post)
{
$post->user_id = Sentry::getUser()->id;
});
static::updating(function($post)
{
$post->user_id = Sentry::getUser()->id;
});
}