Laravel:观察者和服务提供者之间的映射

时间:2018-04-16 09:28:09

标签: php laravel-5

我创建了一个Observer。但是当ItemController中的save()事件触发时,ItemObserver的创建事件不起作用。

我错了什么可以帮助我?

在此代码中,ItemObserver用于在保存事件触发时大写Item模型的名称字段。

>app
 >Observers
  >ItemObserver.php
 >Providers
  >ItemModelServiceProvider.php
 >Http
  >Controllers
   >ItemController.php
 >Models
  >Item.php

ItemObserver.php

namespace App\Observers;

use App\Models\Item;

class ItemObserver
{
    /**
     * Accept Event And Done with Model
     *
     * @param Model $item
     * @return void
     */
    public function creating(Item $item)
    {
        $item->name = strtoupper($item->name);
    }
}

ItemModelServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Models\Item;
use App\Observers\ItemObserver;

class ItemModelServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application services.
     *
     * @return void
     */
    public function boot()
    {
        Item::observe(ItemObserver::class);
    }

    /**
     * Register the application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

ItemController.php

namespace App\Http\Controllers;

use App\Models\Item;
use Illuminate\Http\Request;

class ItemController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $items = \App\Models\Item::orderBy('id', 'desc')->get();

        return view('items/list', compact('items'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('items/create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $item = new Item;
        $item->name = $request->get('name');
        $item->price = $request->get('price');
        $item->save();

        \Session::flash('msg', 'Item has been saved.');

        return redirect('items')->with('msg', 'Item has been saved.');
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function show(Item $item)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function edit(Item $item)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Item $item)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Item  $item
     * @return \Illuminate\Http\Response
     */
    public function destroy(Item $item)
    {
        //
    }
}

Item.php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Item extends Model
{

}

1 个答案:

答案 0 :(得分:0)

创建自定义服务提供商后,需要在 config / app.php 文件中注册。

'providers' => [

    ...
    App\Providers\ItemModelServiceProvider::class,

],

如果在创建更新时更改了模型属性值,则可以使用mutators并定义如下:

class Item extends Model
{

    public function setNameAttribute($value)
    {
        $this->attributes['name'] = strtolower($value);
    }
}