kevinbui

kevinbui

Laravel Developer at Visual Domain

Member Since 4 Years Ago

Melbourne, Australia

Experience Points
51,410
Total
Experience

3,590 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
468
Lessons
Completed
Best Reply Awards
10
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 11
51,410 XP
Dec
09
5 days ago
Activity icon

Replied to How To Conditionally Add Query In Eloquent?

What about convert your query to an Eloquent query and use the when function.

Dec
05
1 week ago
Activity icon

Replied to Calculating Permalink For Each Reply, Without N+1 Problem

At first you say:

$post = Post::where('id', $post)->with('replies.author')->withCount('likes')->first();

And later on when you say:

$reply->post->permalink;

The $reply DOES NOT access the $post you define earlier backward. Its gonna run an extra DB query to the database to get an identical post.

So you got to use the solution given by @sinnbeck.

I recommend reading Illuminate\Database\Eloquent\Model::__get() and this eager loading explained video to get to know this further.

Activity icon

Replied to Laravel - How To Calculate The Sum Of Objects In An Array In Blade View

It would be a bit better if you extract the sum() function to a custom accessor.

class Transaction extends Model
{
    public function getTotalAttribute()
    {
        return $this->options->sum(function ($option) {
            return $option->pivot->price;
        })
    }
}

So later on in the view, you might simply say:

<h4>Options total: {{ $transaction->total }}</h4>
Nov
16
4 weeks ago
Activity icon

Commented on Inviting Users As A Feature Test

I don't know that I can write validation and authorization rules in so many ways.

Nov
03
1 month ago
Activity icon

Commented on Rendering Activity With Polymorphism

Maybe at some later stage you would change $project->activity to $project->activities.

Oct
26
1 month ago
Activity icon

Commented on Sometimes Validation With Form Requests

at 9:31 I immediately thought about tap function. And you did it anyway.

Oct
25
1 month ago
Activity icon

Commented on Improve Test Arrangements With Factory Classes

Excellent lesson! I just wonder if there is an alternative name for ProjectFactory. So that will not be confused with the ProjectFactory file to create an instance of Project.

Sep
16
2 months ago
Activity icon

Replied to LatestActivity Conditional

Is that the outcome that you want?

Also, do you name your model 'Tickets'? Ticket could be a better name.

Activity icon

Replied to Laravel Feature Test

Can you post the controller code as well?

There's nothing wrong with status code 302, its a redirect response. You may want to change the assertions to something like this:

$response->assertStatus(302)
                 ->assertViewHas('settings')
                 ->assertViewIs('setting::admin.index');
Activity icon

Replied to Laravel Feature Test

Does it say any errors when you uncomment $this->withoutExceptionHandling()?

Activity icon

Replied to LatestActivity Conditional

Inside the Ticket model, let's define a lastReply relationship:

class Ticket
{
    public function lastReply()
    {
        return $this->hasOne(Reply::class)->latest();
    }
}

Then get the tickets.

$tickets = Ticket::whereHas('lastReply', function ($query) {
    $query->where('user_id', '!=', auth()->user());
});
Sep
13
3 months ago
Activity icon

Replied to Complex Eloquent / SQL Request, Need Backup

What about this:

$authors = Author::whereHas('books.pages', function ($query) {
    $query->where('published', true);
});
Activity icon

Replied to Assign Value To Data Property From A Method In Vuejs

It's good that you are able to solve your own issue. I think that event handling thing got to be registered inside the CREATED or MOUNTED hooks.

export default {
    mounted() {
        grecaptcha.ready(() => {
                    grecaptcha.execute('6LeawrcUAAAAAIrA-LQ-kytjPFEBcedXDLcWHHHM', {action:      
                    'homepage'}).then((token) => {
                        this.RegForm.token = token;
                    });
                });
    }
}
Sep
07
3 months ago
Activity icon

Commented on Ignition Is Laravel's Amazing New Error Page

Very cool feature.

Aug
28
3 months ago
Activity icon

Replied to Question Regarding Best Practice For Controller Methods

Checkout this talk . This is the best guide for making controllers! Maybe this is what you are looking for.

Activity icon

Replied to When Running Phpunit Test Following Error Occurs In Building Laravel App With TDD Episode 5

Can you show the code for a_user_can_create_a_project test case?

Activity icon

Replied to Phpunit: AssertArrayHasKey() - Undefined Index: Errors

Instead of creating your own function, what about using $response->assertSessionHasErrors();

Aug
26
3 months ago
Activity icon

Replied to Refactoring An Sql Sentence To Eloquent Style

As I understand, it would be more performant if we handle everything in one query.

Aug
25
3 months ago
Activity icon

Replied to Refactoring An Sql Sentence To Eloquent Style

Good point. I have updated my answer accordingly.

Activity icon

Replied to Refactoring An Sql Sentence To Eloquent Style

I assume that you have a Brand model, you might rewrite the query to something like:

$brands = Brand::select('brands.id', 'brands.name')
    ->join('products', 'products.brand_id', '=', 'brands.id')
    ->join('opportunities', 'opportunities.product_id', '=', 'products.id')
    ->where('opportunities.status', 1)
    ->groupBy('brands.name')
    ->get();

An alternative is to use whereHas and relationships. From your query I assume that a brand has many products and a product has many opportunities:

$brands = Brand::select('brands.id', 'brands.name')
    ->whereHas('products.opportunities', function ($query) {
        $query->where('status', 1);
    })->get();
Aug
17
3 months ago
Activity icon

Commented on Object-Oriented Forms: Part 1

Sorry, I didn't hear Jeffrey said that its not gonna work for more complex form.

Activity icon

Commented on Object-Oriented Forms: Part 1

Good lesson! But @keydown is not gonna work with some form elements, such as select. Is there any other event that can be used for all form elements?

