bwrigley

bwrigley

Member Since 2 Years Ago

Experience Points
7,950
Total
Experience

2,050 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
11
Lessons
Completed
Best Reply Awards
1
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
7,950 XP
Jul
09
1 month ago
Activity icon

Replied to Moving From Sentinel To Laravel Auth

@deepu07 thanks but no unfortunately doesn't help with installing in an existing project.

make::auth doesn't create controllers or override existing migration scripts etc.

Jul
07
1 month ago
Activity icon

Started a new Conversation Moving From Sentinel To Laravel Auth

Hi There,

I have an old project that's still running 5.8, but it's also using Sentinel packages for registration, login etc.

I want to replace this with Laravel's Auth but following the docs will only install views and routes as far as I can see, and I need the controllers, middleware and migration files.

Is there a package I can install the holds all of these elements or do I need to manually move them in?

I hope that makes some sense.

Thanks

Mar
14
5 months ago
Activity icon

Replied to Cascading AfterCreatingState() In Factories

@sti3bas so sorry for slow reply, I didn't get a notification for some reason.

I have now realised the issue from my side is that I was running the factory in tinker which seems not to create the relationships properly.

I have it working find now through a seeder, using either make or create

So sorry for wasting your time!

Mar
09
5 months ago
Activity icon

Replied to Cascading AfterCreatingState() In Factories

@sti3bas no problem at all, really appreciate your help with this.

Activity icon

Replied to Cascading AfterCreatingState() In Factories

@sti3bas sorry to ask, but do you not get the same issue?

Mar
04
5 months ago
Activity icon

Replied to Cascading AfterCreatingState() In Factories

@sti3bas I had tried that too and then I get no ForumReply or ForumLike models at all

$factory->afterCreatingState(ForumThread::class, 'with-replies', function ($thread) {

	dd('I got called')

    $thread->forumReplies()
    ->saveMany(factory(ForumReply::class, rand(0,6))
    ->states('with-user')
    ->make());
});

dd never gets called

Activity icon

Replied to Cascading AfterCreatingState() In Factories

@sti3bas thanks for your reply so quickly!

I tried this but and switched all the afterCreatingState() calls to aftermakingState() but all my ForumReply and ForumLike models are still orphaned.

Activity icon

Started a new Conversation Cascading AfterCreatingState() In Factories

I'm creating a forum which has these relationships:

ForumCategory hasmany ForumThread hasmany ForumReply

and both ForumThread and FormReply hasmany ForumLike

I'm now trying to set up factories and seeders and I'm using the afterCreatingState() to create relationship models:

ForumCategoryFactory:

$factory->define(ForumCategory::class, function (Faker $faker) {
    return [

        'name' => implode(' ', $this->faker->words(3)),
        'description' => $this->faker->text(600),

    ];
});


$factory->afterCreatingState(ForumCategory::class, 'with-threads', function ($category) {

    $category->forumThreads()
    ->saveMany(factory(ForumThread::class, rand(0,6))
    ->states('with-user','with-replies','with-likes')
    ->create());
});

ForumThreadFactory:

$factory->define(ForumThread::class, function (Faker $faker) {

    return [
        'title' => implode(' ',$faker->words(3)),
        'message' => $faker->text(600),
        'is_resolved' => false,
        'is_locked' => false,
        'is_pinned' => $faker->boolean(),
        'is_live' => true,
        'is_visible' => true,
        'view_count' => $faker->numberBetween(0,1000),
        'user_id' => null,
        'forum_category_id' => null,

    ];
});

$factory->afterCreatingState(ForumThread::class, 'with-replies', function ($thread) {


    $thread->forumReplies()
    ->saveMany(factory(ForumReply::class, rand(0,6))
    ->states('with-user')
    ->create());
});

$factory->afterCreatingState(ForumThread::class, 'with-likes', function ($thread) {

     $thread->forumLikes()
    ->saveMany(factory(ForumLike::class, rand(0,6))
    ->states('with-user')
    ->create());
});

