BrandonSurowiec

BrandonSurowiec

Michigan

Member Since 3 Years Ago

Experience Points 43,920
Experience
Level
Lessons Completed 439
Lessons
Completed
Best Reply Awards 27
Best Answer
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    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 Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

20 Jun
5 months ago

BrandonSurowiec left a reply on Php Unit Need Help

Are you doing the permission check through a Middleware or in the Controller? I would go to wherever that check is supposed to be performed, and dump() out various variables related to the permission check. Then run the tests again for more information.

19 Jun
5 months ago

BrandonSurowiec left a reply on Refresh Database Trait Remove All Data From Mysql Database

php artisan cache:clear will clear the cache. That should be all you need.

BrandonSurowiec left a reply on Refresh Database Trait Remove All Data From Mysql Database

You shouldn't run php artisan config:cache locally.

BrandonSurowiec left a reply on How To Downgrade My Laravel, I Have 5.6 And I Want Go To 5.4

You can add this to your composer.json to assist in forcing packages to resolve for PHP 5.6. (It will already have a config section, add the platform part.):

    "config": {
        "preferred-install": "dist",
        "platform": {
            "php": "5.6.16"
        }
    }

BrandonSurowiec left a reply on Refactoring Test Methods

You can simply make a new class in your /database/MatchFactory.php. I've done this before.

<?php

class MatchFactory
{
    function create($attributes = [])
    {
        $match = factory(Match::class)->create($attributes);

        // WHatever you need to do to generate wrestlers..
        $match->wrestler()->saveMany([
            factory(Wrestler::class)->create(),
            factory(Wrestler::class)->create(),
        ]);

        return $match;
    }
}

Then you just use that.

