Zhen

Member Since 2 Years Ago

Experience Points
4,580
Total
Experience

420 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
43
Lessons
Completed
Best Reply Awards
0
Best Reply
Awards
  • start your 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-in-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 Created with Sketch.

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • lifer Created with Sketch.

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • evangelist 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.

  • Community Pillar

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

Level 1
4,580 XP
Nov
27
1 month ago
Activity icon

Replied to Retrieving Models With Relationships That Matches Array Of Ids

Thanks a lot, it works. I can't really wrap my head around it though. Why would the fallback query be necessarry in case the input count is < 1?

Activity icon

Started a new Conversation Retrieving Models With Relationships That Matches Array Of Ids

I currently have a Profile model and a Preferences model, they are linked by a profile_preference table in a many to many relationship.

I'm trying to retrieve profiles that satisfy a list of preferences coming from my input. For example:

$profileA->preferences
//outputs [1]
$profileB->preferences;
//outputs [1,2]

If I receive the input of [1,2], I only want to retrieve Profile B, and if I receive the input of [1] I want to receive both Profile A and B.

I have tried with a querybuilder:

Profile::query()
	->whereHas('preferences', function (Builder $query) use ($input) {
		$query->whereIn('preference_id', $input);
        );

But this won't give me profiles that satisfy all conditions, if I receive the input [1,2] it returns both profiles instead of only ProfileB.

Any ideas as to how to approach this?

Oct
24
2 months ago
Activity icon

Replied to Getting Updated Model Attributes After Updating Model

Hi @silencebringer I'm running the code through the debugger and I can see that when I query $profile->mainImage after doing a $profile->update($profileProperties); the mainImage is correctly attached when I evaluate it. But when I run $profile->completionPhotos() after it, it shows false.

Debugging the PhotoProgress class confirms this, as evaluating the $this object in it returns the not-yet-updated model (so no $this->profile->mainImage here)

The way I grab $profile->completionPhotos() is perhaps out of scope for this question since I'm using a graphQL setup where I update it in the mutator and return it through Lighthouse PHP as a type. Photos is a bad example since it is way more complex, so here is another mutator:

  public function __invoke($_, array $args)
    {
        $profileProperties = Arr::only($args, [
            'name',
            'gender',
        ]);

        /** @var Profile $profile */
        $profile = Auth::user()->profile->first();
        $profile->update($profileProperties);

        return $profile;

The $profile returned does indeed contain the name and gender now. But the method checking for it still shows false.

Oct
23
3 months ago
Activity icon

Started a new Conversation Getting Updated Model Attributes After Updating Model

I have a trait in my Profile model called ProfileProgress, it tracks the completion progress based on it's attributes. A snippet:

trait ProfileProgress
{
    /**
     * @return bool
     */
    public function completionPhotos(): bool
    {
        return (new PhotoProgress($this))->getCompleted();
    }

There are multiple functions in this trait that track the completion of certain profile sections. PhotoProgress simply takes a certain set of Profile attributes and checks if they are set.

The problem arises when I update the model, the $this object passed to the progress checkers is not updated with the new data. I have tried $profile->fresh() before running $profile->completionPhotos() to no avail.

I've also tried something silly like this:

trait ProfileProgress
{
	private $model;

	protected static function bootProfileProgress(): void
	{
		static::saving(function ($model) {
			$model->model = $model;
        	});
	}

	public function completionPhotos(): bool
	{
		return (new PhotoProgress($this->model))->getCompleted();
	}

In the boot method $model actually does contain the updated attributes, but I think I'm seriously misunderstanding something about the way models work because setting it like this doesn't work either.

Oct
12
3 months ago
Activity icon

Awarded Best Reply on Unable To Test BelongsToMany Relationships

It turns out that all my BelongsToMany relationships where attempting to link to tables that were not yet seeded. Fixed by running php (new DatabaseSeeder())->call(LocaleSeeder::class); before running the factory in the test.

Activity icon

Replied to Unable To Test BelongsToMany Relationships

It turns out that all my BelongsToMany relationships where attempting to link to tables that were not yet seeded. Fixed by running php (new DatabaseSeeder())->call(LocaleSeeder::class); before running the factory in the test.

Activity icon

Started a new Conversation Unable To Test BelongsToMany Relationships

I'm working with a ProfileFactory that randomly attaches BelongsToMany relationships which works great when seeding the database. It also works fine when running the factory in tinker

$profile = factory(\App\Profile::class)->create();
$profile->spokenLanguages;

This will neatly print out all the randomly attached spokenLanguages.

However, whenever I run the factory in my phpunit test (using sqlite :memory: mode), these attached relationships turn up empty when debugging. This does not happen with hasMany relationships.

Am I running into a incorrect configuration of PHPunit or a certain limitation of sqlite?