nickywan123

nickywan123

Member Since 4 Months Ago

Experience Points
8,690
Total
Experience

1,310 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
2
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.

Level 2
8,690 XP
Aug
11
1 day ago
Activity icon

Replied to Eager Loading Count Not Working

@tisuchi Nope, but I followed @michaloravec solution and it seems to fix the problem.

Activity icon

Replied to Eager Loading Count Not Working

@michaloravec $with is not working anymore?

Activity icon

Started a new Conversation Eager Loading Count Not Working

I refactor my reply model with eager loading like below:

...
protected $with = ['owner','favorites'];

 public function favorites(){
        return $this->morphMany('App\Favorite','favorited');
    }

 public function isFavorited(){

        return !! $this->favorites->where('user_id',auth()->id())->count();
    }

In my view:

 {{$reply->favorites_count}}

favorites_count return null. It was working fine before I add $with.

What did i do wrong?

Activity icon

Replied to Php Artisan Config:cache Removes All Tables

@sinnbeck Thanks, but i can assure you I didn't use RefreshDatabase trait, I only used DatabaseMigrations trait.

Activity icon

Replied to Php Artisan Config:cache Removes All Tables

@geordiejackson I am using the DatabaseMigration trait in my tests.

Activity icon

Replied to Php Artisan Config:cache Removes All Tables

@sinnbeck Okay it makes sense. So avoid caching config when using phpunit.

But it's still odd that it removes the actual tables from my database though.

Activity icon

Replied to Php Artisan Config:cache Removes All Tables

@sinnbeck How come running config:clear will make it work again?

  1. I am using mysql workbench for database. For testing, I configured a local test on my configuration.

  2. It removes all the tables of migration that I have created so I am not too sure.

  3. It happens when i run config:cache and I noticed the tables in my db are all missing. Running the vendor/bin/phpunit then removes it again.

Activity icon

Replied to Php Artisan Config:cache Removes All Tables

@sinnbeck I experimented it and it did remove it, why does it do that?

Activity icon

Started a new Conversation Php Artisan Config:cache Removes All Tables

I am running unit tests and all the tests passed but when I run php artisan config:cache, running the test again makes most of my test failed and I checked my database, the tables were gone.

Why does this happen and how do I fix this?

Tried to run php artisan migrate and run the phpunit test again and the tables are removed again.

Aug
10
2 days ago
Activity icon

Replied to WantsJson Does Not Exist.

Nvm the error is :

Error: Call to a member function assertSeeInOrder() on array.

Activity icon

Replied to WantsJson Does Not Exist.

@michaloravec It shows this error:

TypeError: Argument 1 passed to Symfony\Component\HttpFoundation\Response::setContent() must be of the type string or null.

My browser returns the popularity correctly but the test case shows red with the error above.

Activity icon

Started a new Conversation WantsJson Does Not Exist.

I wrote a test to filter my threads based on popularity:

 /** @test */
    function a_user_can_filter_threads_by_popularity(){
        $this->withoutExceptionHandling();
        // Given we have three threads with 3 replies, 2 replies and 0 reply
        $threadWithThreeReplies = factory('App\Thread')->create();

        factory('App\Reply')->create(['thread_id' => $threadWithThreeReplies->id],3);

        $threadWithTwoReplies = factory('App\Thread')->create();
        factory('App\Reply')->create(['thread_id' => $threadWithTwoReplies->id],2);

        $threadWithNoReplies = $this->thread;

        //We should be able to fetch the threads page by popularity

        $response= $this->getJson('threads?popular=1')->json();

        //Return threads with most replies to least reply.
        $response->assertSeeInOrder([
            $threadWithThreeReplies->title,
            $threadWithTwoReplies->title,
            $threadWithNoReplies->title
            ]);

    }

