खोज…


प्रेक्षक बनाना

लारवेल में एक निश्चित मॉडल के लाइव साइकिल कॉलबैक को सुनने के लिए पर्यवेक्षकों का उपयोग किया जाता है।
ये श्रोता निम्नलिखित में से किसी भी क्रिया को सुन सकते हैं:

  • बनाना
  • बनाया था
  • अद्यतन करने
  • अद्यतन
  • बचत
  • बचाया
  • हटाने
  • हटाए गए
  • बहाल
  • बहाल

यहाँ एक पर्यवेक्षक का एक उदाहरण है।

UserObserver

<?php

namespace App\Observers;

/**
 * Observes the Users model
 */
class UserObserver 
{
    /**
     * Function will be triggerd when a user is updated
     *
     * @param Users $model
     */
     public function updated($model)
     {
         // execute your own code
     }
}

जैसा कि उपयोगकर्ता पर्यवेक्षक में दिखाया गया है, हम अद्यतन कार्रवाई को सुनते हैं, हालांकि इससे पहले कि यह वर्ग वास्तव में उपयोगकर्ता मॉडल को सुनता है हमें सबसे पहले इसे EventServiceProvider अंदर पंजीकृत करना EventServiceProvider

EventServiceProvider

<?php 

namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

use App\Models\Users;
use App\Observers\UserObserver;

/**
 * Event service provider class
 */
class EventServiceProvider extends ServiceProvider
{
    /**
     * Boot function
     *
     * @param DispatcherContract $events
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        // In this case we have a User model that we want to observe
        // We tell Laravel that the observer for the user model is the UserObserver
        Users::observe(new UserObserver());
    }
}

अब जब हमने अपने पर्यवेक्षक को पंजीकृत कर लिया है, तो उपयोगकर्ता मॉडल को सहेजने के बाद हर बार अद्यतन फ़ंक्शन को बुलाया जाएगा।



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow