zachleigh

Hire Me

Experience

200,130

247 Best Reply Awards

  • Member Since 2 Years Ago
  • 819 Lessons Completed
  • 5 Favorites

13th October, 2017

zachleigh left a reply on Dealing With Complexity In Controllers • 6 days ago

@kataklys

The User model is responsible for the users table database records so having a bulk create method on the class makes perfect sense to me.

zachleigh left a reply on Lesson Request: Testing Vue • 6 days ago

Thanks @JeffreyWay!

9th October, 2017

zachleigh left a reply on Axios.get Database Filtering • 1 week ago

You should really be taking advantage of Eloquent and the query builder: https://laravel.com/docs/master/queries

zachleigh left a reply on Axios.get Database Filtering • 1 week ago

Just a db query:

Order::where('order_id', '>', 1000000)->get();

8th October, 2017

zachleigh left a reply on Axios.get Database Filtering • 1 week ago

Send a get parameter, maybe order_id_min, and filter them in your database query.

zachleigh left a reply on How To Get Id From Auth::user() ? • 1 week ago

You arent actually getting the records. You're only getting a builder instance. You need to call get.

$channels = Channel::where('user_id', '=', Auth::user()->id)->get();

zachleigh left a reply on How To Get Id From Auth::user() ? • 1 week ago

What happens when you run the above code?

zachleigh left a reply on Dealing With Complexity In Controllers • 1 week ago

@kataklys

When submitting a form, you would need to have the user information separated from any other information anyway.

[
    'users' => [
        [user 1 data],
        [user 2 data],
        etc.
    ],
    'something else' => [
        //
    ],
]

So in your form request the logic to validate the users information would be namespaced.

'user.*.email' => 'required',
'user.*.name' => 'required|unique',
etc.

So if you do end up making users from some other point in your app, you could simply put the user validation rules on a parent request class or in a trait and merge them in any other rules needed for the request.

The static method on the User model would encapsulate all the logic for starting the transaction, looping through the users and firing the event. This way, if you did end up creating users from some other point in the app, it would be as easy as User::createBulk($data).

zachleigh left a reply on Dealing With Complexity In Controllers • 1 week ago

I would move the validation to a request class, put the user creation logic in a static model method and fire an event (UsersWereCreated) to do anything else like send mail. Every class should ideally have one distinct responsibility. Request class manages the incoming request. Controller class decides what to do with requests. Model is responsible for modeling database records. UsersWereCreated event class manages event data. SendWelcomeEmail event listener sends email to new users. This means that you can very easily unit test every part of this process and hopefully keep them distinct from one another.

At least for me, when I find myself saying 'this class does A AND B AND C', then I know its time to consider a refactor. In some cases it makes sense to not refactor, but if your controller method is making you uncomfortable and is doing multiple tasks, its probably worth creating other classes to perform bits of your logic.

zachleigh left a reply on Validate Birthday (dd/mm/aaaa) • 1 week ago

Are you validating a request? If so, use the the date_format validator: https://laravel.com/docs/master/validation#rule-date-format

zachleigh left a reply on Namespace Declaration Statement Has To Be The Very First Statement • 1 week ago

What about a whitespace after <?php>?

<?php //<- anything here?

7th October, 2017

zachleigh left a reply on Artisan Call In Loop • 1 week ago

My guess is that you are setting the config value in one app instance but when you call Artisan::call, it is actually creating another app instance to run the command in. In this newly created app instance, your config value has not been changed.

Solution could be to create a custom migrate command where you first change the config value and then run the migration by calling the framework classes rather than through another artisan call.

zachleigh left a reply on Namespace Declaration Statement Has To Be The Very First Statement • 1 week ago

Do you have any whitespace between you opening php tag and the namespace declaration?

zachleigh left a reply on Migrate & Seed Only Once? • 1 week ago

I extract the DatabaseTransactions into my own trait to gain control over it:

<?php

namespace Tests;

trait DatabaseTransactions
{
    /**
     * Handle database transactions on the specified connections.
     */
    public function beginDatabaseTransaction()
    {
        $database = $this->app->make('db');

        foreach ($this->connectionsToTransact() as $name) {
            $database->connection($name)->beginTransaction();
        }

        $this->beforeApplicationDestroyed(function () use ($database) {
            foreach ($this->connectionsToTransact() as $name) {
                $database->connection($name)->rollBack();
            }
        });
    }

    /**
     * The database connections that should have transactions.
     *
     * @return array
     */
    protected function connectionsToTransact()
    {
        return property_exists($this, 'connectionsToTransact')
            ? $this->connectionsToTransact : [null];
    }
}

