martinbean

martinbean

Member Since 4 Years Ago

Newcastle upon Tyne, UK

Senior Developer at Visualsoft Ltd

Experience Points 494,055
Experience Level 50

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 481
Lessons
Completed
Best Reply Awards 672
Best Reply
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.

21 Jun
3 days ago

martinbean left a reply on What Is The Best Practice For Building Seperate Login For User And Admin In Laravel 5.7 +

@jinsonjose A login form is a login form.

You should have a single user model, and then use authorization to determine what a user can and cannot do in your application.

19 Jun
5 days ago

martinbean left a reply on Controller/Route Design For "frontend Updating"

@oliverbusk The advice from @aurawindsurfing is good. As Adam says, a “resource” in your application doesn’t have to have a corresponding Eloquent model and/or database table.

So in your example of “changeDPI”, you’re merely updating the DPI or an Image resource, so you could model that route as:

Route::put('images/{image}/dpi', '[email protected]');

The same with optimisations. If you’re applying an optimisation to an image, then you could have another nested resource controller for image optimisations:

Route::post('images/{image}/optimisations', '[email protected]');

Each new optimisation is a POST request to that image’s nested optimisation controller.

17 Jun
1 week ago

martinbean left a reply on Sorry, Please Ignore - First Discussion Post - Testing...

@dactex What exactly were you “testing”? :-/

10 Jun
2 weeks ago

martinbean left a reply on Picking Up Implicit Model In Middleware

@bwrigley If your route parameter is {forum_thread} then you can get what’s bound to it in a middleware class like this:

$thread = $request->route('forum_thread');
07 Jun
2 weeks ago

martinbean left a reply on Di Outside Of Controller?

@6acypman Dependency injection works in any class resolved by the service container. The same for methods.

Dependency injection works in controller actions because controller actions are resolved by the container when routing.

05 Jun
2 weeks ago

martinbean left a reply on Interfaces Again

@MINDEXPERIMENT - @mindexperiment No problem at all! Happy to help :)

04 Jun
2 weeks ago

martinbean left a reply on Ideas For Implementing Customer-levels

@REACHED - @reached So it sounds like campaigns have eligibility criteria.

In that case, each user could have a “value” against them, and then you can have a Level model that represents the threshold for that level.

$user = User::create(['name' => 'John Doe', 'value' => 300]);
$gold = Level::create(['name' => 'Gold', 'threshold' => 250]);
$platinum = Level::create(['Platinum' => 'Platinum', 'threshold' => 500]);
$diamond = Level::create(['name' => 'Diamond', 'threshold' => 1000]);

Given the above, John Doe (a user with a value of 300) would fall within the Platinum level (as his value is over Gold’s threshold of 250, but less than Platinum’s threshold of 500).

You can then link a Campaign to a Level, and display eligible campaigns to the user like this:

// Only fetch campaigns that have a level with a threshold greater than the user’s current value
$campaigns = Campaign::whereHas('level', function ($level) use ($user) {
    return $level->threshold > $user->value;
})->get();

martinbean left a reply on Ideas For Implementing Customer-levels

@reached What is an example of a “level”? How does a level affect campaigns? As your customer–campaign relationship is simply a one-to-one, but knowing more about what a “level” is an how it works may change the relationship.

martinbean left a reply on Interfaces Again

@MINDEXPERIMENT - @mindexperiment No problem. I get what you’re trying to do (polymorphism) and it’s absolutely the right way to go with comments.

Unfortunately, when it comes to routing, you’ll need to somehow tell Laravel what model type it is you’re adding a comment on. One approach (which I prefer) it a “nested resources”, where each “comment-able” model has its own corresponding comment controller, i.e. ArticleCommentController. However, they can all just delegate to the same method to actually create the comment:

ArticleCommentController extends Controller
{
    public function store(StoreCommentRequest $request, Article $article)
    {
        $request->user()->commentOn($article, $request->input('comment_text'));
    }
}
EventCommentController extends Controller
{
    public function store(StoreCommentRequest $request, Event $event)
    {
        $request->user()->commentOn($event, $request->input('comment_text'));
    }
}
ProductCommentController extends Controller
{
    public function store(StoreCommentRequest $request, Product $product)
    {
        $request->user()->commentOn($product, $request->input('comment_text'));
    }
}
class User extends Model
{
    public function commentOn(Commentable $model, string $text)
    {
        return $this->comments()->create([
            'text' => $text,
        ]);
    }
}
03 Jun
3 weeks ago