And in my controller, I handle the response using wantJson() helper like below:

  public function index(Channel $channel, ThreadFilters $filters)
    {

        $threads = Thread::latest()->filter($filters);
        if($channel->exists){
            $threads->where('channel_id',$channel->id);
        }

        if(request()->wantsJson()){
            return $threads;
        }
         
        $threads = $threads->get();
        
        return view('threads.index',compact('threads'));
    }

Running the test says wantJson() does not exist, i'm guessing because the older laravel version has this method as I am watching a tutorial from laracast using this method. Are there any other alternatives to this?

Activity icon

Commented on A User Can Filter Threads By Popularity

It doesn't work for me and i'm using laravel 7 as well. The difference is I didn't use helper method for create()

Activity icon

Replied to Reference Relationship With Bracket And Without

i guess

$thread->replies()->get()

should be declared in the controller and just passed it as one to the view?

Activity icon

Replied to Adding A Global Scope To Count

@bugsysha Okay so if i were to have a relationship called creator() and would do the same like:

$builder->withCount('creator');

And access it like

$thread->creator_count

?

Activity icon

Replied to Adding A Global Scope To Count

@martinbean

I tried accessing it via

{{ $thread->replyCount()->replies_count }}

saying replyCount() is an undefined method.

i think replyCount is redundant and removing it and accessing like your third point:

{{ $thread->replies_count }}

will work.

Also,

class Thread extends Model
{
    protected $withCounts = ['replies'];
}

Does the variable need to be $withCounts or any other name?

Activity icon

Replied to Adding A Global Scope To Count

@bugsysha So "replyCount" is just the reference key of the global scope and

