ainsley_clark

ainsley_clark

Member Since 8 Months Ago

Experience Points
840
Total
Experience

4,160 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
1
Lessons
Completed
Best Reply Awards
0
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 1
840 XP
Feb
21
1 day ago
Activity icon

Replied to Inherit $redirectTo From AuthController Parent

So I got it working somehow, but wondering if this is the best approach to overwriting all the redirectTo paths. I have used a trait for an adminCheck. Please let me know if there is a neater way of going about this.

Many thanks in advance.

<?php

namespace App\Concerns;

use Illuminate\Support\Facades\Auth;

trait AdminCheck {

    /**
     * Checks admin path before logging in.
     *
     * @return string
     */
    public function redirectPath()
    {
        $user = Auth::user();
        if ($user->role_id == 2) {
            return '/admin';
        }

        return '/hello';
    }
}

LoginController

    use AuthenticatesUsers,
        AdminCheck {
        AdminCheck::redirectPath insteadof AuthenticatesUsers;
    }

RegisterController

    use RegistersUsers,
        AdminCheck {
        AdminCheck::redirectPath insteadof RegistersUsers;
    }
Activity icon

Started a new Conversation Inherit $redirectTo From AuthController Parent

Hi,

I'm trying to implement a dynamic $redirectTo variable on the ResetPasswordController, LoginController, RegisterController etc.

I have an AuthController, and all of the classes above inherit from it.

However the redirect paths don't seem to be working and just default to '/login';

AuthController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class AuthController extends Controller
{
    /**
     * The redirect path when logged in.
     *
     * @var string
     */
    protected $redirectTo;
    /**
     * The current user details.
     *
     * @var
     */
    protected $user;

    /**
     * AuthController constructor.
     */
    public function __construct()
    {
        parent::__construct();

        $this->middleware(['auth', 'verified']);
        $this->middleware(function ($request, $next) {
            $this->user = Auth::user();
            $this->adminCheck();
            return $next($request);
        });
    }

    /**
     * Checks if the user is a customer or admin.
     */
    public function adminCheck()
    {
        if ($this->user->role_id == 2) {
            $this->redirectTo = '/admin';
        }

        $this->redirectTo = '/dashboard';
    }

ResetPasswordController

<?php

namespace App\Http\Controllers\Auth;

use App\Http\Controllers\AuthController;
use App\Http\Controllers\Controller;
use App\Providers\RouteServiceProvider;
use Illuminate\Foundation\Auth\ResetsPasswords;

class ResetPasswordController extends AuthController
{
    /*
    |--------------------------------------------------------------------------
    | Password Reset Controller
    |--------------------------------------------------------------------------
    |
    | This controller is responsible for handling password reset requests
    | and uses a simple trait to include this behavior. You're free to
    | explore this trait and override any methods you wish to tweak.
    |
    */

    use ResetsPasswords;

//    /**
//     * Where to redirect users after resetting their password.
//     *
//     * @var string
//     */
//    protected $redirectTo = RouteServiceProvider::HOME;

    public function __construct()
    {
        parent::__construct();
    }

}

Activity icon

Replied to Customer Verification View - Logic

Amazing thank you @bobbybouwmann, Is there anyway to return to the '/dashboard' once verified with data?

For example:

/**
 * Once verified return view with data
 * 
 */
return view('dashboard', [
    'verfieid' => true
]);
Activity icon

Started a new Conversation Customer Verification View - Logic

Hi there,

I'm trying to return a custom view for email verification when a user signs up. The email verification gets sent and clicking the link updates email_verified_at. However when you click the register button, it goes straight to0 '/dashboard' instead of showing the verify route. With 'you need to verify your email address' etc.. So it goes straight to the backend without being verified.

AppServiceProvider:


/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    VerifyEmail::toMailUsing(function ($notifiable) {
        $verifyUrl = URL::temporarySignedRoute(
            'verification.verify',
            \Illuminate\Support\Carbon::now()->addMinutes(Config::get('auth.verification.expire', 60)),
            [
                'id' => $notifiable->getKey(),
                'hash' => sha1($notifiable->getEmailForVerification()),
            ]
        );

        return new EmailVerification($verifyUrl, $notifiable);
    });
}

