Wiz0TM

Wiz0TM

Member Since 5 Days Ago

Experience Points 150
Experience Level 1

4,850 experience to go until the next level!

In case you were wondering, you earn Laracasts experience when you:

  • Complete a lesson — 100pts
  • Create a forum thread — 50pts
  • Reply to a thread — 10pts
  • Leave a reply that is liked — 50pts
  • Receive a "Best Reply" award — 500pts
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Reply
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

18 Jun
7 hours ago

Wiz0TM left a reply on Eloquent CRUD Logging Solution

@AUDUNRU - Thanks for taking the time to try and replicate the issue, and for the long explanation. FYI, I tried to implement it my way, because sometimes, the information I want to log is all in the model, so in those cases, it's rather convenient to just override the single accessor in the model. This part works fine. However, sometimes, the info I want to log is actually in the controller (esp. when a log event triggered by a single model requires info from related models to be logged as well, and this legacy app doesn't always have the relationships defined in the models.), hence my attempt to pass the payload as an attribute from the controller. This part works for update, but fails for create. I'm just trying to justify my reasons for implementing it my way. I see your point in suggesting observers as it separates the logging logic from the models and controllers, but that very separation can be a bit of a pain. I now need to figure out a good way to pass the relevant info from the controller to the observer implementation. Do you see what I mean? Of course, in some cases, the info from related models can be retrieved anew from the database, but wouldn't it have been convenient and more efficient if I could have just passed in the info from the controller where the info has already been loaded is already present... I still wish somebody could've figured out what the real issue is with the create, but I truly appreciate the time and effort you put into this. You were also the only one who responded to this discussion, so thanks! :-) I'll accept your answer :-)

16 Jun
2 days ago

Wiz0TM left a reply on Hoping For Pointers On CRUD Logging...

@BOBBYBOUWMANN - But the current method is also supposed to pass in the current model, but somehow, the payload is not set (only on created). Any idea why?

Wiz0TM started a new conversation Hoping For Pointers On CRUD Logging...

I posted a question a few days back, but I've yet to receive much participation from users. The main issue remains unresolved, while I haven't really gotten much constructive criticism, except from one user. Does everybody think I should have gone the observer path? Pointers appreciated...

Thanks everyone! :-)

14 Jun
4 days ago

Wiz0TM left a reply on Eloquent CRUD Logging Solution

@AUDUNRU - Well, I did try doing print_r($request['payload']); die; and it did print the array. This is similar to other legacy code in the app...

12 Jun
6 days ago

Wiz0TM left a reply on Eloquent CRUD Logging Solution

@AUDUNRU - Ah, as for that, let me clarify... I only needed the accessor so I can override it in a derived model to return a different payload from the parent model, but specific to the derived model. I can get rid of the mutator altogether and directly set the payload using the member variable, but it seemed nice to refer to it as payload instead of event_payload. One method of setting the payload is to override the accessor. The other method, which is failing to work for create() is setting the payload attribute directly, from the controller, as sometimes it's necessary to log additional info which the controller is holding.

Wiz0TM left a reply on Eloquent CRUD Logging Solution

@AUDUNRU - I'm not sure I understand... What transformations do I need to do?

Wiz0TM started a new conversation Eloquent CRUD Logging Solution

I have tried to create an Eloquent CUD logging silution for a legacy app. I'm stuck with Laravel 5.4.* due to legacy reasons and consequent policy decision. I tried to implement a part of the solution using Model events. Let me present some of the code...

<?php

namespace App\Traits;

use Illuminate\Support\Facades\Auth;
use App\UILog;

trait LoggingTrait
{
    protected function ui_log($source, $event, $payload, $json = TRUE)
    {
        $r = FALSE;
        $request = request();

        if($json)
            $payload = json_encode($payload);

        $new = [
            'created_by' => Auth::user()->name,
            'source' => $source,
            'event' => $event,
            'payload' => $payload
        ];

        try {
            $ui_log = UILog::create($new);
            $r = TRUE;
        } catch(\Exception $e) {
            $request->session()->flash('error_message', 'Error writing to UI Log: ' . $e->getMessage());
        }

        return $r;
    }
}