$builder->withCount('whatevername);

should be called like

$thread->whatevername_count

?

Activity icon

Started a new Conversation Adding A Global Scope To Count

I have a thread model and each thread has many relationship for replies. I added a global scope to my model so I can call it anywhere.

Thread model:

  protected static function boot(){
        parent::boot();

        static::addGlobalScope('replyCount',function ($builder){
            $builder->withCount('replies');
        });
    }

 public function replies(){
        return $this->hasMany('App\Reply');
    }

So when I try to retrieve the count in my blade like below:

This thread has {{$thread->replyCount} comments

it returns null. Was wondering, am I calling my scope wrongly?

Activity icon

Replied to Reference Relationship With Bracket And Without

So if I use

foreach($thread->replies() as $reply)
......

It would return nothing as it is a query builder ?

Activity icon

Replied to Reference Relationship With Bracket And Without

@sinnbeck Thanks for reply.

But with getting the creator name like

{{$thread->creator()->name}}

Don't I need to go into the database to retrieve the name of the thread creator?

That's what I am confused.

Activity icon

Started a new Conversation Reference Relationship With Bracket And Without

I am trying to reference a relationship for my threads that I created with creator and replies.

I found out that these two ways will work and fetch the correct value:

{{$thread->replies()->count()}}

or 

{{$thread->replies->count()}}

In another instance, say I want to find the name of the poster, I could do this:

{{$thread->creator->name}}

but this wouldn't work:

{{$thread->creator()->name}}

My thread model:

public function replies(){
        return $this->hasMany('App\Reply');
    }

 public function creator(){
        return $this->belongsTo('App\User','user_id');
    }

Why is this the case and when do I use bracket or without bracket to avoid confusion?

Aug
08
4 days ago
Activity icon

Commented on A Lesson In Refactoring

Same here, it was so simple to understand until this lesson where he starts the refactoring where it looks overkill and sooooo confused.... I have no idea if I should continue lol.

Aug
05
1 week ago
Activity icon

Replied to Failed Asserting That Exception Of Type "Illuminate\Auth\AuthenticationException" Is Thrown.

@martinbean You told me to use the act -arrange -assert pattern. What if I wish to redirect to a different route,say register page?

Will that still be a valid test case?

Activity icon

Replied to Failed Asserting That Exception Of Type "Illuminate\Auth\AuthenticationException" Is Thrown.

@sinnbeck But isn't this the wrong way?

Thought $this->expectExpection() should does the work, how come it does not work?

Activity icon

Replied to Failed Asserting That Exception Of Type "Illuminate\Auth\AuthenticationException" Is Thrown.

@sinnbeck Still throws the same error.

If I add this then it will work:

 $this->withoutExceptionHandling();
         $this->expectException(AuthenticationException::class);
Activity icon

Started a new Conversation Failed Asserting That Exception Of Type "Illuminate\Auth\AuthenticationException" Is Thrown.

I wrote a test case for unauthentication user so I will obviously expect the auth exception to be thrown but it was handled with this code below:

 public function unauthenticated_users_may_not_add_reply(){
        
         $this->expectException('Illuminate\Auth\AuthenticationException');
    
         $this->post('/threads/some-channel/1/replies', []);
        
        //$response->assertRedirect(route('login'));
       
    }

However, it still throws the same error. How do I handle this?

Aug
04
1 week ago
Activity icon

Replied to Factory Is Not Generating A Channel_id

@geordiejackson Okay i already solved it after banging my head few times.

Activity icon

Replied to Factory Is Not Generating A Channel_id

@geordiejackson Yea did check my imports.

This is my Channel migration (which appears fine):

    public function up()
    {
        Schema::create('channels', function (Blueprint $table) {
            $table->id();
            $table->string('name',50);
            $table->string('slug',50);
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('channels');
    }
Activity icon

Replied to Factory Is Not Generating A Channel_id

@vpanta Why would it return id without specifiying to return id like ->id?

Also, tested with your code but same error.

Activity icon

Started a new Conversation Factory Is Not Generating A Channel_id

REMOVED

Activity icon

Replied to Integrity Constraint Violation: 19 NOT NULL Constraint Failed: Threads.channel_id

@tray2 Like this:

    /** @test */
    function a_thread_belongs_to_a_channel(){
        $thread = factory('App\Thread')->create();
        $this->assertInstanceOf('App\Channel', $thread->channel);
    }

It throws exception at this test since it couldn't create data for channel_id column:

 /**
     * A basic feature test example.
     *
     * @test
     */
    function an_authenticated_user_may_create_a_new_thread(){
        $this->withoutExceptionHandling();
        //Given an authenticated user
        $this->actingAs(factory('App\User')->create());

        //Create a new thread
        $thread = factory('App\Thread')->make();

        $this->post('/threads', $thread->toArray());

        //Then when we visit the thread

        $this->get($thread->path())
        ->assertSee($thread->title)
        ->assertSee($thread->body);
    }
Activity icon

Replied to Integrity Constraint Violation: 19 NOT NULL Constraint Failed: Threads.channel_id

@tray2 This is my Thread model:

class Thread extends Model
{
    protected $guarded = [];
    // return a single thread
    public function path(){
        return '/threads/' .$this ->id;
    }

    //return replies associated with a thread
    public function replies(){
        return $this->hasMany('App\Reply');
    }

    //return creator of the thread
    public function creator(){
        return $this->belongsTo('App\User','user_id');
    }

    public function addReply($reply){
        return $this->replies()->create($reply);
    }

    //Every thread belongs to a channel
    public function channel(){
        return $this->belongsTo('App\Channel');
    }
}

I added $guarded so it should still work though.

Activity icon

Replied to AssertSee Not Working

@aurawindsurfing I changed my code to this:

    function an_authenticated_user_may_create_a_new_thread(){
        $this->withoutExceptionHandling();
        //Given an authenticated user
        $this->actingAs(factory('App\User')->create());

        //Create a new thread
        $thread = factory('App\Thread')->make();

        $this->post('/threads', $thread->toArray());

        //Then when we visit the thread

        $this->get($thread->path())
        ->assertSee($thread->title)
        ->assertSee($thread->body);
    }

So now it works, no error. Is this okay?

I think the error earlier was because there was no route post in my web.php. After adding it, it is working.

Activity icon

Replied to AssertSee Not Working

@aurawindsurfing What does withoutExceptionHandling() do?

Does it tells you the exact error if it fails?

Activity icon

Replied to Are Routes Request Considered To Be Same As REST API?

What actually is the difference between REST and RESTful?

Then what about REST API as there is an api.php in routes as well?

Activity icon

Started a new Conversation Are Routes Request Considered To Be Same As REST API?

Just wanted to clarify this as I've noticed laravel applications, we tend to use GET,POST,PUT,etc in web.php for routing.

Is that considered the same as REST API since they also have the same methods of request?

Activity icon

Replied to AssertSee Not Working

@aurawindsurfing I changed it to create() and it still didn't persist to the database. Am I doing anything wrong?

Aug
03
1 week ago
Activity icon

Replied to Test Case- How To Get Authentication Exception?

@martinbean i meant i added this is what i am expecting it to behave:

  function a_guest_may_not_create_a_thread(){
        $this->withoutExceptionHandling();
        $thread = factory('App\Thread')->make();

        $this->post('/threads', $thread->toArray())->assertRedirect('/login');
    
    }

That's what I am looking for.

Activity icon

Replied to Test Case- How To Get Authentication Exception?

@martinbean It passes but what I am trying to achieve is how do I make it fail as because I want to verify that an unauthenticated user is not able to create the thread?

Activity icon

Started a new Conversation Test Case- How To Get Authentication Exception?

I written a test case that a guest cannot create a thread if it's not authenticated.

In my threads controller"

  //Add middleware for authenticated user only
    public function __construct()
    {
        $this->middleware('auth')->only('store');
    }

My test case:


     /**
     * A basic feature test example.
     *
     * @test
     */
    function a_guest_may_not_create_a_thread(){

        $thread = factory('App\Thread')->make();
        
        $this->post('/threads', $thread->toArray());   
    
    }

When I run the php artisan test, it passes this but i should be expecting an authentication exception. Why it is not throwing it?

Activity icon

Replied to AssertSee Not Working

@aurawindsurfing ok it passes now. If I use create() thread instead of make(), why my db table does not insert the new record?

Activity icon

Replied to AssertSee Not Working

@silencebringer So if I still get a warning telling me there's no assertion in my test. Do I need to add any kind of assertion?

Activity icon

Replied to AssertSee Not Working

@aurawindsurfing Nah, it did create the actual thread, problem is make() couldn't assertSee the thread that is created.

Activity icon

Replied to AssertSee Not Working

@silencebringer Doing this will passed though:

     $this->actingAs(factory('App\User')->create());

        //Create a new thread
        $thread = factory('App\Thread')->create();

        $this->post('/threads', $thread->toArray());

        //Then when we visit the thread

        $this->get($thread->path())->assertSee($thread->title)->assertSee($thread->body);
Activity icon

Replied to AssertSee Not Working

I guess if i remove assertSee() then it passes but how do you explain for $reply in my other test function that passes even i didn't create it.

Activity icon

Replied to AssertSee Not Working

@silencebringer nope it passes. I am using latest laravel version if that matters. I wrote a different test case like this:

    public function an_authenticated_user_may_participate_in_a_thread(){
        //Given we have an authenticated user
        //$user = factory('App\User')->create();
        $this->be($user = factory('App\User')->create());
        //And an existing thread
        $thread = factory('App\Thread')->create();

        //When the user adds a reply to the thread
        $reply = factory('App\Reply')->make();

        $this->post($thread->path().'/replies', $reply->toArray());

        //then their reply should be visible on the page
        $this->get($thread->path())->
        assertSee($reply->body);
    }

and it passed as well.

Activity icon

Replied to AssertSee Not Working

@silencebringer That's not the issue because I checked line by line and the last line in the test case is making it failed.

Activity icon

Replied to AssertSee Not Working

Though I remove the last line of code and it passes so the culprit is definitely assertSee().