EmailVerification:

<?php

namespace App\Mail;

use Auth;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class EmailVerification extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $baseUrl;
    public $verifyUrl;

    /**
     * Create a new message instance.
     *
     * @param $verifyUrl
     * @param $user
     */
    public function __construct($verifyUrl, $user)
    {
        $this->user = $user;
        $this->verifyUrl = $verifyUrl;
        $this->baseUrl = env('APP_URL', 'http://www.mywebsite.com');
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $this->to($this->user->email);
        return $this->view('email.emails.welcome');
    }
}

Any help would be greatly appreciated,

Feb
17
5 days ago
Activity icon

Replied to Call Custom Blade Directive Outside Service Provider?

Sorry presuming it would be in the setup function?

Activity icon

Replied to Call Custom Blade Directive Outside Service Provider?

@sinnbeck thank you so much for your help.

If I'm splitting the methods up into different functions (the blade directives).

Where would be the best place to call them all?

Activity icon

Replied to Call Custom Blade Directive Outside Service Provider?

Hi @sinnbeck

Thanks for your reply again.

My project is getting bigger and bigger and I was just looking to make my codebase more maintainable.

Would something like this work?


<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    use BladeFunctions;

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        $this->setupBladeDirectives();
    }
}
<?php



namespace App\BladeFunctions;

trait BladeFunctions {

    public function setupBladeDirectives() {
        \Blade::directive('directive_name', function ($expression) {
            return $expression;
        });
    }

}
Feb
16
6 days ago
Activity icon

Replied to Call Custom Blade Directive Outside Service Provider?

Hi @sinnbeck

Sorry if I wasnt clear.

I'm trying to use custom blade directives in a seperate file (not a service provider) like a bladefunctions.php file.

I dont know if theres a way of calling a file within the service provider?

Thanks again.

Activity icon

Started a new Conversation Call Custom Blade Directive Outside Service Provider?

Hi,

Just wondering if it's possible to have custom blade directives outside of the service provider?

In turn, the service provider will then call a .php file and include the functions?

Thanks in advance.

Feb
11
1 week ago
Activity icon

Replied to Laravel - Extremley Slow When There's An Error?

@sinnbeck thank you so much, its unrecognisably quicker after that quick fix.

If anyone else is facing this issue:

  • php artisan vendor:publish --provider="Facade\Ignition\IgnitionServiceProvider" --tag="flare-config"
  • Set app/config/flare.php - 'collect_git_information' => false,
Feb
10
1 week ago
Activity icon

Replied to Laravel - Extremley Slow When There's An Error?

@fylzero thanks for your reply.

If I create a typo in a use statement its 20 seconds. Or miss spell a function name it's way over 30.

The problem is not just limited to the AppServiceProvider. It was just a crude example.

Activity icon

Replied to Laravel - Extremley Slow When There's An Error?

Thanks for your replies.

@sinnbeck I will see if this helps.

@siangboon this was just a demonstration. It happens with any error, literally anything.

Perhaps its something to do with homestead?

Activity icon

Started a new Conversation Laravel - Extremley Slow When There's An Error?

Just wondering if anyone else has experienced extremley slow response times when there's an error in Laravel? It took just shy of 30 seconds to produce this page.

image

Feb
06
2 weeks ago
Activity icon

Replied to Custom Request Validation Class, Outside Controller?

Thanks for your reply @snapey

The store() method used to create a resource isnt a controller though. Basically I would like to use the validate class (with messages and rules) without having to instantiate it when creating a resource class.

I need to pragmatically detect when validation has failed to throw an error.

I hope that makes sense.

Feb
05
2 weeks ago
Activity icon

Started a new Conversation Custom Request Validation Class, Outside Controller?