Aug
03
4 months ago
Activity icon

Replied to Getting HasOne Relationship

Cool. Just a note on that, you may say:

optional($user->credit)->amount;

So even if a user has no credit, it still return null and dont raise any error.

Activity icon

Replied to Getting HasOne Relationship

Does that user has any credit?

Activity icon

Replied to Getting HasOne Relationship

Because you define a HasOne relationship so I think credit() could be a better function name.

public function credit()
{
    return $this->hasOne(Credit::class);
}

If you call the credit() function, you are gonna have a Illuminate\Database\Eloquent\Relations\HasOne object. If you want to access the credit model, pls say:

$user->credit->amount;
Jul
17
4 months ago
Activity icon

Replied to Groupby And Sum The Total_amount Of Same Suppliers

Interesting. Because we paginate the results, so $suppliers is an instance of Illuminate\Pagination\LengthAwarePaginator. Therefore, we are not able to use Higher Order Messages directly.

I have updated my answer slightly to consider that.

Activity icon

Replied to Groupby And Sum The Total_amount Of Same Suppliers

I have updated my answer accordingly.

Jul
15
4 months ago
Activity icon

Replied to FirstOrCreate On Multiple Related Records From XML Feed

So what data format did you have after using Rodenastyle/stream-parser?

Activity icon

Replied to Pass Array From Child To Parent

That looks a bit complicated. What about storing products in the WorkSheet component and then pass it down to the ProductCart components?

// WorkSheet.vue
<template>
<div>
    <ProductCart :products="products"/>
</div>
</template>

<script>
    import ProductCart from './ProductCart'
 
    export default {
        components: {
            ProductCart
        },
        data() {
            return {
                products: []
            }
        }
    }
</script>

So the WorkSheet component already got the products in the beginning.

Activity icon

Replied to Call Controller Function From Another Controller

I am not sure what your question is but it may not necessary to do so. Laravel supports heaps of redirecting mechanisms, including this redirecting to actions.

Activity icon

Replied to Call Model From Associate Table

This is a feature named Higher Order Message.

So the full form is gonna be:

$totalAmountDue = $child->user->payments->sum(function ($payment) use ($userId) {
    return $payment->usrAmountdue($child->user->id);
})

if ($totalAmountDue == 0) {
    ....
}

And of course, with higher order message, it become much simpler:

if ($child->user->payments->sum->usrAmountdue($userId) == 0) {
    ...
}
Activity icon

Replied to Call Model From Associate Table

If I understand your question correctly, what you want may be something like this:

if ($child->user->payments->sum->usrAmountdue($userId) == 0) {
    ...
}
Jun
22
5 months ago
Activity icon

Replied to Groupby And Sum The Total_amount Of Same Suppliers

@abdulbazith That's an interesting point. Payments for a supplier could have different due dates, so it's not possible to group by suppliers. I have updated my answer above, I basically put all due dates in one cell.

Activity icon

Replied to Groupby And Sum The Total_amount Of Same Suppliers

For better naming, I renamed the relationship from purchasetostore to payments.

public function payments()
{
    return $this->hasMany(PurchaseToStore::class, 'supplier_id');
}

We could make a query starting from the Supplier model, not the PurchaseToStore one.

$suppliers = Supplier::whereHas('payments', function ($query) use ($fromDate, $toDate) {
        $query->whereBetween('due_date', [$fromDate, $toDate]);
    })
    ->with(['payments' => function ()  use ($fromDate, $toDate) {
        $query->whereBetween('due_date', [$fromDate, $toDate]);
    }])
    ->paginate(50);

From the view (I assume that you use timestamp for the due_date field):

<thead>
    <tr>
        <th>S.No</th>
        <th>Supplier</th>
        <th>Due Dates</th>
        <th>Amount</th>
    </tr>
</thead>
<tbody>
@foreach($suppliers as $supplier)
    <tr>
        <td>{{$supplier->id}}</td>
        <td>{{$supplier->name}}</td>
        <td>{{
            $supplier->payments
                ->pluck('due_date')
                ->map
                ->format('d/m/Y')
                ->unique()
                ->implode(', ')
        }}</td>
        <td>{{$supplier->payments->sum('amount')}}</td>
    </tr>
@endforeach
</tbody>
<tfoot>
    <tr>
        <td colspan="4">Total: ${{$suppliers->getCollection()->map->payments->collapse()->sum('amount')}}</td>
    </tr>
</tfoot>
Jun
19
5 months ago
Activity icon

Replied to Help Me Refactor This Code Its A Bit Lengthy

Is it alright to make the order field a sequential value? If so, we can make that auto incremented in the migration file:

Schema::create('course_items', function (Blueprint $table) {
    // ...
    $table->signedInteger('order')->autoIncrement();
    // ...
});

Please let me know @successdav so I can update my answer accordingly.

Jun
17
5 months ago
Activity icon

Replied to Laracast Loads Very Fast

There is also a free course about optimisation:

https://serversforhackers.com/laravel-perf

Jun
12
6 months ago
Activity icon

Replied to PDF With QrCode

I did the same task before and those are the two packages that I used:

https://github.com/barryvdh/laravel-dompdf

https://github.com/endroid/qr-code

Jun
11
6 months ago
Activity icon

Replied to Grouping By Pivot Table Columns

You might try this:

$result = $thread->tags->groupBy(function ($tags) {
    return $tag->pivot->tag_type;
});
Activity icon

Replied to How To Add Extra Values In Every Result?

This would be a perfect case for accessor:

https://laravel.com/docs/5.8/eloquent-mutators#accessors-and-mutators

You could do something like:

use Illuminate\Support\Str;

public function getTempCodeAttribute($value)
{
    return "$value-" . Str::random(4);
}