Laravel 中的配置观察器
Configuration observer in Laravel
我是 Laravel 的初学者。我在我的项目中使用 Laravel 8.
我有这个代码:
- 控制器
public function index(Request $request)
{
$query = $this->model
->orderBy($request->column ?? 'created_at', $request->order ?? 'desc');
if ($request->search) {
$query->where(function ($query) use ($request) {
$query->where('name', 'like', '%' . $request->search . '%')
->orWhere('id', 'like', '%' . $request->search . '%');
});
}
return DictionaryResource::collection($query->paginate($request->per_page));
}
public function create()
{
$statuses = DB::table('status')->select('status.name as label', 'status.id as value')->get();
$types = DB::table('dictionary_types')->select('dictionary_types.name as label', 'dictionary_types.id as value')->get();
return response()->json([$statuses, $types]);
}
public function store(DictionaryRequest $request)
{
$data = $request->only([
'name',
]);
if($request->status == 2) $status = 2;
else $status = 1;
if(is_null($request->type)) $type = 1;
else $type = $request->type;
$data['status'] = $status;
$data['type'] = $type;
$this->model->create($data);
return response()->json(['status' => 'success']);
}
- 型号
class Dictionary extends Model
{
use ScopeActiveTrait,
SoftDeletes;
protected $guarded = ['id'];
protected $fillable = [
'name',
'type',
'status'
];
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
}
- 观察者
class DictionaryObserver
{
public function created(Dictionary $dictionary)
{
Log::info('yyyyyyyyy');
}
public function retrieved(Dictionary $dictionary)
{
Log::info('xxxxxxxxxx'.$dictionary);
}
public function updated(Dictionary $dictionary)
{
//
}
public function deleted(Dictionary $dictionary)
{
//
}
}
- 服务提供商
public function boot()
{
Paginator::useBootstrap();
Dictionary::observe(DictionaryObserver::class);
}
我有 2 个问题:
如何在控制器中禁用跟随(索引方法)?我只需要记录有人打开一条记录进行编辑的瞬间,并没有列出列表中的所有记录
我有模型动作:
class Action extends Model
{
use ScopeActiveTrait,
SoftDeletes;
protected $guarded = ['id'];
protected $fillable = [
'company_id',
'user_id',
'ip',
'user_agent',
'description'
];
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
}
我需要将有关用户 ip 的信息保存到此模型,user_agent itp(用户已登录)。
我怎样才能做到?
正如您所发现的,当您加载模型实例时,无论您加载一个还是多个(如果您加载多个,它都会为每个加载的模型调用一次)在加载模型实例时被调用。
您可以通过使用 ::withoutEvents() 静态方法将其包装在回调函数中来抑制触发事件(并且,经过测试,这包括事件和观察者)。
所以(使用我的网站之一的代码)如果我使用:
$games = Game::where('id', '>=', 4900)->where('id', '<=', 4910)->get();
那么 GameObserver 将被调用 11 次(因为加载了 11 个模型)。但是,如果我像这样将它包装在 ::withoutEvents 方法中:
$games = Game::withoutEvents(function () {
$games = Game::where('id', '>=', 4900)->where('id', '<=', 4910)->get();
return $games;
});
我是 Laravel 的初学者。我在我的项目中使用 Laravel 8.
我有这个代码:
- 控制器
public function index(Request $request)
{
$query = $this->model
->orderBy($request->column ?? 'created_at', $request->order ?? 'desc');
if ($request->search) {
$query->where(function ($query) use ($request) {
$query->where('name', 'like', '%' . $request->search . '%')
->orWhere('id', 'like', '%' . $request->search . '%');
});
}
return DictionaryResource::collection($query->paginate($request->per_page));
}
public function create()
{
$statuses = DB::table('status')->select('status.name as label', 'status.id as value')->get();
$types = DB::table('dictionary_types')->select('dictionary_types.name as label', 'dictionary_types.id as value')->get();
return response()->json([$statuses, $types]);
}
public function store(DictionaryRequest $request)
{
$data = $request->only([
'name',
]);
if($request->status == 2) $status = 2;
else $status = 1;
if(is_null($request->type)) $type = 1;
else $type = $request->type;
$data['status'] = $status;
$data['type'] = $type;
$this->model->create($data);
return response()->json(['status' => 'success']);
}
- 型号
class Dictionary extends Model
{
use ScopeActiveTrait,
SoftDeletes;
protected $guarded = ['id'];
protected $fillable = [
'name',
'type',
'status'
];
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
}
- 观察者
class DictionaryObserver
{
public function created(Dictionary $dictionary)
{
Log::info('yyyyyyyyy');
}
public function retrieved(Dictionary $dictionary)
{
Log::info('xxxxxxxxxx'.$dictionary);
}
public function updated(Dictionary $dictionary)
{
//
}
public function deleted(Dictionary $dictionary)
{
//
}
}
- 服务提供商
public function boot()
{
Paginator::useBootstrap();
Dictionary::observe(DictionaryObserver::class);
}
我有 2 个问题:
如何在控制器中禁用跟随(索引方法)?我只需要记录有人打开一条记录进行编辑的瞬间,并没有列出列表中的所有记录
我有模型动作:
class Action extends Model
{
use ScopeActiveTrait,
SoftDeletes;
protected $guarded = ['id'];
protected $fillable = [
'company_id',
'user_id',
'ip',
'user_agent',
'description'
];
protected $dates = [
'deleted_at',
'created_at',
'updated_at'
];
}
我需要将有关用户 ip 的信息保存到此模型,user_agent itp(用户已登录)。 我怎样才能做到?
正如您所发现的,当您加载模型实例时,无论您加载一个还是多个(如果您加载多个,它都会为每个加载的模型调用一次)在加载模型实例时被调用。
您可以通过使用 ::withoutEvents() 静态方法将其包装在回调函数中来抑制触发事件(并且,经过测试,这包括事件和观察者)。
所以(使用我的网站之一的代码)如果我使用:
$games = Game::where('id', '>=', 4900)->where('id', '<=', 4910)->get();
那么 GameObserver 将被调用 11 次(因为加载了 11 个模型)。但是,如果我像这样将它包装在 ::withoutEvents 方法中:
$games = Game::withoutEvents(function () {
$games = Game::where('id', '>=', 4900)->where('id', '<=', 4910)->get();
return $games;
});