Orkhan's Blog on software development

Rendering response from Laravel exception

In your Laravel controller or somewhere in your code you probably have a try/catch like this:

use App\Exceptions\FailedRequestException;

class ApiController
{
    public static sendRequest()
    {
        $api = new SomeApi();
        
        try {
            $api->sendRequest();

            return response()->json([
                'data' => $api->getData(),
            ]);
        } catch(FailedRequestException $e) {
            return response()->json([
                'error' => 'Failed to send API request',
            ]);
        }
    }
}

read more

Using traits to boot and initialize Eloquent models

If you ever used Eloquent events, you are probably aware of special boot() static method in Eloquent models. This method allows you to hook into special events by running given functions.

Here’s an example, let’s say we have a Post model and when we are creating a new post, model needs to generate slug attribute based on name.


read more

Vue.js v-model binding on custom components

When working with form elements, Vue’s v-model directive comes useful for 2-way data binding. But when you create your own custom form components and wrap input elements inside, it becomes hard to maintain the same 2-way binding with custom form component and parent component.


read more