Sultenhest

Sultenhest

Member Since 2 Years Ago

Experience Points
44,130
Total
Experience

870 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
440
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 9
44,130 XP
Jul
04
4 months ago
Activity icon

Replied to Querying A HasMany Through A BelongsToMany

Hi @bobbybouwmann

I went along with your suggestion and wrote a DB query like this

$statuses = DB::table('follows')
            ->join('statuses', 'follows.followed_user_id', '=', 'statuses.user_id')
            ->join('users', 'statuses.user_id', '=', 'users.id')
            ->select('statuses.*', 'users.username', 'users.name')
            ->where('follows.follower_user_id', '=', auth()->id())
            ->latest()
            ->limit(15)
            ->get()
            ->toArray();

Which seems to return what i want.

In the meantime i've decided to expand the project and implement an activity feed, which logs statuses, likes, user updates and so on, and use that as my main feed instead of just statuses.

Jul
02
4 months ago
Activity icon

Started a new Conversation Querying A HasMany Through A BelongsToMany

Hi guys

I'm currently practicing my Laravel skills by building a Twitter clone and i'm still relatively new at building eloquent queries.

I'm stuck building the "feed", which should consists of the statuses of the other user profiles the given user is following.

This is what my models looks like:

User.php

public function statuses()
{
    return $this->hasMany(Status::class)->latest('updated_at');
}

public function following()
{
    return $this->belongsToMany(User::class, 'follows', 'follower_user_id', 'followed_user_id');
}

public function followers()
{
    return $this->belongsToMany(User::class, 'follows', 'followed_user_id', 'follower_user_id');
}

Status.php

public function user()
{
    return $this->belongsTo(User::class);
}

This is what the MySQL query looks like:

SELECT statuses.*
FROM follows
INNER JOIN statuses ON follows.followed_user_id = statuses.user_id
WHERE follower_user_id = 151
ORDER BY statuses.created_at DESC
LIMIT 15;

Where 151 is the current user id and the limit is a pseudo pagination.

My current attempt at eloquent looks like this

auth()->user()->following()->with('statuses')->orderBy('created_at', 'desc')->paginate(15);

It is querying the correct followings along with ALL of their statuses, and its ordering by user.created_at and not status.created_at.

What have i missed?

Jun
25
4 months ago
Activity icon

Replied to Self Referencing Many-To-Many Relationship

@snapey

The problem was indeed with the HasOne, in a sort of way.

In my ProfileController it said:

auth()->user()->profile()->following()->toggle($profile);

And it should've been

auth()->user()->profile->following()->toggle($profile);

..without the parentheses. Oops :)

Regarding making followers and followed from profile and not user: Is it considered better practice to bind them to Users, considering it's a one-to-one relationship?

Activity icon

Replied to Self Referencing Many-To-Many Relationship

@SNAPEY - Hi Snapey

Thanks for the reply.

This is whats getting logged:

[2019-06-25 08:03:01] testing.ERROR: Call to undefined method Illuminate\Database\Eloquent\Relations\HasOne::following() {"userId":1,"exception":"[object] (BadMethodCallException(code: 0): Call to undefined method Illuminate\Database\Eloquent\Relations\HasOne::following() at /Users/Simon/Laravel/laratweet/vendor/laravel/framework/src/Illuminate/Support/Traits/ForwardsCalls.php:50)

EDIT: Yes, the assertDatabaseHas returns that the table is empty and the assertRedirect returns a 500 status code.

Activity icon

Started a new Conversation Self Referencing Many-To-Many Relationship

Hello guys,

I'm currently building a pseudo-Twitter clone using TDD for Laravel practice and i'm stuck building the "follow" functionality.

As it is now, every user, has a profile (one-to-one, works fine), which is created on user registration. Profiles should now be able to follow, and be followed, by other profiles.

I've looked at the official Laravel documentation as well as this other post i found here Handling Self reference Many to Many relationships, but i can't seem to make it work. This is the code i've got:

Migration:

Schema::create('follows', function (Blueprint $table) {
    $table->primary(['follower_profile_id', 'followed_profile_id']);
        $table->unsignedBigInteger('follower_profile_id');
        $table->unsignedBigInteger('followed_profile_id');
        $table->timestamps();

        $table->foreign('follower_profile_id')->references('id')->on('profiles')->onDelete('cascade');
        $table->foreign('followed_profile_id')->references('id')->on('profiles')->onDelete('cascade');              
});

Route:

Route::post('/profile/{profile}/follow', '[email protected]');

ProfileController:

public function follow(Profile $profile)
{
    auth()->user()->profile()->following()->toggle($profile);

    return redirect($profile->path());
}

Profile model:

public function path()
{
    return "/profile/{$this->username}";
}

public function following()
{
    return $this->belongsToMany(Profile::class, 'follows', 'follower_profile_id', 'followed_profile_id');
}

public function followers()
{
    return $this->belongsToMany(Profile::class, 'follows', 'followed_profile_id', 'follower_profile_id');
}

Test file:

public function test_a_profile_can_follow_another_profile()
{
    $follower = factory('App\Profile')->create();

    $followed = factory('App\Profile')->create();

    $response = $this->actingAs($follower->user)
                    ->post($followed->path() . '/follow');

    $this->assertDatabaseHas('follows', [
        'follower_profile_id' => $follower->id,
        'followed_profile_id' => $followed->id
    ]);

    $response->assertRedirect($followed->path());
}

And this is what i get from running the test:

The table is empty.

Or if i remove the assertDatabaseHas part:

Response status code [500] is not a redirect status code.

What am i missing?