function test_a_non_title_match_can_set_a_winner()
{
    // By default, let's assume your match factory auto create two wrestlers.
    $match = (new MatchFactory)->create();

    $winner = $match->wrestlers->first();
    $loser = $match->wrestlers->last();

The cool thing about this, is you can do whatever you want. You can add assertion helper methods, or config options to the constructor on how many wrestlers to create, etc.

BrandonSurowiec left a reply on Refresh Database Trait Remove All Data From Mysql Database

@bobbybouwmann You're right, but this is an odd edge-case. He cleared the cache and has his phpunit.xml set correctly and it still doesn't work. This same thing happened to one of my projects and I never use the caching feature locally. The rest of my projects act perfectly normal.

There is something else going on. If I ever spend the time digging into it I'll update this thread.

BrandonSurowiec left a reply on Refactoring Test Methods

For the most bang for your buck test these methods through the endpoint where you are using them, and not directly like a unit test.

That way: A. You know your implementation works B. If you change your implementation your test shouldn't have to change. C. You'll stop duplicating the code that is in your controller.

Here's a rewrite of the first method.

function test_a_non_title_match_can_set_a_winner()
{
    // By default, let's assume your match factory auto creates both wrestlers.

    $match = factory(Match::class)->create();

    $winner = $match->wrestlers->first();
    $loser = $match->wrestlers->last();

    $this->post("match/{$match->id}", $update = [
        'winner_id' => $winner->id,
        'won_by' => 'pinfall',
    ]);

    $match->refresh();
    $this->assertEquals($update['won_by'], $match->won_by);
    $this->assertTrue($winner->is($this->match->winner));
    $this->assertTrue($loser->is($this->match->loser));
}

I would probably leverage states() with the Match class and have that generate everything that you need so you can follow a similar format as the above..

18 Jun
5 months ago

BrandonSurowiec left a reply on Eol ORM

You can sum in the database, something like this:

$sum = User::where(...)->selectRaw('SUM(num1 - num2) as total')->value('total');

Or after the fact with the collection:

$sum = User::where(...)->get()->sum(function($user) { 
    return $user->num1 - $user->num2;
});

BrandonSurowiec left a reply on Refresh Database Trait Remove All Data From Mysql Database

Try this:

public function createApplication()
{
    $app = require __DIR__.'/../bootstrap/app.php';

    $app->make(Kernel::class)->bootstrap();

    config(['database.default' => 'sqlite']);

    return $app;
}

And you could also just try commenting out DB_CONNECTION from your .env file.

BrandonSurowiec left a reply on Refresh Database Trait Remove All Data From Mysql Database

I've had this happen to me before too, where the phpunit.xml wasn't setting the custom .env variables, but my env file was overriding them.

Pretty sure I did a workaround by setting the env vars in the createApplication() trait/method like:


    public function createApplication()
    {
    
        putenv("DB_CONNECTION=sqlite");

        //    ...

BrandonSurowiec left a reply on Refactoring Test Methods

You could leverage your factories to generate some of the other models.

Your match could automatically generate the event as needed.

// In your Match Factory

[
   'event_id' => factory(Event::class)->lazy(),
]

You could also leverage states to encapsulate some of the factory generation.

$title = factory(Title::class)->states('withChampion')->create();

Then you could even leverage $title->currentChampion as one of the fighters.

15 Jun
5 months ago

BrandonSurowiec left a reply on ->count() VS Select Count(*) As Count

The difference seems negligible. I'd use the one that is easiest to work with. (Case A)

BrandonSurowiec left a reply on ->count() VS Select Count(*) As Count

They are the exact same query. So A would be faster since B is doing more work.

01 Jun
5 months ago

BrandonSurowiec left a reply on Is Version 5.6 Really This Much Faster Than 5.4?

I have no idea if the comparison is accurate, but Laravel is always pushing to improve the developer experience.

There have been various optimizations that you can look through the change logs for. I remember different collection and array methods getting speed improvements. There was also a push to move away from using the array and string helpers, and to use the facade classes directly.

For the 5.5 or 5.6 release, Taylor went through the entire code base of Laravel and did a bunch of refactoring for readability in the core. There could have been some performance gains there as well.

A lot of performance improvements have come from contributors in the form of Pull Requests.

26 May
5 months ago

BrandonSurowiec left a reply on Laracon Tickets

Email [email protected] and ask to be put on the waiting list. Occasionally, when someone has to give up their ticket, they tweet about it on Twitter and Taylor retweets them.

18 May
6 months ago

BrandonSurowiec left a reply on Vue Modal Window

Put a : in front of the name field so Vue knows to parse it.

<modal :name="`dispute-modal-${++index}`">

BrandonSurowiec left a reply on Middleware To Check If User Is Verified/activated

When the user is not logged in, auth()->user() returns null. You can update your check to something like this:

if (auth()->guest() || ! auth()->user()->verified) {

    return redirect('/login');            
}

The ForgotPasswordController is only accessible to logged out users, so your 'activated' middleware doesn't make sense there.

10 May
6 months ago

BrandonSurowiec left a reply on Eloquent Relationships Advice

I would suggest you try the group column approach that @burlresearch suggested. I know you're concerned about adding "extra" data later on with their own models, but ..

  1. That's not today's problem.

  2. There will be several ways that adding data can be handled while avoiding polymorphic relationships.

Idea: Add a type column to the standard ticket table and populate it with a string for any type of ticket that may have extra details in the future.

Later: Thetype column could then be used at a later date to make code decisions.

  • One approach could be to add specific models that are global scoped to only grab data using the specific type of ticket.

  • Or a driver based approach, where all the extra methods defer to a driver based on the type. which could be a plain PHP class.

function driver()
{

$class = 'App\' . Str::studly($this->type) . 'Ticket';

// would check if the class exists, if not, return a NullableTicketDriver

return new $class($this); 
}


function someOtherCoolMethod() 
{
    return $this->driver()->someOtherCoolMethod();
}

  • For the extra data, it could be stored on a ticket_metas table, or directly on your ticket table as nullable columns. (Sometimes denormalization is okay. It just depends on the requirements/trade offs.)

If you do want to stay with and maintain the Polymorphic approach, you could create plain PHP class called TicketFactory. It could have static methods to help persist the different ticket models in the database given an array of ticket data. It could encapsulate all the complexity.

--

When trying to code with future possibilities in mind, I found my code usually gets more complex and harder to maintain. Today's code should be the simplest and easiest implementation. If it needs to change tomorrow, change it then.

Try the other approach, see if it feels better.

27 Apr
6 months ago

BrandonSurowiec left a reply on Looking To Hire

Have you considered using https://laravelshift.com ?

17 Apr
7 months ago

BrandonSurowiec left a reply on ActingAs Works Half The Time

I haven't used the Passport Facades when testing, but actingAshas a second param for the guard.

$this->actingAs($user = factory('App\User')->create(), 'api');
14 Apr
7 months ago

BrandonSurowiec left a reply on Running Out Of Memory

When you use chunk you need to pass in the $newsLetter variable with a use statement..

->chunk(100, function($results) use ($newsLetter) {

}

Assuming MySQL, you could also do this with a single query. The format is like this:

INSERT INTO table_1 (column1, column2)
SELECT co1, col2, FROM table_2;

http://www.mysqltutorial.org/mysql-insert-statement.aspx

The columns in your select should match the columns in table_1 in the same order. Plainly it would look something like this:

public function addRecipients(NewsLetter $newsLetter)
{ 
    \DB::statement("
        INSERT INTO news_letter_recipients('news_letter_id', 'user_id', 'user_slug')
        SELECT '{$newsLetter->id}'
             , id
             , slug 
        FROM users 
        WHERE news_letter_subscription = 1 
        AND IS NULL email_complaint
        AND IS NULL email_bounced
    ");
    
    return back(); 
}

The downside is that no models events will be fired, but that can be worked around as well.

09 Apr
7 months ago
02 Mar
8 months ago

BrandonSurowiec left a reply on How Do You Only Update Validated Inputs?

I don't think it is worth the effort, but sure it is possible.

You would want to create a custom function that will try and catch the validation, looping through one rule at a time. Something like below. (Completely untested but should work.)

// Add as a helper function somewhere...
function validated($request, $rules = [])
{
    $data = [];
    
    foreach($rules as $key => $rule) {
        try {
            $data = array_merge($data, $request->validate([$key => $rule]));
        } catch(\Illuminate\Validation\ValidationException $e) {}
    }

    return $data;
}


// Usage
$fields = validated($request, [
    'name' => 'required',
    'email' => 'required|email',
    'telephone' => 'required',
]);

dump($fields);
21 Feb
8 months ago

BrandonSurowiec left a reply on Vue-js-modal Not Working

Looks like your app.js file might not be requiring bootstrap.js.

require('./bootstrap');

BrandonSurowiec left a reply on Vue-js-modal Not Working

Did you recompile with npm run dev or npm run watch?

06 Apr
1 year ago

BrandonSurowiec left a reply on Using MorphMap On More Than One Polymorphic Relationship

I know this is an old thread, but you need to prefix your model declarations with .

'sign' =>  \App\Models\FreeLook\SignVendor::class,


// or use a string..

'sign' =>  'App\Models\FreeLook\SignVendor',

The only reason morphMap fails to work is because it can't find the model specified. It was failing for me recently as I was using the wrong namespace.

20 Jan
1 year ago

BrandonSurowiec left a reply on Is This An Acceptable Way To Change Database Connections?

@jthorpe You can put it in the app/ folder. I put all my models in there. (app/ClaimForm.php). For something that is going to wrap the models and do some advanced querying for you, maybe something like app\Repositories\ClaimTotals.php.

I'd love to help you more, but there will be some delays in my replies.

19 Jan
1 year ago

BrandonSurowiec left a reply on Is This An Acceptable Way To Change Database Connections?

You could create a method in the trait that wraps the connection:


use Illuminate\Support\Facades\DB;

trait ConnectionsTrait
{
    function query()
    {
        return DB::connection($this->dbconnection());
    }

}

Then you can do:

$totals = $this->query()->selectRaw("SELECT str_to_dat...");

I'm pretty sure Eloquent models can handle a bunch of the workload.

If you don't want to use models right now, might I suggest some simple classes with methods that wrap the different query calls? It would help clean up your controller and allow someone who is reading through it to understand it better.

$totals = (new ClaimTotals)->byWeek();

or static methods if you want.

$totals = ClaimTotals::byWeek();

This would allow you to later replace your queries easily with models or reuse the queries between different controllers. You don't have to, just a thought. Hope that helps!

29 Dec
1 year ago

BrandonSurowiec left a reply on My Personal (bad) Experience With Forge Service

Taylor is spending a lot of his time on Forge and the Framework. Mohamed Said is working on Spark, Lumen and issues / bugs in the Laravel Framework.

15 Nov
2 years ago

BrandonSurowiec left a reply on Using Sub Directories For Models (5.3)

Does your app\xmaster\Section.php file have namespace App\xmaster; declared at the top?

11 Aug
2 years ago

BrandonSurowiec left a reply on Replace Object Within Another

It should be..

$collection = Model::find(12)->with('details');
$collection->setRelation('details',  collect($_collection->details)->keyBy('id')->groupBy('group_id', true));
dd($collection);
10 Aug
2 years ago

BrandonSurowiec left a reply on Img Onerror Do Something On Vuejs

@fugues Want to share your final solution?

03 Jun
2 years ago

BrandonSurowiec left a reply on Routing With Slugs And No Prefix

Laravel matches routes in order. So put that rule last in routes.php and it will only be followed if nothing else was. Also, you can do this sort of thing too:

    Route::get('videos/{page}', '[email protected]')->where('page', '[0-9]+');

If page wasn't a number, the route wouldn't match.

28 May
2 years ago

BrandonSurowiec left a reply on I Had A Problem In The Edit Controller And Edit View Blade

Try making your controller edit like this:


$event = Event::findOrFail($id);
$event_addresss = Image::lists('address','image', 'id','event_id')->all();      
return view('news.edit', compact('event', 'event_addresss'));

20 May
2 years ago

BrandonSurowiec left a reply on Str_plural To Work With "is"

Make a pull request :P

BrandonSurowiec left a reply on Difference Between Double Braces And PHP Tags

Use {{ $var }} to echo variables. It gets translated to <?php echo e($var); ?>.

18 May
2 years ago

BrandonSurowiec left a reply on Question About Being A Mid Level Developer

@cameronasmith When you're done writing some new code and it works, go back through it and spend 1-2 minutes tightening it up a bit more. (Maybe nothing needs changing!) It's easiest to do if there are tests. Doing small clean ups over and over will make the code simpler to understand and easier to change in the future. Keep it up :)

11 May
2 years ago

BrandonSurowiec left a reply on BUG In Posting Links

There was a problem with links from medium.com having @ in them not linking that I emailed Jeffery about, so he fixed, but it looks like it broke everything else ;)

10 May
2 years ago

BrandonSurowiec left a reply on Valet On Windows

This is how someone else did it: https://medium.com/@CretuEusebiu/running-laravel-valet-on-windows-77910475b8f5#.5tdh0vcn4

With the newer update to use Caddy, it might possibly be more Windows-friendly now, though not officially supported.

28 Apr
2 years ago

BrandonSurowiec left a reply on Allowed Memory Exhausted/Gateway Timed Out

If you have relationships declared in your models you can write out the chain of method names using dot notation and Laravel will do the rest. For example...

Menu::where('menuID', $request->menuID)->with('categories.items.modifierGroup')->firstOrFail();

Look at Nested Eager Loading in the docs: https://laravel.com/docs/5.2/eloquent-relationships#eager-loading

If you don't have the relationship methods in the models I would make them.

BrandonSurowiec left a reply on Allowed Memory Exhausted/Gateway Timed Out

Your main problem is the amount of queries you are doing within loops. Also, with that said, parts of your code are unnecessarily complex unless you stripped it down to show :)

For example in ItemService you have the following function:

public function getMenuItemsToClone($menuID, $restaurant, $menuToClone)
    {
        $menuCategories = $menuToClone->categories;

        foreach ($menuCategories as $menuCategory) {
            $items = $menuCategory->items()->get(); // this only returns the very last set, so you just ran queries only to use 1
        }

        return $items;
    }
  1. You aren't using the $menuID, $restaurant arguments at all.
  2. You are querying the database in a loop, but you'll only ever return the last set of category items that you query.
  3. You can eager load the items beforehand: $menuToClone = Menu::where('menuID', $request->menuID)->with('categories.items')->firstOrFail();

Another example of queries within a loop is in cloneNewMenuItems.

foreach ($items as $itemToClone) {
            $restaurantItems = ItemModel::where('RestaurantID', $restaurantID)->get(); // this
            $modGroups = ItemToModifierGroup::where('MenuItemID', $itemToClone->ItemID)->with('modifierGroup')->get(); // this
            $itemExists = $restaurantItems->where('ItemName', $itemToClone->ItemName);

And your cloneMenuCategories is running a query, which is also inside of the loop in cloneNewMenuItems(). So you'll need to work on doing your queries up front together everything at once.

I would first start with refactoring to remove unused variables from method calls and keep your code the same.

Then push your queries up the chain into postCloneMenu() before your loop.

You have the list of restaurant ids, so load all the CategoryModels and ItemModels up front using Eloquent's whereIn('RestaurantID', $restaurants) and eager load any relationships you plan to use. Then you should be able to loop through the results and do whatever manipulation you wish to do.

There's also probably even a simpler way to do what you're trying to do, but since I don't have the full picture/structure it's hard to suggest :)

Do what you can. Hope that helps.

BrandonSurowiec left a reply on User Profile Information [opinion Needed]

Agreed. I've done both and ended up refactoring down to one table. Just makes everything simpler.

27 Apr
2 years ago

BrandonSurowiec left a reply on Allowed Memory Exhausted/Gateway Timed Out

If you show your cloning code maybe we can suggest a less resource intensive way to go about it :)

