martinbean

martinbean

Senior Developer at Visualsoft Ltd

Member Since 5 Years Ago

Newcastle upon Tyne, UK

Experience Points
510,825
Total
Experience

0 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
516
Lessons
Completed
Best Reply Awards
688
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 50
510,825 XP
Nov
22
10 hours ago
Activity icon

Replied to Request Naming Conventions And Structure?

@nakov Asking rhetorically. A request is part of the HTTP layer, which is part of your application layer.

Your domain does not care how it is invoked, be it a HTTP request, a console command, queued job, etc.

Your application layer should have controllers, requests, console commands, etc. Your domain layer should contain the models of your application, services, any repositories, etc.

Nov
21
1 day ago
Activity icon

Replied to Request Naming Conventions And Structure?

If you follow DDD then you will have the requests within your module

@nakov Surely requests belong in the application layer...?

Nov
08
2 weeks ago
Activity icon

Awarded Best Reply on 6.x Adding My Own 2FA Implementation - How To Intercept Auth?

@dlystyr Laravel doesnt offer 2FA out of the box, so youll need to implement your own solution Im afraid.

I dont know about newer versions, but Spark used to intercept a login by overriding the authenticated() in the LoginController. It would immediately log the user out, but put the user elsewhere in the session, and redirect to a page to accept the token. The token would be checked for the user in the session and if it matched, the user was re-authenticated and redirected back to where they wanted to go.

A simple implementation of that would look like this:

class LoginController extends Controller
{
    // Other methods

    public function authenticated($request, $user)
    {
        // Immediately log user out
        Auth::logout();

        // Store user elsewhere in session
        $request->session()->put('auth.id', $user->getKey());

        // Redirect to token form
        return redirect()->route('2fa.token');
    }
}
class TwoFactorController extends Controller
{
    public function showTokenForm()
    {
        return view('2fa.token');
    }

    public function verifyToken(Request $request)
    {
        // Retrieve user from session
        $user = User::findOrFail($request->session()->get('user.id'));

        // Verify token with third-party services
        if ($someTokenService->isValid($user, $request->input('token'))) {
            // Token was valid; reauthenticate user and redirect
            Auth::login($user);
            
            return redirect()->intended('/');
        }

        // Token was not valid; redirect back or show error
    }
}
Activity icon

Replied to 6.x Adding My Own 2FA Implementation - How To Intercept Auth?

@dlystyr Laravel doesnt offer 2FA out of the box, so youll need to implement your own solution Im afraid.

I dont know about newer versions, but Spark used to intercept a login by overriding the authenticated() in the LoginController. It would immediately log the user out, but put the user elsewhere in the session, and redirect to a page to accept the token. The token would be checked for the user in the session and if it matched, the user was re-authenticated and redirected back to where they wanted to go.

A simple implementation of that would look like this:

class LoginController extends Controller
{
    // Other methods

    public function authenticated($request, $user)
    {
        // Immediately log user out
        Auth::logout();

        // Store user elsewhere in session
        $request->session()->put('auth.id', $user->getKey());

        // Redirect to token form
        return redirect()->route('2fa.token');
    }
}
class TwoFactorController extends Controller
{
    public function showTokenForm()
    {
        return view('2fa.token');
    }

    public function verifyToken(Request $request)
    {
        // Retrieve user from session
        $user = User::findOrFail($request->session()->get('user.id'));

        // Verify token with third-party services
        if ($someTokenService->isValid($user, $request->input('token'))) {
            // Token was valid; reauthenticate user and redirect
            Auth::login($user);
            
            return redirect()->intended('/');
        }

        // Token was not valid; redirect back or show error
    }
}
Oct
29
3 weeks ago
Activity icon

Awarded Best Reply on Looking To Improve A Piece Of Code

@danieloplata You could create a custom SurveyCollection, and add a method to that class:

class SurveyCollection extends Collection
{
    public function areComplete(): bool
    {
        // Determine if all surveys are complete by rejecting
        // completed surveys and checking that collection is then empty
        return $this->reject(function (Survey $survey) {
            return $survey->pivot->completed;
        })->isEmpty();
    }
}
@if($surveys->areComplete())
    <div class="text-green-600">All surveys complete</div>
@else
    <div class="text-red-600">You still have surveys to complete</div>
@endif
Activity icon

Awarded Best Reply on Face Recognition Package For Laravel Site

@saleem_hadad Yes, because everyone has the time, budget, resources, and expertise to build their own facial recognition software…

@Mrs_Beginner Amazon has a service, imaginatively called Rekognition (https://aws.amazon.com/rekognition/). And with it being an Amazon Web Service, it’s pay as you go. I’m not sure which would be your closest AWS region, but for U.S. East (North Virginia) it would cost $1 USD per 1,000 images processed in a single month.

Oct
24
4 weeks ago
Activity icon

Replied to Looking To Improve A Piece Of Code

@danieloplata Yeah, you need to add a method to your Eloquent model to tell it what class it should use for collections.

Oct
23
4 weeks ago
Activity icon

Replied to Looking To Improve A Piece Of Code

@danieloplata You could create a custom SurveyCollection, and add a method to that class:

class SurveyCollection extends Collection
{
    public function areComplete(): bool
    {
        // Determine if all surveys are complete by rejecting
        // completed surveys and checking that collection is then empty
        return $this->reject(function (Survey $survey) {
            return $survey->pivot->completed;
        })->isEmpty();
    }
}
@if($surveys->areComplete())
    <div class="text-green-600">All surveys complete</div>
@else
    <div class="text-red-600">You still have surveys to complete</div>
@endif
Oct
08
1 month ago
Activity icon

Replied to Does There Any Website That Offer Tutorials On How To Do Star Rating System In Laravel

@awadgorg ...and the lesson I linked you to will show you how to write your own star rating system!

Activity icon

Replied to Multiple Databases

@emilmoe I don't understand this database-per-customer approach every one wants to do. If you have a hundred customers, are you really wanting to manage, migrate, seed, etc 100 MySQL databases? 101 if you need a database for generic application data as well?

I have a multi-tenant CMS application. It has dozens of customers but only one database. Not once have I thought, "Urgh, wish customer data was in a different database".

Activity icon

Replied to Does There Any Website That Offer Tutorials On How To Do Star Rating System In Laravel

@awadgorg Yes. There's a little website called Laracasts. You may have heard of it. It has a video tutorial on creating a star rating system: https://laracasts.com/series/how-do-i/episodes/22

Sep
23
1 month ago
Activity icon

Replied to Testing Hundreds Of Sites

@jasonfrye You could look into something like Selenium: https://www.seleniumhq.org

Activity icon

Replied to Unit Testing A Model

@xtremer360 I'm not sure what you mean by "checks". I guess your policy should determine who can retire a player, and the unit test for your model should check if, when retire() is called on a Player model, it actually retires that player:

class PlayerPolicyTest extends TestCase
{
    protected function setUp(): void
    {
        parent::boot();

        $this->policy = new PlayerPolicy;
    }

    public function testCannotRetirePlayerThatIsEmployed(): void
    {
        $user = factory(User::class)->make();
        $player = factory(Player::class)->state('employed')->make();

        $this->assertFalse($this->policy->retire($user, $player));
    }

    public function testCannotRetirePlayerThatIsAlreadyRetired(): void
    {
        $user = factory(User::class)->make();
        $player = factory(Player::class)->state('retired')->make();

        $this->assertFalse($this->policy->retire($user, $player));
    }

    public function testSuperAdministratorCanRetirePlayer(): void
    {
        $user = factory(User::class, 'super_administrator')->make();
        $player = factory(Player::class)->state('eligible_to_retire')->make();

        $this->assertTrue($this->policy->retire($user, $player));
    }

    public function testAdministratorCanRetirePlayer(): void
    {
        $user = factory(User::class, 'administrator')->make();
        $player = factory(Player::class)->state('eligible_to_retire')->make();

        $this->assertTrue($this->policy->retire($user, $player));
    }
}
class PlayerTest extends TestCase
{
    use RefreshDatabase;

    public function testPlayerCanBeRetired(): void
    {
        $player = factory(Player::class)->state('eligible_to_retire')->create();

        $this->assertFalse($player->is_retired);

        $player->retire();

        $this->assertTrue($player->fresh()->is_retired);
    }
}
Activity icon

Replied to Refactor Huge Eloquent Query

@miggi You could move a lot of those conditional when() clauses to a filter class, like described in this video: https://laracasts.com/series/eloquent-techniques/episodes/4

Activity icon

Replied to Different Types Of Tags/categories For A Model

@zefex It sounds like you have three models, then: an Ad, a tag type, and tags.

Activity icon

Replied to User Course Watched Videos Relationship

@abdifatah Well, you need to get the number of videos in a course, the number of watched videos in the course, and then divide that by the total:

$totalVideos = $course->videos()->count();

return ($watchedVideosCount / $totalVideos) * 100;
Sep
18
2 months ago
Activity icon

Replied to Best Way For Sub Apps In Laravel

@engrlaravel Just add a prefix to your routes?

Route::bind('company', function ($slug) {
    return Company::where('slug', '=', $slug)->firstOrFail();
});
Route::prefix('/{company}')->group(function () {
    // Your company routes
    
    Route::get('/news', '[email protected]');
    // If {company} is acme, URI will be /acme/news
});

It will mean each of your controller actions will need to take a company model (or whatever your model is called) instance as its first parameter:

class ArticleController extends Controller
{
    public function index(Company $company)
    {
        return $company->articles()->paginate();
    }
}
Sep
15
2 months ago
Activity icon

Replied to Best Way For Sub Apps In Laravel

@engrlaravel So you want to redirect somewhere after logging in? In that case, you'll need to override the redirect path to look up the user's company, and then redirect accordingly:

use Illuminate\Http\Request;

class LoginController extends Controller
{
    public function authenticated(Request $request, $user)
    {
        // Look up company for user
        // Change this to however you associate a company with a user
        $company = $user->company;

        // Redirect to company URL instead of default redirect URL
        // If $company->slug is acme, will redirect to /acme
        // Again, you will need to change this if column name is different
        return redirect()->intended($company->slug);
    }
}
Sep
12
2 months ago
Activity icon

Replied to My Testing Taking Too Much Time To Pass

@omda Again, look at what your test case is doing and try and find any bottlenecks there. We can't really tell much from your phpunit.xml file.

Sep
11
2 months ago
Activity icon

Replied to My Testing Taking Too Much Time To Pass

@omda Which test? Look into what the test is testing and find the bottleneck.

Activity icon

Replied to Best Way For Sub Apps In Laravel

@engrlaravel I don't really understand. A URL is a URL?

Sep
10
2 months ago
Activity icon

Replied to Best Way For Sub Apps In Laravel

my idea is to create folders (customer1,customer2...) inside laravel and re-upload whole application in each folder?

@engrlaravel So you want to maintain more than 200 applications?

Have middleware that inspects the incoming host name and try and match it to a customer. If there is a match, you can load your customer routes:

class DetectCustomerHostname
{
    public function handle($request, Closure $next)
    {
        $hostname = $request->getHost();

        $customer = Customer::whereHostname($hostname)->first();

        if ($customer) {
            // Load customer routes
        }

        return $next($request);
    }
}
Sep
06
2 months ago
Activity icon

Replied to Percentage Difference Between Dates

@inspiredprynce You still need a starting point! You can't just have a percentage between two arbitrary dates. That's like asking, what's the percentage between 10 and 15? Doesn't make sense.

Activity icon

Replied to Laravel 6: What's Covered By Semantic Versioning

@alanstorm The public API of Laravel is the classes and methods you use. Going forward, any changes to these classes and methods that would "break" applications should become a major release. So expect these more frequently.

Aug
28
2 months ago
Activity icon

Replied to XSS Vulnerability In Laracasts.com

@itbazen Security issues are something you should keep private rather than publicly disclosing them.

If Jeffrey hasn't replied or addressed the issue, feel free to send him another email.

Activity icon

Replied to Question Regarding Best Practice For Controller Methods

@tomhartley97 Closing an issue could simply be sending a PUT request to the URL for that issue and setting a status parameter or similar:

$client->put('/issues/{id}', [
    'form_params' => [
        'status' => 'closed',
    ],
]);
Aug
27
2 months ago
Activity icon

Replied to Anyway To Pass Parameter Into Eloquent Function?

@kankai You don't need to use scopes. Eloquent relationships return query builder instances, which you can add methods on to further refine the relationship:

$detail = $game_id->detail()->whereDate('created_at', '=' $date)->get();
Aug
23
2 months ago
Activity icon

Replied to Migration Solution From Codeigniter To Laravel

@arjunkaimattathil Yes. It's called a development team.

There's no tool that will convert an application built in CodeIgniter to an application written in Laravel.

Activity icon

Replied to Need Help With Laravel Model's Design

@aktheon I am struggling to work out what your model methods are doing. You say they are in a UserData class but then you are just using the DB facade?

If you have resourceful controllers, then just use the models as intended:

class UserDataController extends Controller
{
    public function index()
    {
        return UserData::paginate();
    }

    public function store(StoreUserDataRequest $request)
    {
        return UserData::create($request->validated());
    }

    // And so on...
}
Aug
22
3 months ago
Activity icon

Replied to Track Down The Condition Of The Device In Database

@left A device has a current condition, so store it against the device. But by all means, store the condition against purchases as well (as I assume a device can degrade in condition over time, or be restored by an owner). The current condition of an item can then be changed between purchases.

Activity icon

Replied to Route Model Binding

@jaheller If you are passing all three parameters to your controller action, you could look all three up using findOrFail() queries, then check each parameter belongs to the previous one.

Route::bind('{category}', function ($slug) {
    return Category::whereSlug($slug)->firstOrFail();
});

Route::bind('{subCategory}', function ($slug) {
    return SubCategory::whereSlug($slug)->firstOrFail();
});

Route::bind('{product}', function ($slug) {
    return Product::whereSlug($slug)->firstOrFail();
});
class ProductController extends Controller
{
    public function show(
        Category $category,
        SubCategory $subCategory,
        Product $product
    ) {
        abort_unless($product->sub_category_id == $subCategory->id, 404);
        abort_unless($subCategory->category_id == $category->id, 404);

        // Display product
    }
}
Aug
20
3 months ago
Activity icon

Replied to How To Design Multi Vendor Ecommerce

@remember-me Are you wanting something to do everything for you...?

You're asking a massively open-ended question. Try refining it a bit. It's like asking how to make Facebook, or Google...

Activity icon

Replied to Multiple Laravel Codebases And User Tables

@laracastsluvr Why are you so hell-bent on having separate users? A user is a user. If your application has multiple front-ends, then use authorisation to restrict what a user can see based on their role.

Activity icon

Replied to Laravel Nova Licensed

@deric1439 No. Nova works on trust as far as I am aware.

Activity icon

Replied to How To Integrate Razorpay Payment Gateway In Laravel 5.5 ?

please give some suggestion how to integrate razorpay step by step

@van1310 No one is going to write you a step-by-step guide on how to integrate a payment gateway.

Activity icon

Replied to Using Laravel Passport For User Authentication

Hi, is good using Laravel Passport for authenticating users?

@hakhsin As opposed to what?

Activity icon

Replied to Laravel Nova Licensed

@deric1439 This exact use case is covered in the Nova FAQs:

Can I ship a copy of Nova in my own product? Nova is not intended to be bundled with any other product or service. However, the code you write for Nova (custom resource tools, metrics and cards, etc) are completely yours to do with what you like. That means you may charge for your Nova packages or any "pre-packaged" Nova resource, etc.

Aug
19
3 months ago
Activity icon

Replied to How To Login/Register From Client Using API Token Authentication

@suli API tokens and Passport are pretty much the same thing when boiled down. Passport allows you to add OAuth to your application, in which your application will issue tokens that you can then use to make subsequent API requests. The "token" guard is just a simpler implementation where a token is stored with user records in your database, rather than issued via OAuth.

If you use the simple token approach, then you're going to need a registration endpoint that will create a user, generate an API token, and return that token to that user. The user will need to store it to then be able to perform any additional requests. If they lose that token, then there's no way to retrieve it (other than you going in the database, reading it, and sending it to the user, but you shouldn't be doing that).

With Passport, users can authenticate via OAuth. Again, you'll still need an endpoint to allow a user to register. After they've registered, they can use Passport to retrieve a token. There's the redirect flow where they're sent to a page in your application and asked if the app should have permission to use their account (similar to logging in with Facebook or Twitter). If the user accepts, they'll be sent back to the application with a code they can exchange for an access token. These tokens usually have a limited lifetime and issued in tandem with a refresh token that can be used to request a new, valid token when the current one expires.

Aug
13
3 months ago
Activity icon

Replied to Constructive Feedback Request -- Controller Method Optimization

@jcgivens21 No problem. Glad you found it useful :)

Activity icon

Replied to Product Architecture

@dippy How are users expected to log in to your system? Should they log in and out using each email address? Or can they log in with one email address and still access the other companies they are a member of?

Activity icon

Replied to Laravel Shopping Cart Package

@adiksudip Well what are you requirements? Features? Number of products. "Best" is such a subjective thing.

Activity icon

Replied to Constructive Feedback Request -- Controller Method Optimization

@jcgivens21 Looks like a good start. In response to your actual questions:

$this->validate: Should I move this validation logic into a Request class? Or is this generally overkill?

I always place validation in form request classes. I prefer my controllers to only be dealing with a valid request, not validate the request and then do the processing.

I'm creating the Employee here and saving it directly in the controller. Is it better to decouple this logic? I feel I've "slimmed" my controller based on the number of methods, but there's still a huge amount of text here, and I'm not sure if this is the best place to handle this Employee saving logic.

Instead of assigning each property individually, you could make your attributes fillable in your model and then use create():

$employee = Employee::create($request->validated());

Note: ->validated() will only work if your request does the validating.

In line with #2, there is some condition-based logic that when the user is inputting the employee data, they select whether the employee is paid Hourly or by Salary, and then I accept either of those values from the form, and I also calculate the other value even if it isn't sent and store both in the database table, in addition to whether it "is_salary" so I know when displaying the employee whether or not to display the values as hourly or salary. Condition-based logic like this is best in this controller logic?

This depends on a case-by-case basis. If I find theres lots of conditionally branching, I may move the code that deals with persisting a record to its own job class, and then dispatch that synchronously:

$employee = CreateEmployee::dispatchNow($request->validated());

Also with #3, there is some calculations being done in addition to the condition-based logic. Should calculation results be stored in the model/database tables or should those be handled real-time? It was simpler when I started to just store everything in the table without worrying about calculating these values upon request--but in proper design, should only 1 of those values be saved to the database and the other always calculated?

Again, it depends on a case-by-case basis. In this case, you seem to be manipulating one of two values depending if the employee is salaried or not. You could maybe move this logic to an mutator in that case:

public function setWageAttribute($value)
{
    if ($this->is_salary == 0) {
        $this->hourly_rate = $value;
        $this->salary = $value * 40 * 52;
    } else {
        $this->hourly_rate = $value / (40 * 52);
        $this->salary = $value;
    }
}

This of course depends on is_salary being set on your Employee model instance first.

$employee->is_salary = 1;
$employee->wage = 40000;

Once the employee is added, I have a Toastr notification (basically a popup) that informs the user that their form submission was successful. I do this in basically EVERY function in the program, and in my Layouts.app blade template, it just picks up these Toastr notifications. I feel this is a very good implementation, and it works well for me, but I'm open to criticisms of this.

My only criticism is, its directly tied to an implementation. Personally, I just redirect and set success or error messages in the session:

return redirect()->route('event.index')->withSuccess('Event was deleted.');

Ill then have the view do something with that, i.e. display it in the header, as a toast pop-up, etc. If I want to change how flash messages are displayed on the front-end, then I just have to do it there rather than go through all of my controller actions and change instances of Toastr::success() to whatever Im going to use instead.

On the employee creation submit form, there are 2 submit buttons. One button (which is labeled "Submit" is just to submit the employee and then redirect the user back to the page that displays all employees. The second button is labeled "Submit & Return To This Page" and its function is to return the user back to the same form so that they don't have to click an additional button to get back here to add more employees. In the controller, I just check which button was submitted and redirect based on which button was pressed. Any thoughts on this setup?

No real thoughts. It's conditionally logic, so it's fine to have an if statement change where the controller redirects the user to.

Other general thoughts?

None other than those above! Essentially:

  • Put validation in form requests.
  • Try to use mass-assignment rather than setting properties individually.
  • Try to keep things generic, rather than tied to specific implementations.

Following the above, you may end up with controller actions that look like this:

public function store(StoreEmployeeRequest $request)
{
    $employee = Employee::create($request->validated());

    $url = $request->add_and_return_button ? back() : route('employee.show', $employee);

    return redirect()->to($url)->withSuccess('Employee has been added.');
}
Aug
12
3 months ago
Activity icon

Replied to Sharing Users Between Multiple Laravel Projects

@jamespavett If you are going to have multiple applications authenticating against the same database of users, then I'd lean towards having a "single sign-on" server.

It will essentially be another Laravel application that contains your users and has Passport installed. Your applications would then authenticate against your Passport server.

If users should log in to your application using an email address and password, then you will want to use password grant tokens. If you want to redirect to your SSO server and have users approve or rejects your applications using their data (think authenticating with Facebook or Twitter), then you want to implement the redirect flow described here: https://laravel.com/docs/5.8/passport#issuing-access-tokens

Activity icon

Replied to Laravel Basics

define additional class files

@2015u058 Your question makes no sense. You put classes in their own files. No other file "defines" a class other than the file the class itself is in!

I suggest you watch some of the introductory videos here on the Laracasts if you're unsure which files to put classes in.

Activity icon

Replied to How To Use Laravel Session?

@hjortur17 Usually you create an order in a "new" or "unpaid" state, redirect to the off-site payment gateway (passing the order ID), then your website marks the order as paid when returning from the gateway after a successful payment.

Activity icon

Replied to Unable To Prepare Route [/] For Serialization

@tesoro Read the error message. It tells you exactly what the problem is.

LogicalException: Unable to prepare route[/] for serialization. uses closure.

Error messages are there for a reason. To tell you what has gone wrong and diagnose.

Activity icon

Replied to Applying Authorisation Policy For Nested Resource

@pauladams8 Hey. Yeah, there seems to be two things at play in your example: authorisation (can this user view this question?) and data integrity (does this answer belong to this question? Does this comment belong to this answer?).

In this scenario, Ive opted for a separate middleware to do the data integrity checks and throw a 404 error:

class CommentController extends Controller
{
    public function __construct()
    {
        $this->middleware('can:view,question');
    }

    public function show(Question $question, Answer $answer, Comment $comment)
    {
        abort_unless($comment->answer->is($answer), 404);
        abort_unless($answer->question->is($question), 404);

        // If here then comment belongs to answer and answer belongs to question
    }
}