<?php

namespace App\Events;

use Illuminate\Queue\SerializesModels;

abstract class ModelEvent
{
    use SerializesModels;

    public $event_name;
    public $model_name;
    public $payload;

    /**
     * Create a new event instance.
     *
     * @param  string  $event_name
     * @param  string  $model_name
     * @param  string  $payload
     * @return void
     */
    public function __construct($event_name, $model_name, $payload)
    {
        $this->event_name = $event_name;
        $this->model_name = substr(strrchr($model_name, '\'), 1);
        $this->payload = $payload;
    }
}

<?php

namespace App\Events;

use App\LoggingModel;

class ModelCreated extends ModelEvent
{
    /**
     * Create a new event instance.
     *
     * @param  LoggingModel  $model
     * @return void
     */
    public function __construct(LoggingModel $model)
    {
        parent::__construct('Created', $model->model_name, $model->payload);
    }
}

<?php

namespace App\Listeners;

use App\Events\ModelEvent;
use App\Traits\LoggingTrait;

class LoggingModelEventSubscriber
{
    use LoggingTrait;

    /**
     * Handle LoggingModel events.
     *
     * @param  \App\Events\ModelEvent\ModelEvent  $events
     */
    public function handleLoggingModelEvent(ModelEvent $event) {
        $this->ui_log($event->model_name, $event->event_name, $event->payload);
    }

    /**
     * Register the listeners for the subscriber.
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {
        $events->listen(
            'App\Events\ModelCreated',
            static::class . '@handleLoggingModelEvent'
        );

        $events->listen(
            'App\Events\ModelDeleted',
            static::class . '@handleLoggingModelEvent'
        );

        $events->listen(
            'App\Events\ModelSaved',
            static::class . '@handleLoggingModelEvent'
        );

        $events->listen(
            'App\Events\ModelUpdated',
            static::class . '@handleLoggingModelEvent'
        );
    }
}

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Notifications\Notifiable;
use App\Events\ModelCreated;
use App\Events\ModelDeleted;
use App\Events\ModelSaved;
use App\Events\ModelUpdated;

abstract class LoggingModel extends Model
{
    use Notifiable;

    /**
     * The event map for the model.
     *
     * @var array
     */
    protected $events = [
        'created' => ModelCreated::class,
        'deleted' => ModelDeleted::class,
        'saved' => ModelSaved::class,
        'updated' => ModelUpdated::class,
    ];

    public $model_name;

    public $event_payload;

    /**
     * Create a new model instance.
     *
     * @return void
     */
    public function __construct(array $attributes = [])
    {
        parent::__construct($attributes);
        $this->model_name = get_class($this);
    }

    /**
     * Mutator for payload attribute.
     *
     * @param  array  $value
     * @return void
     */
    protected function setPayloadAttribute($value)
    {
        $this->event_payload = $value;
    }

    /**
     * Overridable. Accessor for payload attribute.
     *
     * @return array
     */
    protected function getPayloadAttribute()
    {
        return $this->event_payload ?? ['id' => $this->id];
    }
}

The thing is, this works fine for all of the cases where I'm setting the payload by overriding in derived models only, or the case where I update after setting the payload attribute in the controller before model save(). But when I try to set the payload when calling create () in controller, only the base model's payload is logged, not the value I'm passing to create(). By all accounts, based on the docs and online examples and tutorials, the create() should set the payload then trigger the proper events. However this apparently isn't working as expected (by me). If this issue is specific to older versions of Laravel, I'd like to know. Anyhow, any pointers are welcome...

$request['payload'] = ['country' => 'UK'];

$item = Service::on('connection_a')->create($request->only('some_field_1', 'some_field_2', 'payload'));

Note: I'm not supposed to mess with the legacy code too much, esp. the controllers. I'd like to keep my interventions minimally invasive there. Anyhow, since I've already written my new code this way, I'd like to see if I can get this working with as little modifications as possible, while not missing any glaring pitfalls.

Thanks everyone :-)