///

ForumReplyFactory:

$factory->define(ForumReply::class, function (Faker $faker) {
    return [
        'message' => $faker->text(600),
        'is_solution' => $faker->boolean(),
        'is_locked' => $faker->boolean(),
        'is_pinned' => $faker->boolean(),
        'is_visible' => true,
        'user_id' => null,
        'forum_thread_id' => null,
    ];
});


$factory->afterCreatingState(ForumReply::class, 'with-likes', function ($reply) {

    $reply->forumLikes()
    ->saveMany(factory(ForumLike::class, rand(0,6))
    ->states('with-user')
    ->make());
});

///

For the most part it is working, the category is created as are threads and replies and likes. The threads have relationships to the category.

The problem I'm having is that the ForumReply and ForumLike models all have null 'forum_thread_id`

Thanks for any guidance or spotting of glaring errors.

Activity icon

Replied to Resource Controllers With Multiple Param Types

@sti3bas brilliant thank you! I missed that.

Mar
03
5 months ago
Activity icon

Replied to Resource Controllers With Multiple Param Types

@sti3bas thanks for coming to the rescue again!

both good options thank you. But can those routes work with resource controllers? I know you can specify expected parameters something like:

Route::resource('forum_reply', 'Forum\ForumReplyController')
->parameters(['type' => 'forum_thread']);

Activity icon

Started a new Conversation Resource Controllers With Multiple Param Types

Hello,

I'm a big fan of the resource controllers and would love to keep everything as CRUD as I can and wonder if there is way I can achieve this without having to do custom routes.

I'm writing a Forum which has ForumThread and ForumReply models and I'm adding a ForumLike model with a polymorphic relationship to either a thread or a reply.

Now when I'm creating a new ForumLike how can I pass a ForumThread or a ForumReply as a param so that I can establish the relationship?

Thanks for any help.

Feb
27
5 months ago
Activity icon

Replied to Eloquentish For Next Autoincrement ID?

@click @snapey thanks for your thoughts.

Yes I may well do that, although less attractive if people choose not to edit their slug.

Thanks for input!

Activity icon

Replied to Eloquentish For Next Autoincrement ID?

@sti3bas

Thank you, you are a star.

I only had

    public function emails(): HasMany
    {
        return $this->hasMany('App\Email');
    }
Activity icon

Replied to Eloquentish For Next Autoincrement ID?

@snapey

Yes I totally get that however what I am trying to do here is to ensure that the slug I am setting in this model is unique and can be edited by the user later.

The best way I can think of to ensure it's unique is to add the id to the slug.

So yes it's dangerous, but only if I get two users registering with the same name at exactly the same time.

Activity icon

Replied to Eloquentish For Next Autoincrement ID?

@sti3bas thank you so much and thank you for your guide.

I am still hitting one issue though as I didn't show you the whole store method which now looks like this:

        $profile->fill([
            'first_name' => $request->first_name,
            'last_name' => $request->last_name,
            'long_title' => $request->first_name . ' ' . $request->last_name . ' ' . $profile->getNextId(),
            'slug' => Str::slug($profile->long_title)
         ]);

        $user->profile()->save($profile);

        $profile->emails()->create(['address' => $request->email]);

This last line is what causes the issue with

PDOException: SQLSTATE[HY000]: General error: 1 table emails has no column named mockery_2__app__profile_id

This is obviously because it is picking up the mocked field when trying to attach the two models. Any thoughts on how to get around this?

Thanks again!

Activity icon

Started a new Conversation Eloquentish For Next Autoincrement ID?

Hello,

I have a Profile model that has a few fields which are indexable and therefore cannot be null and must be unique. So I'm auto-generating them at first save (can be edited later).

In Profile model:

    public function getNextId()
    {
        $query = DB::select("show table status like 'profiles'");
        return $query[0]->Auto_increment;

    }

In ProfileController:

        $profile = new Profile;
        $profileId = $profile->getNextId();
        $profile->first_name = $request->first_name;
        $profile->last_name = $request->last_name;
        $profile->long_title = $request->first_name . ' ' . $request->last_name . ' ' . $profileId;
        $profile->slug = strtolower($request->first_name . '_' . $request->last_name . '_' . $profileId);

        $user->profile()->save($profile);

All works great. Until I run my tests which are using SQLite for in-memory tests and then I get the error:

Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 near "show": syntax error (SQL: show table status like 'profiles')

So I'm guessing show is not SQLite syntax.

Is there a way to write my getNextId() method in a DB agnostic way, or do I need to find the current highest id and add 1?

Feb
26
5 months ago
Activity icon

Replied to How To Ask Faker To Generate Text Over $x Chars Long?

Yeah I wondered about that too, but wasn't sure if that will always definitely be longer than 500, but I expect it will.

Activity icon

Replied to How To Ask Faker To Generate Text Over $x Chars Long?

@sinnbeck oops our messages crossed.

sadly no, even

$faker->text(600),

often generates text shorter than 500 chars so my test is flaky.

Activity icon

Replied to How To Ask Faker To Generate Text Over $x Chars Long?

@sinnbeck thanks for the fast response.

Yes I can and I will but I was hoping to generate more 'natural' text for some other tests I am doing.

Perhaps I will isolate this test and just do as you suggest and use faker in the other test.

Do you know of a way to ask Faker to generate text/lorem ipsum longer than n?

Activity icon

Replied to How To Get Data From Table, And Display In Search Bar After Type Column Name?

Check out Algolia, very fast and integrates beautifully with Laravel.

https://laravel.com/docs/6.x/scout

Activity icon

Started a new Conversation How To Ask Faker To Generate Text Over $x Chars Long?

Hello,

Hoping this will be really simple.

I have verification on a field to ensure it is no longer than 500 chars:

'about' => ['required', 'max:500'],

And now I want to write a test that will always go over that limit so I can check the correct error message appears.

How can I ask Faker/Generator to generate random text longer than 500 chars?

Thanks!

Feb
16
5 months ago
Activity icon

Replied to How To Seed Database With Many To Many Relationship?

@dr24 sorry for delay, I didn't get alerted to your reply.

Assuming you have set up the relationships correctly, then what I have shown you will create entries in the user_interests table.

$user->fields()->saveMany(factory(Field::class, rand(0, 4))->make());

creates 0-4 new Field models and 'attaches' them to the User model, this will create entries in your joining table.

Feb
12
6 months ago
Activity icon

Replied to Faker And Locale

@sti3bas

Thank you, good idea!

I just assumed it would pick it up automatically when i instantiated a new Faker. Thanks for your help!

Activity icon

Replied to Faker And Locale

@sti3bas 5.8.31 still, not quite with 6 yet

Activity icon

Replied to Faker And Locale

@sti3bas thank you so much for replying.

So I have tried this but I still get

InvalidArgumentException: Unknown formatter "county"

which is en_GB locale specific. So it seems it's still not picking up the locale for some reason.

Activity icon

Replied to How To Seed Database With Many To Many Relationship?

So how I would do it is

Field Factory:

use Faker\Generator as Faker;

$factory->define(App\Field::class, function (Faker $faker) {
    return [
        'label' => $faker->randomElement(['Men','Women','Relationship','Chatting']),
        'value' => $faker->boolean(50) ? 1 : 2,
        'sort' => $faker->boolean(50) 1: 2,
    ];
});

(not sure what value and sort are for so just guessing at their values)

User Factory:

use Faker\Generator as Faker;

$factory->define(App\User::class, function (Faker $faker) {

    return [
        'username' => $faker->userName,
        'email' => $faker->unique()->safeEmail,
    ];
});

User Seeder:


use App\User;
use App\Field;

class UsersTableSeeder extends Seeder
{

    public function run()
    {
        factory(User::class, 150)
            ->create()
            ->each(function($user) {
        $user->fields()->saveMany(factory(Field::class, rand(0, 4))->make());
            });
    }
}

I'm not quite following your structure, but if I have understood, then this Seeder with create 150 User models each with 0-4 Field models attached. Is that what you mean?

Activity icon

Replied to How To Seed Database With Many To Many Relationship?

Something along these lines in your User factory?

Generates 30 users, grabs 0-4 random fields and attaches them in your join.

        factory(User::class, 30)
            ->create()
            ->each(function($user) {
                $randomFields= Fields::all()->random( rand(0, 4) )->pluck('id');
                $user->fields()->attach($randomFields);
        });
Activity icon

Replied to Loop Through Query Items Before Paginating

Is there any reason you are asking the DB to do all the heavy lifting? Would you consider accessing the data and using Laravel to process the data to get what you need instead?

Activity icon

Started a new Conversation Faker And Locale

Hello everyone,

I'm pretty sure I'm being dumb here but I'm having issues with Faker locale.

In my config/app.php:

    'locale' => 'en',
    'faker_locale' => 'en_GB',

this works fine in a factory:

use Faker\Generator as Faker;

$factory->define(\App\Address::class, function (Faker $faker) {
    return [
        'country' => $faker->randomElement(['England','Wales','Scotland','Northern Ireland','Southern Ireland','United Kingdom']),
        'country_code' => 'GB',
        'town' => $faker->boolean(50) ? $faker->city() : null,
        'county' => $faker->boolean(50) ? $faker->county() : null,
        'address1' => $faker->boolean(50) ? $faker->buildingNumber() . ' ' . $faker->streetName() : null,
        'address2' => $faker->boolean(80) ? $faker->streetName() : null,
        'postcode' => $faker->boolean(50) ? $faker->postcode() : substr($faker->postcode(),0,3),
        'is_verified' => $faker->boolean(50),
        'is_live' => $faker->boolean(50),
        'is_visible' => $faker->boolean(50),
        'is_primary' => $faker->boolean(50),
        'raw' => str_replace("\n",'<br>',$faker->address()),
        'latitude' => $faker->latitude(49.82,59.47),
        'longitude' => $faker->longitude(-10.85,2.02),
        'profile_id' => 1,
    ];
});

but when I try in a test:

use Faker\Factory as Faker;

       //

        $faker = Faker::create();

        $addressDetails = [
            'country' => $faker->randomElement(['England', 'Wales', 'Scotland', 'Northern Ireland', 'Southern Ireland', 'United Kingdom']),
            'country_code' => 'GB',
            'town' => $faker->city(),
            'county' => $faker->county(),
            'address1' => $faker->buildingNumber() . ' ' . $faker->streetName(),
            'address2' => $faker->streetName(),
            'postcode' => $faker->postcode(),
            'is_verified' => $faker->boolean(50),
            'is_live' => $faker->boolean(50),
            'is_visible' => $faker->boolean(50),
            'is_primary' => true,
            'latitude' => $faker->latitude(49.82,59.47),
            'longitude' => $faker->longitude(-10.85,2.02),
        ];

then I get the error:

InvalidArgumentException: Unknown formatter "county"

and with

 $faker = Faker::create('en_GB');

it works again fine!

I'm guessing it's to do with how I'm creating my instance of Faker that's causing the issue as it's not coming from the service container.

Thanks for any help.

Feb
10
6 months ago
Feb
09
6 months ago
Activity icon

Replied to Storing Data In A Random Attribute Of A Model That Will Be Excluded When Saving It.

Is there a reason you need to store the data as an attribute on the model? I would definitely look for a different place to store it.

But if you have to, and this feels like a hack, have you tried adding to $guarded as this will stop those fields being written at save(). Warning: haven't tested this myself.

    /**
     * @var string[string]
     */
    protected $guarded = ['slowFunctionResult'];