Nasrul Hazim Bin Mohamad

Laravel: Package Generator


Assalamualaikum / Hi,

It’s been a long time, I wan to update my Laravel Package Generator and now it’s officially updated with version 2.0.2.

Lots of experience, study case, observation, setting up things in Laravel package. Now I believe, it’s ready to speed up your Laravel Package Development.

So, what the Packager do? Basically it creates a basic skeleton of Laravel package which consist of:

  1. Service Provider
  2. Facade
  3. README – with Installation instruction, License Term ready! Other details you need to update manually.
  4. Dependencies – phpunit/phpunit, orchestra/testbench and codedungeon/phpunit-result-printer
  6. phpunit.xml
  7. helpers

See my tweet on this package

That’s all, thanks!

Laravel: Blueprint Macro


Next, the thing that I want to share is how I create Blueprint Macro. Similar to my previous posts on Response and Route.

But this time, we going to add more functionality to Blueprint which used in migrations.

Update your user migration file:

Now your code will be much cleaner.

Feel free to fork and contribute to Blueprint Macro.

Laravel: Route Macro


Here I want to show, how to extend existing route – get, post, etc. for your application.

I’m going to create a new route Route::setting('module'), which will create all necessary route setup.

Now register this new macro in your AppServiceProvider, in register() method.

Then, you can start use the new route.

To use this new route, you need a controller.

So run php artisan make:controller Settings/ApplicationSettingController -r

Then you may add in routes/web.php to register a new route setting.

In this case, you just need to add Route::setting('application').

Then run php artisan route:list. You should get something like the following:

Laravel: Response Macro


It’s quite straightforward to extend a class functionality with Macro. This time I show how to extend the Response class in Laravel.

Create a class in app/Macros/Http called Response.php and add the content as following:

Then register the Macro in AppServiceProvider

Now you may test your new responses:

Laravel: Create Hashed ID for A Resource Using Observer


Install hashids/hashsids package

composer require hashids/hashids

Create a wrapper for hashids

Create a helper for hashids

Create a config file for hashids

Create a migration, which add a hashslug field

Create a helper to get slug name of the given class name (optional)

Create an observer for hashids which monitor on creating new record – if there’s a hashslug column and it’s null, do create hashed id for the record.

Register the observer to model in boot() method in your app/Providers/AppServiceProvider.php

Now let’s test.

php artisan migrate
php artisan tinker
$users = factory(\App\User::class, 10)->create();
$user = $users->first();

You should get something like:

=> App\User {#766
     name: "Lauren McKenzie",
     email: "",
     hashslug: "BgJRlGpQKejb",
     updated_at: "2017-12-29 16:45:43",
     created_at: "2017-12-29 16:45:43",
     id: 1,

Notice the hashslug created in random string (hashed id).

I always use this approach to for my important resources – just to make sure people don’t get easily guess what’s my next record in database.

Happy coding!

P/S: You may encounter an issue – where a lot of models, using the same observer. How do you manage them? Keep it bloated in service provider?

Migrate from WordPress to Laravel – Part 1


Assalamualaikum / Hi,

I’ve been using WordPress, since 2012 – 5 years, of service. I think it’s time to migrate to another platform – Laravel. I’ve been reading how did.

Basically, I got the idea how he did. But, as my sense of explorer, want to learn something new, I read few more articles, packages available out there for migrate from WordPress to Laravel.

I ended up with my own solution.

The idea – use the WordPress REST API.

Of course, I need to prepare my database schema, but I left that part later. What’s important is to grab all the contents from my blog – posts, pages, comments, media, tags, categories and users(since i’m the only author, I don’t have to fetch the user).

Here what I did:

Create the service to call WordPress REST API.

Create an artisan command to fetch all contents.

Do register the artisan command in `Kernel.php`

Then create a `wp` directory in `storage` directory.

So, what I need to do, just call php artisan import:wp and I will get as following, store in storage/wp in JSON Pretty Format.

And that’s it for Part 1.

Next, I need to grab all the media, download it to my storage directory.

See you guys on next post.

Laravel: MoneyPHP Wrapper and Helper


I’ve created a simple MoneyPHP wrapper to simplify my day to day work dealing with MoneyPHP.

and a currency configuration – where you can specify the label for display use, and swift_code for MoneyPHP usage:

And I’ve create a helper in my app/Support/helpers.php, to simplify the usage of the MoneyPHP Wrapper.

A simple usage as following:

money()->toHuman(10000) – use it for display it to end user.

money()->toMachine(10000) – use it for create the right format before use in database or calculation.

money()->toCommon(10000) – use if for common display at the frontend.

Laravel: Managing Frontend from Backend


Assalamualaikum / Hi,

A simple user case: How can you setup your frontend from the backend? Means, how you going to display a view, from a value set in database.

For instance, you have a reference table in database named statuses. In the table, you have id,label,name. How do you use values from database to create view – in term of management and usability?

During seeding / add new data: standardised the label name. In my case, I use lower case.

Then, create views based on generated values in label field.

Then here how you can use it. In my case, I have setup relationship with Course model. So that’s how I access to the label field and get the view for the status.

The Result