If you try to use the DatabaseTransactions trait that comes with Laravel, it will be registered with the test runner and this will not work. In my TestCase class, I do the following:

<?php

namespace Tests;

use Tests\DatabaseTransactions;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication, DatabaseTransactions;

    /**
     * If true, setup has run at least once.
     *
     * @var boolean
     */
    protected static $setUpRun = false;

    /**
     * Set up the test.
     */
    public function setUp()
    {
        parent::setUp();

        if (!static::$setUpRun) {
            \Artisan::call('migrate:refresh');

            \Artisan::call('db:seed', ['--class' => 'TestDatabaseSeeder']);

            static::$setUpRun = true;
        }

        $this->beginDatabaseTransaction();
    }
}

Basically, if its the first run, we're going to migrate and run a seeder to get everything set up. Otherwise, we'll just start database transactions.

11th September, 2017

zachleigh left a reply on Dusk Suddenly Can't Find Elements That Need Scrolling To Access • 1 month ago

@danielbaylis Excellent. Had no idea Browser was macroable. The scrollTo one works perfectly for my situation. Thanks a lot!

zachleigh left a reply on VueJS Component VS Laravel Blade • 1 month ago

If you don't want page refresh after the user changes something, go with Vue. You should check out the current build a forum series on the site. It covers exactly what you are asking about.

https://laracasts.com/series/lets-build-a-forum-with-laravel

zachleigh left a reply on Is It Possible To Shuffle Lazy Loading Collection? • 1 month ago

In your code, $stack is a model. If you check the api out (link below) you'll see that load returns the model. So you are calling shuffle on a model, which is going to fail.

https://laravel.com/api/5.3/Illuminate/Database/Eloquent/Model.html#method_load

zachleigh left a reply on Dusk Suddenly Can't Find Elements That Need Scrolling To Access • 1 month ago

@bashy Yes, I tried that and had no luck.

@danielbaylis Could be a Chrome issue. Don't know if I feel like managing outdated Chrome versions just to make tests pass though.

I found that same GitHub issue and one comment in there suggested first using type near the element to draw focus. All my failing tests are on forms so I type something near the button before doing the click. Its dirty and I don't like it, but it works.

10th September, 2017

zachleigh started a new conversation Dusk Suddenly Can't Find Elements That Need Scrolling To Access • 1 month ago

I updated composer dependencies yesterday and since then am getting this error on several tests:

Facebook\WebDriver\Exception\UnknownServerException: unknown error: Element is not clickable at point (1450, 2309)

Test code:

