Wiz0TM

Wiz0TM

Member Since 4 Months Ago

Experience Points
270
Total
Experience

4,730 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-engines Created with Sketch.

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • first-thousand Created with Sketch.

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • 1-year Created with Sketch.

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • 2-years Created with Sketch.

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • 3-years Created with Sketch.

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • 4-years Created with Sketch.

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • 5-years Created with Sketch.

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • school-session Created with Sketch.

    School In Session

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

  • welcome-newcomer Created with Sketch.

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • full-time-student Created with Sketch.

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • pay-it-forward Created with Sketch.

    Pay It Forward

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

  • subscriber-token Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer-token Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • lara-evanghelist Created with Sketch.

    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 Created with Sketch.

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • lara-veteran Created with Sketch.

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • 10k-strong Created with Sketch.

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • lara-master Created with Sketch.

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • laracasts-tutor Created with Sketch.

    Laracasts Tutor

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

  • laracasts-sensei Created with Sketch.

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • top-50 Created with Sketch.

    Top 50

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

Level 1
270 XP
Jun
22
3 months ago
Activity icon

Replied to Hoping For Some Assistance With A Possible Laravel Bug...

@TRAY2 - Thanks, I appreciate the input, but I'm not supposed to touch the DB at this point, as it's a live system. I was aware of triggers but the idea of using triggers for this purpose didn't previously occur to me, possibly because when I was assigned this task, initially two possibilities were suggested. Either logging to a text file or logging to the DB or possibly both. So guess I was thinking along those lines... But thanks for reminding me :-)

EDIT: FYI: Someone else is taking care of creating the log table for me...

Activity icon

Replied to Hoping For Some Assistance With A Possible Laravel Bug...

@SNAPEY - Thanks, I was only repeating what was already being done in the legacy code. I was supposed to minimize changes to existing code for the moment, so I chose to do as had already been done, and left the rest be. But your method is useful if I ever get around to the point of refactoring the code...

Activity icon

Replied to Hoping For Some Assistance With A Possible Laravel Bug...

@TRAY2 - To put very simply, to develop a logging solution for a legacy app which doesn't already have any logging. There are more details, but they've already been covered for the most part in the other two discussions. If you have any additional questions, that haven't already been covered elsewhere, please ask, and I'll try to answer as best as I can... This particular issue has been bugging me for many days, and is taking too long.

  1. I'd like to know if the specific issue is due to a Laravel bug.
  2. Is it specific to version 5.4.* or thereabouts?
  3. Can I patch (modify) laravel itself to fix it? (I know you might not recommend it, but I'd like to know exactly what is causing the issue if possible...)

Because, theoretically, I don't see anything wrong with my implementation. If I'm doing something wrong, please let me know. But I like my implementation, and would like to keep it. It should work as is, and for the most part it does, but in the specific case of create, it fails... Why?

Activity icon

Replied to Hoping For Some Assistance With A Possible Laravel Bug...

@TRAY2 - All that is explained in the two discussions I've linked to...

Activity icon

Started a new Conversation Hoping For Some Assistance With A Possible Laravel Bug...

I started two discussions previously, but I didn't get the exact answer I was looking for. So I'm going to be more specific in my phrasing this time. Go through the previous discussions, and if you can, help me figure out why my implementation is failing only for create...

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

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

The application is failing to log the correct values, but just to clarify, the create (insert) itself is working fine.

Jun
20
3 months ago
Activity icon

Replied to Hoping For Pointers On CRUD Logging...

@MTHOMAS - Why do I want to do that? I haven't gone over that in my discussions previously, but the reason is this is supposed to be a sort of user-friendly (in the sense, somewhat non-technical) CUD log. It's what I was assigned to do. Not my decision.

As for getting the country name from the Id, I already do that in the controller, hence my attempt to simply pass in the already resolved value from there. Otherwise I'd have to allow an additional DB request.

As for relationships, there are a number of models already defined, and most of the time, no relationships were defined. Legacy app, like I said. Besides, that would still require additional DB requests.

However, these were not exactly the question I was expecting to be answered, when I asked the original question. It was: Why is my own implementation not working only for create.

I like your other ideas, and appreciate your input though. I'll like this post and wait a bit to see if anyone has anything more to add... Thanks :-)

Jun
19
4 months ago
Activity icon

Replied to Hoping For Pointers On CRUD Logging...

@MTHOMAS - No, that's not what I was trying to get at. I think everyone's missing a couple of key points here. I know that any model can be logged, but what I really want to know is can I log a particular model's attributes in response to a different model's event? I've been trying to get this point across from the start. For example, let's say User has country_id. Country has country_name. When I'm logging User, I also want to log User's related country_name. Now do you get it?

Jun
18
4 months ago
Activity icon

Replied to Hoping For Pointers On CRUD Logging...

@MTHOMAS - Can you log values not in the model with this package? From the example, I couldn't immediately figure out how. I wan't to be able to log values from related models as well (sometimes), or sometimes values present in the controller... For details, check the discussion linked to in the OP, esp. the last comment so far by me...

Activity icon

Replied to 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 :-)

Jun
16
4 months ago
Activity icon

Replied to 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?

Activity icon

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! :-)

Jun
14
4 months ago
Activity icon

Replied to 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...

EDIT: Besides, similar code works for the updated, but for created, the event doesn't receive the payload.

EDIT 2: Though I said the event doesn't receive the payload, that's not completely accurate. What I should've said is the that the payload set in the controller isn't logged, meaning country => UK wouldn't be logged. Instead, the payload actually logged comes from the base model (LoggingModel) implementation: the id, and this can happen only if event_payload is not explicitly set.

Jun
12
4 months ago
Activity icon

Replied to 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.

Activity icon

Replied to Eloquent CRUD Logging Solution

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

Activity icon

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 :-)