CorCronje

CorCronje

Member Since 3 Years Ago

Bloemfontein, South Africa

Experience Points
21,230
Total
Experience

3,770 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
191
Lessons
Completed
Best Reply Awards
3
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 5
21,230 XP
May
17
5 months ago
Activity icon

Started a new Conversation Add A Trait To A Class As A Private Variable

Hello there!

I've got a trait that wish to use in an abstract way inside a class, my objective is to use the trait in the following manner,

$this->trait->method();

I've attempted the following, but it does not work as expected,

trait NumberFormatter
{
    function toCurrency($float)
    {
        return number_format($float, 5 , '.', ' ');
    }
}

class Sales {

    use NumberFormatter;

    private $format;

    public function __construct()
    {
        $this->format = NumberFormatter::class;
    }

    public function getSalesAmount()
    {
        return $this->format->toCurrency('123456.3210');
    }
}

Is it possible to do something like this in PHP?

Thanks.

May
16
5 months ago
Activity icon

Replied to Count Distinct Children Within Relationship

Hello,

Your objective is "count how many distinct keys are in a session", thus this should theoretically work, I haven't tested it out.

$count = Session::->whereBetween('end_time', ['2018-05-19 04:42:00', '2019-05-19 04:42:00'])->events->pluck('key')->unique()->count();

Best.

Activity icon

Replied to Integrity Constraint Violation: 1052 Column 'id' In Where Clause Is Ambiguous

Hello,

You could spend time to hack that function to possibly work, but it would probably still be a hack and look messy. Why don't you try this instead.

On your Group model, add the inverse of the relation,

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

Then you can easily find the members of multiple groups by passing the groups.id as an array into the model's find() method,

$members = Group::find([1, 3, 9, 35, 8])->members; // non-distinct

$unigueMembers = Group::find([1, 3, 9, 35, 8])->members->unique(); //distinct

Just a note, the $members variable will hold a Collection instance and if I'm right, the user may be listed in the collection multiple times, depending on how many groups the user is a member off. Thus you can use the unique() method on the collection to get only unique users.

Activity icon

Replied to Laravel + Vue.js: [Vue Warn]: Failed To Mount Component: Template Or Render Function Not Defined.

Hi there!

Have you tried moving these two "require" lines up so that the files are loaded before the "import" statement is called?

require('./components/Navbar.vue').default
require('./components/Customers.vue').default
May
15
5 months ago
Activity icon

Replied to Date Format In Laravel Controller

Awesome! Happy to help.

Activity icon

Replied to Date Format In Laravel Controller

Pleasure man!

This line will return a string,

$d = DB::table('student_data')->value('date_of_birth');

Now lets overwrite $d with a Carbon instance from the string $d and reformat it like so,

$d = Carbon::createFromFormat('Y-m-d H:i:s', $d)->format('d/m/Y');

Then you can pass $d to the view/blade as before,

...['mydate' => $d]...

Then in blade you can do,

{{ $mydate }}
Activity icon

Replied to DataTables Error

I had the same issue late last year.

Open your composer.json file and add "yajra/laravel-datatables": "^1.2",

"require": {
        "php": "^7.1.3",
        "fideloper/proxy": "^4.0",
        "laravel/framework": "5.8.*",
        "laravel/tinker": "^1.0",
        "yajra/laravel-datatables": "^1.2"
    },

Then update your project dependencies,

composer update

Regards,

Activity icon

Replied to Date Format In Laravel Controller

@KONRMS - It is a bit difficult to see without the code for retrieving the entity.

Lets work on a cleaner approach. are we still talking about the "student_data" table and is there a StudentData model?

Activity icon

Replied to Date Format In Laravel Controller

Yes, or I completely forgot about about date casting:

You can add this to your StudentData model:

protected $casts = [
    'date_of_birth' => 'datetime:d/m/Y',
];

Casting works with serialization, thus this should work,

$objStudent = StudentData::first(); // get the first row
$arrStudent = $objStudent->toArray(); //serialize to an array
$d = $arrStudent['date_of_birth']; //15/05/2019

// can also string it together
$d = StudentData::first()->toArray()['date_of_birth']; //15/05/2019

But it is too long and messy.

I would rather just create an accessor on the StudentData model:

public function getBirthDateAttribute()
{
        return $this->date_of_birth->format('d/m/Y');
}

Then in Blade you can simply do,

{{ $student->birth_date }} //15/05/2019

Anyhow, hope this helps.

Activity icon

Replied to Date Format In Laravel Controller

Hi there,

Please try as per the following:

$d = DB::table('student_data')->first()->date_of_birth //returns a string value via the query builder

$d = App\StudentData::first()->created_at //returns a Carbon instance via Eloquent

$d->format('d/m/Y'); // "12/05/2019"

Then in you blade template you can do somethong like,

{{ $student->date_of_birth->format('d/m/Y') }}

Regards,

Activity icon

Replied to In Axios Request I Got Has Been Blocked By CORS Policy Error

This CORS thing again! I've tripped over it many times.

Checkout this Laracast where Jeffery discuss VueJS and CORS in detail.

Best,

Activity icon

Replied to Date Format In Laravel Controller

I believe when retrieving a datetime value from your DB via Eloquent, the returned property would be a Carbon instance, and thus the following should work.

dd($d->format('dd/mm/Y'));

Carbon Docs

Best,

Activity icon

Replied to DataTables Sorting & Searching Not Working

Hello there,

I believe there is a missing parameter true when setting the defaults.

Try changing your code as follow:

$.extend(true, $.fn.dataTable.defaults, {
    ordering: true,
    searching:true,
    select: true,
    "order": [[ 0, 'desc' ], [ 1, 'desc' ], [2, 'desc'], [3, 'desc']]
} );

Checkout this package, makes server-side pagination, sorting etc. a breeze!

yajra/laravel-datatables

Best,

Activity icon

Replied to SQLSTATE[23000]: Integrity Constraint Violation: 1048 Column 'product_id' Cannot Be Null (SQL: Insert Into `category_product`

Hi Irankhosravi,

I encountered a similar issue before, and then realized that I was testing without a valid user login. If there is no user logged in, then auth()->user()->id is null and saving a null value to the DB in a table column with a foreign key or relation would break. At the time I was manually testing by hand, and simply registering a user and logging in, and then navigating to the page I was working on resolved the issue for me. I also fixed my code by validating that a user was in fact authenticated using a guard or middleware.

Hope this helps,

Best.

May
11
5 months ago
Activity icon

Started a new Conversation Where To Store Your JWT Token

Hello there!

I'm consuming a RESTful API using Guzzle and initially do an authentication request to JWT authorization token that is to be included in the header of all subsequent requests.

This token is typically valid for a predetermined period of time, 24 hours in this case, and thus I wish to store the token somewhere and optimally reuse it. I don't want to store it in a DB as this means that have to query the DB each and every time I call the class. Secondly I aiming to find some database / platform agnostic solution that I can apply to all my projects, as I'm consuming various APIs that works in a similar manner.

I am currently storing the token in a ".token" file in the root of my app, and then read the file each time the class is instantiated, but I am a bit concerned for security and performance.

How do you store your API tokens and is there some other efficient solution that I can implement over all my projects?

Thanks!