$browser->visit(new EditPage()
    ->click('#add');

The element it is trying to access (#add) is simply down the page a bit. It is fully visible and if I pause the test before the click and manually scroll down, the test continues as normal. I'm not 100% sure if the update yesterday is related to the issue, but I can't think of anything else that may have caused this to happen so suddenly. Any body else experiencing this? Any ideas on how to fix it?

5th August, 2017

zachleigh left a reply on Any Tutorials On Laravel Blog • 2 months ago

zachleigh left a reply on How To Add Additional Librery To Bootsrap Js • 2 months ago

bootstrap.js is just a js file that you can use to load up libraries etc. It is included at the top of app.js so anything you put in there will be done before the rest of your js.

zachleigh left a reply on How To Redirect To Another Controller Action? • 2 months ago

Just tried my above code and it works fine here.

zachleigh left a reply on How To Redirect To Another Controller Action? • 2 months ago

But the index method works fine?

zachleigh left a reply on How To Redirect To Another Controller Action? • 2 months ago

Does this work?

public function find(Request $request) {
    return $this->index($request, $events, $searchConf);
}

This will work in other frameworks. Not sure about Laravel, but I don't see any reason why it wont.

zachleigh left a reply on Method Not Loading • 2 months ago

Glad you got it working!

zachleigh left a reply on How To Add Additional Librery To Bootsrap Js • 2 months ago

Did you initiate it anywhere? From the docs:

new WOW().init();

zachleigh left a reply on Method Not Loading • 2 months ago

What does this do?

$this->submission->generateReport();

zachleigh left a reply on Method Not Loading • 2 months ago

What happens when you call the handle method?

zachleigh left a reply on Echo Result From Table To Blade • 2 months ago

What value do you get if you dump our $stove->stove_defra?

{{ dd($stove->stove_defra }}

31st July, 2017

zachleigh left a reply on How To Use Trim When I Used Validation In Laravel ? • 2 months ago

Please be more specific and show us your code.

zachleigh left a reply on How To Handel BadMethodCallException • 2 months ago

Please show us Handler.php.

zachleigh left a reply on Better Web Dev Workflow • 2 months ago

What specifically are you struggling with?

30th July, 2017

zachleigh left a reply on Upload Files To Production Server • 2 months ago

Use git. Its a great way to update your site.

29th July, 2017

zachleigh left a reply on Laravel 5.4 File Put Content • 2 months ago

What do your permissions look like? ls -l in console.

zachleigh left a reply on Laravel 5.4 File Put Content • 2 months ago

Dont use 777 on a production server.

sudo chgrp -R www-data storage bootstrap/cache
sudo chmod -R ug+rwx storage bootstrap/cache

zachleigh left a reply on Run "where Like" In A Table Inserted With The "with" • 2 months ago

You should use a join instead of eager loading. https://laravel.com/docs/master/queries#joins

zachleigh left a reply on Laravel Unwanted Stuff Grrrr • 2 months ago

You can do whatever you want :) bootstrap.js is simply a file to load up js libraries. Remove it if you don't want it. If you don't want to use laravel mix, the yes, you can remove all associated files. Id recommend keeping that though. It makes building front end stuff a breeze. But, its entirely up to you.

zachleigh left a reply on Laravel Unwanted Stuff Grrrr • 2 months ago

Remove whatever you dont want from package.json before running npm install. Then just delete the unwanted stuff from bootstrap.js and app.js.

This is a common complaint and is being dealt with in Laravel 5.5: https://laracasts.com/series/whats-new-in-laravel-5-5/episodes/4

zachleigh left a reply on Call JS Function At The End Of The Page (AJAX Session Check) • 2 months ago

You can use remember me to prevent users from having to log in again. This works, but the csrf token can still expire. For this you can try something like Caffine: https://caffeine.forlaravel.com

zachleigh left a reply on Graphic Chart • 2 months ago

Check out the getting started documentation: http://www.chartjs.org/docs/latest/

zachleigh left a reply on Call JS Function At The End Of The Page (AJAX Session Check) • 2 months ago

@if (Auth::check())
<script type="text/javascript">
    $(document).ready(function() {
        console.log("ping");
        checkSession();
    });
</script>
@endif 

Wouldn't you be just double checking auth?

zachleigh left a reply on Mysql Connection Error • 2 months ago

This could be due to several reasons. Most common is that the sock file doesnt exist. Verify that the file '/tmp/mysql.sock' exists and if it doesn't, see if one exists elsewhere on your system. If you have one somewhere else, set the correct file path in my.cnf.

zachleigh left a reply on Graphic Chart • 2 months ago

An easy solution would be to use a js charting library chart.js.

zachleigh left a reply on Provide A Reason That A Video/series Has Been Archived • 2 months ago

As far as I know, all series are archived when they cease to be relevant. That Vue series was archived not necessarily because Jeff made a new one, but because it was based on an outdated version of Vue. All other archived series that I know about are the same. I suppose Jeffery is the only one can really answer this one though...

19th July, 2017

zachleigh left a reply on BelongsTo • 3 months ago

How about this:

foreach (Tickets::all() as $ticket) {
    var_dump($ticket->tcode_id);
}

dd('finished');

Did all the tickets have a tcode instance?

zachleigh left a reply on How Do I Retrieve The Currently Logged In Email Address And Send A Mail... • 3 months ago

Have you red the documentation about the from method?

https://laravel.com/docs/master/mail#writing-mailables

Not sure, but maybe you could configure this in the mail class?

zachleigh left a reply on Call To A Member Function IsAdmin() On Null • 3 months ago

Might need to use the full class path:

\App\Models\User::existsAndAdmin()

Which makes it just as ugly as the first solution. Id go with a helper function.

zachleigh left a reply on Loading Class In A Service Provider • 3 months ago

Why not just new one up?

public function boot()
{
    $test = new \Acme\Core\Universal();
    $theme = $test::getTheme();
        
    //Base Theme
        $this->app->view->composer('themes.base.layouts.default', 'Acme\Composers\EcommHeaderComposer');
        $this->app->view->composer('themes.base.layouts.default', 'Acme\Composers\NavFooterComposer');
    }

zachleigh left a reply on How Do I Retrieve The Currently Logged In Email Address And Send A Mail... • 3 months ago

Is the email address on the user model? If so, you can simply do this:

Auth::user()->email

zachleigh left a reply on Loading Class In A Service Provider • 3 months ago

Which class cannot be found?

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.