03 Apr
2 years ago

BrandonSurowiec left a reply on Undefined Variable - Even If Initiated

You forgot the use statement.

Profile::whereHas('expectations', function($query) use($userSelect) {
04 Mar
2 years ago

BrandonSurowiec left a reply on Issue Finding A User By Email And Password

The hash is unique every time you run it through. You'll want to check the password against the hash. Laravel has a built-in function for authenticating and logging in a user, which is used in the Auth Controller.

Auth::attempt(['email' => $email, 'password' => $password]).

You can read about that here: https://laravel.com/docs/5.2/authentication#authenticating-users

If you want to dig into the attempt function, you'll find it in vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php.

If you simply want to return the user like you're trying to do and NOT log them in, then do this:

$request = Request::all();

$user = User::where('email', $request['email'])
->where('is_active', 1)
->where('is_guest', 0)
->where('is_admin', 0)
->get();

$validCredentials = Hash::check($request['password'], $user->getAuthPassword());

if ($validCredentials) {
    return $user;
}

If the $user->getAuthPassword() doesn't work due to an older version of Laravel, you can simply do $user->password. Hope that helps.

BrandonSurowiec left a reply on Carbon AddDays But Without Modyfing Existing Instance

Like this:

$new_date = $start->copy()->addDays(5)
29 Jan
2 years ago

BrandonSurowiec left a reply on Filter Attribute On Collection

Let your database do the work. You're doing extra work by grabbing everything and then filtering after the fact when you don't need to.

//  My vote
Booking::where('status', Booking::NEW_BOOKING)->get();  
Booking::whereIn('status', [Booking::NEW_BOOKING, Booking::CONFIRMED])->get();  

I'd also go one step further and make a custom query scope that allows you to do this:

Booking::status(Booking::NEW_BOOKING)->get();  
Booking::status([Booking::NEW_BOOKING, Booking::CONFIRMED])->get();  
27 Jan
2 years ago

BrandonSurowiec left a reply on Scope With No Result = Return First Result

You could try something like...

 public function defVariant()
{
    return $this->hasOne('App\ItemVariant')->orderBy('is_default', 'DESC')->first();
}