martinbean left a reply on Interfaces Again

@mindexperiment Yes. How else are you expecting Laravel to know what model you’re leaving a comment on?

In your example…

public function comment(Request $request, SubjectInterface $subject);

…just what exactly are you expecting the corresponding route to look like? How are you going to tell Laravel what model type and its ID through a single parameter?

martinbean left a reply on Interfaces Again

@mindexperiment Again, you’re type-hinting an interface.

If you have multiple classes implementing SubjectInterface, how is Laravel supposed to know which one you want to inject?

You can’t do what you’re trying to do. I get you’re trying to DRY your code out, but Laravel just isn’t able to read minds. You can’t type-hint an interface that can have multiple concrete implementations and expect Laravel to know which one you want.

Instead, you need to type-hint a concrete implementation in your controllers:

class ArticleCommentController extends Controller
{
    public function store(StoreCommentRequest $request, Article $article)
    {
        // $article can implement SubjectInterface

        // comment method should type-hint SubjectInterface

        $request->user()->comment($request->all(), $article);
    }
}
31 May
3 weeks ago

martinbean left a reply on Brownfield Testing

@chrisebner It depends on the time and budget you have.

If I inherit a legacy project and want to change some code, I’ll usually write a test for the code in its current condition. I’ll then re-factor, using the test to ensure I’m not breaking anything.

The key to re-factoring is small changes. If you’re only changing small amounts of code at a time, then that makes writing tests around those portions of code easier.

29 May
3 weeks ago

martinbean left a reply on Where Should Basic Logic Be Placed?

IP Blocking

@learn-by-flying This is intercepting a HTTP request and doing something if a condition isn’t met, which makes it a perfect candidate for middleware.

You could create a class that checks the IP address of the user and redirects if it’s not in your whitelist:

class VerifyUserIsUsingVpn
{
    public function handle($request, Closure $next)
    {
        $ipAddressOfVpn = '123.123.123.123';

        if ($request->ip() === $ipAddressOfVpn) {
            return $next($request);
        }

        return redirect()->to('url-containing-connection-instructions');
    }
}

Is there a way to define some master global variables for the entire framework?

I’m guessing you’re using to have a file you include with a load of variables/constants. Instead, you can organise your “global” variables into configuration files.

Without knowing what “global” variables you are wanting to set, I can’t really give an example.

28 May
3 weeks ago

martinbean left a reply on Best Schema For Comments Table Which Has Users And Visitors Comments

i see the current approach is kinda not good

@ahmedsami Why so?

16 May
1 month ago

martinbean left a reply on Testing Gate Policies

@chrisblackwell If you look at a policy, they’re just plain ol’ PHP classes (“POPOs”), so you can just test their methods by passing a user and a model under test:

class ArticlePolicyTest extends TestCase
{
    use RefreshDatabase;

    protected $policy;

    protected function setUp()
    {
        parent::setUp();

        $this->policy = new ArticlePolicy;
    }

    public function testUpdate()
    {
        $user = factory(User::class)->create();
        $article = factory(Article::class)->create();

        // Assert whether user should or should not be able to update article
        $this->assertFalse($this->policy->update($user, $article));
    }
}
14 May
1 month ago

martinbean left a reply on In Axios Request I Got Has Been Blocked By CORS Policy Error

@mstdmstd The resource at example.com:9090/searchWeb needs to add the Access-Control-Allow-Origin header with the value of the domain where /website-blogs is hosted.

13 May
1 month ago

martinbean left a reply on PHP Vs Python- Which Is The Best Programming Language For Development?

@judi Given this is your first post on a PHP-related forum, I’m going to guess you’re trolling.

martinbean left a reply on How Do You Install A Vue.js Package Into Laravel?

@connecteev Read the install instructions for the package.

08 May
1 month ago

martinbean left a reply on Screen Reader Accessibility Inquery

I just want to be sure I'm using the right framework for the job

@stormdragon2976 Laravel is a server-side framework. It delivers HTML to an end user. What’s in that HTML, is up to you.

martinbean left a reply on ERP Open Source

@jmistani And you’re pretty salty if your first post is to write a comeback to a four year old post of mine.

07 May
1 month ago

martinbean left a reply on How To Set Two Isset In View Laravel?

