Flerex

Flerex

Member Since 2 Years Ago

Experience Points
4,490
Total
Experience

510 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
42
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
4,490 XP
Sep
02
2 months ago
Activity icon

Replied to Some Bugs I Found

The same thing happens when I published this post. It seems like there's a 6h offset from the real time.

Activity icon

Started a new Conversation Some Bugs I Found

I just answered a post and went to check my profile to find this:

As you can see it says that I have posted that message 7 hours ago but I literally just posted it.

Also, while I was writing this new topic I found that if you try to attach an image to the post using Markdown and then use the preview button, the part of the editor where the toggle and the publish/cancel buttons are is cut off.

And, finally, a smaller detail. In a topic, there's no margin between the best answer and the first post but between all other posts there is. See:

Versus:

Activity icon

Replied to How To Initialize A Class In Its Constructor With Session Data, While Being Able To Inject It In A Controller's Constructor

After some research I found that the best way to solve this is the following.

First, the Auth related logic should not be placed in the controller, as it is considered a bad practice (because of things like this). This logic should be placed elsewhere and leave the constructor just to build the dependencies of the object.

With this said, by having a getter method (even tho it is protected) we can check if the attribute that we're trying to access has been initialized and, if not, doing it right away.

class Helper
{

    /** @var array $settings */
    protected $settings;

    protected getSettings() {
        if ($this->settings === null) {
            if (Auth::check()) {
                $this->settings = Auth::user()->settings;
            } else if (Cookie::has('settings')) { // Data from guest users is stored in a cookie
                $this->settings = Cookie::get('settings');
            } else {
                $this->settings = [];
            }
        }

        return $this->settings;
    }
    
    public function getDefinedSettings() {
        return $this->getSettings();
    }
    
    // More methods that make use of $this->getSettings()
    
}
Aug
24
2 months ago
Activity icon

Replied to How To Initialize A Class In Its Constructor With Session Data, While Being Able To Inject It In A Controller's Constructor

I am not sure, actually. I liked what you proposed as it would be a way to initialize when it actually can, but what would you do if it weren't a singleton? Because this problem could arise in a situation were I wouldn't want/need a singleton and I would be back at the same problem.

Activity icon

Replied to How To Initialize A Class In Its Constructor With Session Data, While Being Able To Inject It In A Controller's Constructor

@jlrdw No, I'm not using traits. I missunderstood you. I just don't know where you're going with your static method. Maybe if you provided some code based on the example I provided it would make it much more clearer to me what you're trying to say. But I would like to avoid static methods as they're generally considered a bad practice.

Activity icon

Replied to How To Initialize A Class In Its Constructor With Session Data, While Being Able To Inject It In A Controller's Constructor

What @snapey proposes is an interesting approach, but for some reason I think it feels dirty, as it happens to work just because it is a singleton. If it weren't, then you wouldn't be able to get every single instance of the class.

Regarding what you're proposing @jlrdw, the problem I see is what I already contemplated in the main post. By delegating the logic into their own method and then hiding it under a trait, I would still have to remember every time that a public method is created to run this one to check if the class has been initialized before and, if not, do it again. I think you're forgetting that the class has state and that there's a private $settings attribute that I have to set different values depending on whether the user is logged in or not.

Activity icon

Replied to How To Initialize A Class In Its Constructor With Session Data, While Being Able To Inject It In A Controller's Constructor

So, you're saying I should delegate the initialization of the Helper class to the methods in that class so I can use the session because they will be called in a situation that the session would already be created?

Activity icon

Replied to How To Initialize A Class In Its Constructor With Session Data, While Being Able To Inject It In A Controller's Constructor

I can't use static methods as the Helper class has state. In fact there are class attributes. In a static method I wouldn't be able to access those attributes.

Activity icon

Replied to How To Initialize A Class In Its Constructor With Session Data, While Being Able To Inject It In A Controller's Constructor

Thanks for your answer @jlrdw!

When you say to use a use statement I suppose you mean to create the instance of the class by using new, right? If that is the case, I would rather not, as I am currently making this class a singleton using the service container. This is for efficiency reasons, as every time the helper is instantiated it runs multiple SQL queries (in the example I provided there would be only one query, but it would still not be ideal).

Activity icon

Started a new Conversation How To Initialize A Class In Its Constructor With Session Data, While Being Able To Inject It In A Controller's Constructor

I am developing a site that allows to save a visitor's preferences, whether they're logged in or viewing the site as a guest.

To make things easier, I abstracted some logic around this under a helper class (a service). This class can be injected anywhere throughout my code, and will provide methods like $helper->getSettings(), so we can obtain the current visitor's settings without bothering about where they come from (User model, the cookie, the defaults).

Here's a dummy example of how the class would look like:

class Helper
{

    /** @var array $settings */
    protected $settings;

    public function __construct()
    {
        if (Auth::check()) {
            $this->settings = Auth::user()->settings;
        } else if (Cookie::has('settings')) { // Data from guest users is stored in a cookie
            $this->settings = Cookie::get('settings');
        } else {
            $this->settings = [];
        }
    }
    
    public function getSettings() {
        return $this->settings;
    }
    
    // More methods that make use of $this->settings
    
}

Note: This class is bound to the Service Container as a singleton.

I am currently using this class in a controller's private method. As I cannot directly inject this dependency in the private method's signature, I have to inject it from within the controller's constructor, like this:

class MyController extends Controller
{


    /** @var Helper $helper */
    protected $helper;

    public function __construct(Helper $helper)
    {
        $this->helper = $helper;
    }

    private function somePrivateMethod() {
        // Using $this->helper
    }

    //

}

Now, the problem is the following:

I am injecting this class into the controller by using its constructor. As the controller's constructors are run before the middleware pipeline, there's no session and, hence, authentication yet, so my Helper class will not work as expected.

To solve this I have thought of some alternatives:

  • Creating a initialize() method in the Helper class that would check if the class has been initialized. This method would be called before any of the other methods in the helper. This option, however, feels very dirty and prone to error as I could forget to add this check.
  • Using the app() global helper, but I am not a fan of globals and would like to avoid them if possible.

As I'm not very happy about my options I come to you to enlighten me with your wisdom. What would you do?

Aug
20
3 months ago
Activity icon

Replied to Change Index Behavior If 'ids' Parameters Exists

Hello,

If you are trying to eager load only one specific column you must always select also the ID, like this:

Group::with([
    'users' => function($query) {
        $query->select(['id', 'name']);
    },
    'permissions' => function($query) {
        $query->select(['id', 'name']);
}];

Which is equivalent to:

Group::with(['users:id,name', 'permissions:id,name']);

So, from @tykus answer:

use Illuminate\Http\Request;

public function index(Request $request)
{
    $query =  Group::with(['users:id,name', 'permissions:id,name']);

    if ($request->has('ids')) {
        return $query->pluck('id');
    }

    return $query->get();
}