I have a request classed shown below for storing resources. As I'm not using this validation class in a controller but a separate class, what would be the most, simplest, proficient way to inject this into the resource class? Presuming instantiating it seems a bit long winded?

I would like to use the Validate::make method in order to determine if its failed etc, unless theres a better way?

Thanks in advance.

    /**
     * Resource constructor.
     */
    public function __construct()
    {

    }

    /**
     * Creates a resource.
     */
    public function store($data) 
    {
       // Use validate make here?
    }
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return false;
    }

    /**
     * Get the validation rules that apply to the a resource.
     *
     * @return array
     */
    public function rules($resourceID = false)
    {
        $rules = [
            'name' => 'required|unique:resources,name',
            'friendly_name' => 'required',
            'slug' => 'unique:resources,slug',
            'theme' => 'required',
        ];

        if ($resourceID) {
            $rules['name'] = 'required|unique:resources,name,' . $resourceID;
            $rules['slug'] = 'unique:resources,slug,' . $resourceID;
        }

        return $rules;
    }

    /**
     * Get the validation messages that apply to a resource.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'required' => 'A :attribute name must be defined.',
            'name.unique' => 'The name \':input\' has already been defined.',
            'slug.unique' => 'The slug \':input\' has already been defined.',
        ];
    }
Jan
29
3 weeks ago
Activity icon

Replied to Namespacing For Facades

Hi @alishahuk

Thanks for your reply.

Yep thats loaded in and working.

    "autoload": {
        "psr-4": {
            "App\": "app/",
            "Core\": "core/"
        },
Activity icon

Replied to Namespacing For Facades

Hmmm I appreciate your help but diverting me to the Laravel documentation, when I said I have read it isn't very constructive.

I'm pretty new to this stuff and as I'm sure you can imagine it's very daunting when trying to reach out to people for help.

Thanks anyway.

Activity icon

Replied to Namespacing For Facades

Sorry @jlrdw you have lost me, I have a read the documentation.

  • The __callStatic method you mentioned above, where would I implement that?
  • Can you elaborate on 'probably just do instance methods'?
  • Again not sure what you mean by static looking calls.

Thanks.

Activity icon

Replied to Namespacing For Facades

Hi @jlrdw

Thanks for your reply.

I'm using a facade because I would like to use different methods inside a class to validate a file.

How would I obtain a 'use' structure similar to above?

Activity icon

Started a new Conversation Namespacing For Facades

Hi,

I'm trying to use facades with namespacing similar to that of Laravels. I have a JSON class that will eventually do some parsing etc and it resides in Core/Util/JSON which is sits in the root of the Laravel install.

Currently I add the following to the top of the file I want to use the class in, PHPStorm is indicating that the class is undefined.

use JSON;

Ideally I would like to have

use Core\Util\JSON;

Im not sure how you Laravel references Facades and still being able to use the structure use Illuminate\Support\Facades\DB;

Class

<?php

namespace Core\Util\JSON;

class JSON {


    /**
     * Validates JSON File
     *
     * @param $json
     */
    public function validate($path)
    {
    //do something
    }

Facade

<?php

namespace Core\Util\JSON;

use Illuminate\Support\Facades\Facade;

class JSONFacade extends Facade {

    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'json';
    }

}

app.php

'JSON' => Core\Util\JSON\JSONFacade::class,
Core\Util\JSON\JSONServiceProvider::class,

Service Provider

<?php

namespace Core\Util\JSON;

use Illuminate\Support\ServiceProvider;

class JSONServiceProvider extends ServiceProvider
{
    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        $this->app->bind('json', function() {
            return new JSON();
        });
    }
}

Many thanks for your help in advance.

Jan
25
4 weeks ago
Jan
24
4 weeks ago
Activity icon

Started a new Conversation Database Schema Review / Questions For CMS

Hi there,

I'm in the mist of creating a Laravel application that will closely mimic Wordpress. The thought process is quite similar. You are able to create resources from a config.json file that sits in the themes directory add custom fields with flexible content (similar to that of Advanced Custom Fields).