@raviawasti The same way you would in raw PHP:

@if(isset($data['location']->name) && isset($data['subject']->subject_code))
    <!-- is set -->
@endif

The isset function can take multiple arguments though, so you could pass all your conditions and it will only return true if all arguments are set:

@if(isset($data['location']->name, $data['subject']->subject_code))
    <!-- is set -->
@endif

You can also use Blade’s shortcut @isset directive:

@isset($data['location']->name, $data['subject']->subject_code)
    <!-- is set -->
@endisset

martinbean left a reply on Quering A Ldapserver And Synchronize Laravel Database

@ottaviane Laravel’s auth providers and guards and created so that you shouldn’t need to modify your LoginController code to support a different authentication mechanism.

Reading your problem, it sounds like you want to create a new user provider that essentially authenticates using LDAP and synchronises users to a local database table.

I’d go down the route of treating your LDAP directory as a third-party user store, similar to authenticating via OAuth. So your local users might contain the ID of your user in the LDAP directory, name, but no authentication data such as email address and password (as these may change in your LDAP directory).

Laravel has some documentation on creating custom user providers, but I imagine someone will have already created an LDAP user provider for Laravel that you can add to your project, and configure for your installation.

03 May
1 month ago

martinbean left a reply on Referencing All Models At Once In A Controller?

@zaster No. You need to import the classes you need in each file.

If you’re needing to import a lot of classes, then it may be you’re trying to do too much need to break your file into smaller objects that have narrower responsibilities.

martinbean left a reply on Bulk Mailing With Laravel - Right Approach

@thebigk The best approach is to use something like MailChimp or Campaign Monitor. They have the infrastructure, white-listed IPs, and so on.

martinbean left a reply on How To Override Illuminate\Database\Connection Class

@pdm91 Check out Laravel Debugbar. It logs queries executed on your page, how long they’re taking, etc.

martinbean left a reply on Is There A Way To Dynamically Set A Guard Provider?

@alex29 Yeah, this is the very reason I don’t create multiple models to represent a user in my applications.

A user is a user. Use roles and authorization to determine what parts of your application a user can interact with. If a user has a “employer” role, then you can restrict them to only be able to view employer routes and resources.

As soon as you create another “user” model, you have to create additional auth guards, providers, controllers, views, etc and it just becomes a mess to authenticate a user.

02 May
1 month ago

martinbean left a reply on Can Anyone Give Example Of Confirm Password Validation?? I Am Using Following Way But Not Working Properlly.

@MUNAZZIL - @munazzil That won’t work. If you have the following rule:

return [
    'password_confirmation' => 'confirmed',
];

Then Laravel is going to check the value of password_confirmation matches the value of a field named password_confirmation_confirmation.

Docs: https://laravel.com/docs/master/validation#rule-confirmed

martinbean left a reply on Is There A Way To Dynamically Set A Guard Provider?

@alex29 How are you intending to select which guard is needed before actually authenticating the user?

martinbean left a reply on Auth Middleware In Routes Vs In Controller

@hal9k No. You only need to define middleware once—either in a route group, or in your controller’s constructor method.

martinbean left a reply on Process 10000 Of Jobs Per Minute.

@mehedi Well, if jobs take a second or so to process, and you need to process tens of thousands of them, then you need to scale horizontally, i.e. have lots of workers working at the same time like @devfrey says.

martinbean left a reply on Intercepting Laravel Login Error

@ottaviane What is it you’re wanting to do?

martinbean left a reply on Redirect To Intended Page After Login - Laravel 5.7

I don’t think you ever need to put url.intended to session manually. That’s exactly what intended() method does.

@roborobok Wrong. It either redirects to what’s in the session under the url.intended, or redirects to the URL given as the default.

Source: https://github.com/laravel/framework/blob/e6c8aa0e39d8f91068ad1c299546536e9f25ef63/src/Illuminate/Routing/Redirector.php#L98-L112

martinbean left a reply on I Can't Understand The Service Container!

@MohamedAladdin If you think of Laravel, it’s made up of components. So things like queuing, the filesystem, are all components that can have their implementations “swapped” (so for queueing, you can use SQS, Redis, etc).

To make these components available (and swappable), they’re placed in a container. A container is just a central registry of components that you can add things to, and request things from. So when you need to work with queues, you can say the container, “Hey, give me what’s registered for queues”.