As this is one of my first big builds I have a couple of questions regarding fields & categories:

Categories:

A developer is able to add a resource (such as news, or posts) and each resource can have a category which is labelled and inputed into the category table. A category can belong to more than one resource. What I can't wrap my head around is how to separate these two tables (categories and category content), what might feature in a different table and what I might even name it!

Fields

An administrator is able to create field groups that can belong to resources or pages. A field group would contain an array a fields, which would contain data such as type (richtext, name, required, conditional logic etc..). How would this data look in a schema? Would would be best pratice?

Any help would be greatly appreciated, I'm new to this world!

Please feel free to provide any constructive criticism regarding this design.

Many thanks.

EDR Diagram can be found here - CMS - SqlDBM Link

CMS Schema

Jan
22
1 month ago
Activity icon

Replied to Binding Not Working As Expected.

Thank you @skauk so much.

I need it was simple!

Activity icon

Started a new Conversation Binding Not Working As Expected.

Hi there,

Im trying to bind a simple class to my app in AppServiceProvider

use App\Helpers\Test;

/**
 * Register any application services.
 *
 * @return void
 */
public function register()
{
    $this->app->bind(Test::class, function() {
        return new Test('Hello, app!');
    });
}

Test is in App\Helpers like:

public function __construct($message)
{
    dd($message);
}

How is it not printing a message?

Thanks.

Jan
21
1 month ago
Activity icon

Replied to Adding View Locations Dynamically In Model

Hi @artcore

Many thanks for your reply.

Would there be anyway to put this logic in a class which is instansiated in a service provider?

Jan
20
1 month ago
Activity icon

Started a new Conversation Adding View Locations Dynamically In Model

Hi there,

Im just getting used to the Service Container & Provider (tricky stuff). I'm trying to add dynamic view locations in Laravel through a Model/Manager. I'm using this for dynamic theming.

Originally I have this, in a ViewServiceProvider.

public function registerViewFinder()
{
    $this->app->bind('view.finder', function ($app) {
        $paths = $app['config']['view.paths'];

        $test = dirname(base_path()) . '/themes';
        $themes = scandir($test);
        unset($themes[0]);
        unset($themes[1]);

        foreach ($themes as &$theme)
        {
            $themeLocation = $test . '/' . $theme . '/views';
            array_push($paths, $themeLocation);
        }

        return new FileViewFinder($app['files'], $paths);
    });
}

This works fine, but It would be great to encapsulate this logic into a more centralised location, perhaps a Theme Object, so I tried to bind a the ViewFinder to the Theme Class

public function register()
{
    $this->app->singleton( ThemeContract::class, function ($app) {
        return new \App\Models\Theme($app, $this->app['view']->getFinder());
    });
}

Add in the Theme Model:

public function __construct($app, ViewFinderInterface $finder)
{
    $this->app = $app;
    $this->finder = $finder;
    dump($this->finder);
    $this->finder->addLocation('.../cms/test/');
    dump($this->finder);
}

Finder add location is actually adding it into the paths, as shown in the dump, but the views are not being returned correctly.

Is there anyway to have this logic in the same spot without going into namespaces?

Any help would be very much appreciated.

Jan
17
1 month ago
Activity icon

Replied to Run DB Setup For CMS Only Once

Thanks for your reply @kevinblank

Im looking to insert data into an options table by reading the config file (json format). Presumbly I cant run functions before a migration so I'm looking for a way to insert it once when setting up.

Activity icon

Started a new Conversation Run DB Setup For CMS Only Once

Hi there,

I'm trying to wrap my head around some logic and wondering if you could help.

Im making a CMS, it scans a directory and gets a json file and goes on to create tables & performs some actions to get the database set up, based on the json data.

Im wondering how to run this function only once? How you would approach this?

Below is an example of what function I would be running.

/**
 * Set global theme
 */
public function setTheme($theme)
{
    if (!$theme) {
        return false;
    }

    $themeInfo = $this->getThemeConfig($theme)->theme;

    //Insert config to DB

}

Many thanks in advance.