To add things to the container, you use service providers. These are just classes that bind things to a key. So you can think of the the container as an array: you set a “key” using a service provider, and can then request a “key” elsewhere in your application.

martinbean left a reply on How To Get Only One Row Of One To Many With Condition?

@aarad You can use any query builder methods, including take():

$products = Product::with(['inventories' => function ($query) {
    $query->where('count', '>', 0)->orderBy('id')->take(1);
}])->orderBy('inventories.price'')->get();

Although I’m not sure the orderBy('inventories.price') will work, as you’ll still have a collection of Inventory models (even though they’ll only be one), as unfortunately that’s just how a has-many relationship works.

martinbean left a reply on Redirect To Intended Page After Login - Laravel 5.7

@nasirnobin You need to set the intended URL before redirecting to login.

$request->session()->put('url.intended', url('/intended-url'));

martinbean left a reply on Catching Wildcards In Routes

@MattB You need to decide how you’re serving images. You can’t upload them to your public directory, but then access the image via a Laravel route.

The server checks if the requested URL is a file. If it is, it just returns that file. If not, then it passes the request through Laravel.

So if you upload a file to /public/images/test.jpg, and then request example.com/images/test.jpg, then the server’s just going to return the test.jpg file without invoking Laravel.

30 Apr
1 month ago

martinbean left a reply on Get Comments On My Company Facebook Page And Display Them On My Laravel Application

@mariomacedo You’ll need to authenticate with Facebook to allow a user to link their Facebook Page with your application. You’ll then get a token that you can make Graph API requests as that Page.

As for comments, comments on what? Everything in the Graph API has an object ID, so you’d need to know the ID of a particular object (i.e. a Post, Photo) before you could fetch photos from it. You can’t just say, “Give me all comments on anything on my Page”. I imagine because it goes against the core functionality of Facebook’s Page Manager.

martinbean left a reply on Catching Wildcards In Routes

@mattb I don’t really follow. What do you mean by “so it can be updated in the db”. So what can be updated in the database? What is it you’re trying to do?

martinbean left a reply on Catching Wildcards In Routes

@mattb If the image actually exists on disk in your public directory, i.e. you’re requesting site.test/images/test.jpg and a file exists at /public/images/test.jpg, then Apache/nginx will just serve that image directly without passing the request through Laravel.

If you don’t want users to be able to access images by their filename then don’t put them in your public directory.

martinbean left a reply on Catching Wildcards In Routes

@mattb What route did you use, what URL did you try, and what response did you get?

29 Apr
1 month ago

martinbean left a reply on Interfaces Again

@Mindexperiment You can’t bind multiple classes to a single interface. Laravel isn’t clairvoyant.

Given this code…

class PublishableController
{
    public function __construct(Publishable $model)
    {
        $this->model = $model;
    }
}

…how is Laravel supposed to know what “publishable” model you want to inject?

26 Apr
1 month ago

martinbean left a reply on I Need Structural Advices For A Complex Logic To Create A Calendar With Events

@leisure_webdev It sounds like you have a similar problem as I’ve been working on the past couple of weeks :D

So, you’d have one entity in your application: Event. This represents an event in your application. When fetching “events” from other data sources (Eventbrite, etc) you’ll convert their representations into an instance of your application’s Event entity. To do this, you’d use a data mapper to convert to/from your Event entity instance to a representation Eventbrite expects.

I don’t really know what your controllers are doing as to what operations you‘re performing but you’d have a DAO class for each service; a data mapper for each service; and one, single Event entity.

$eventbriteDao = new EventbriteDAO;

// Fetch event from Eventbrite.
// DAO will return event in Eventbrite representation.
$eventbriteEvent = $eventbriteDao->find($eventbriteEventId);

// Map the Eventbrite event to Event entity
$mapper = new EventbriteDataMapper;
$event = $mapper->toEvent($eventbriteEvent);

// You now have an Event entity, which you can call methods on
$event->getId();
$event->getName();
$event->getStartDate();

You can then do the reverse to persist the event to Eventbrite again:

$mapper = new EventbriteDataMapper;

// The mapper can convert Event entities to Eventbrite event instances again
$eventbriteEvent = $mapper->fromEvent($event);

// Use the DAO to save the Eventbrite event back to Eventbrite
$eventbriteDao->save($eventbriteEvent);