Saturday, October 14, 2017

Designing database for your Laravel Application

Laravel is one of the trending PHP frameworks for a couple years now. Most of its advantages and benefits are all said from the post I linked. From Laravel helpers, authentication tool, database migration and many more.

Of course, with the support of its community some developed a tool that to make the development even more faster.

Since most of the web / online systems development heavily involves database design, I would consider Laravel is second to none when it comes to its database tool. There's the migration and schema builder. It eases the database table creation and updates. If you are in a huge team, table design or updating are easily done by using migration tool.

On top of these tools, I personally recommend  Laravel Schema Designer website as part of database design. Laravel SD features helps its users to create, export and share your Laravel Schema. As a typical database design, you can design tables, add fields to it, add relationships to other tables. Since this is mainly for Laravel, the field type options such as nullable, fillable etc are all available. Here's a simple illustration I screenshot.


The best in all of this is the complete export which includes migration, controller, model with the relationships as designed like this.

namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
    protected $table = 'users';
    public $timestamps = true;
    public function role()
    {
        return $this->belongsToMany('Role');
    }
    public function attempts()
    {
        return $this->hasMany('Login_attempts');
    }
}
When all are set for your migrations, you can copy it to your laravel installation migration folder and execute the migration command to generate your schema. Some other files such as controllers are also available at php artisan commands so you might not need them anyway. In my case, I only use migration files.

That's what I can share for now.

I believe there are also other tools that is similar to the one I've posted. You can comment the tools you use in your Laravel development below.

Thursday, September 21, 2017

jQuery ajax post not working in Laravel 5.3

Here's the issue jQuery ajax post not working in Laravel 5.3 (could be occurring to other version too).

So here's the details, I created a jQuery ajax post to our Laravel backend. You might ask, why not just VueJS or other JS framework? Well, basically just a simple ajax request so why bother.

For a developer like me who immediately get his feet wet without reading in development, I would keep on trying until it'll work.

My jQuery code goes below:

      var request = $.post("/api/requestkoto",{
                data: {
                    'fieldOne': fieldOne,
                    'fieldTwo': fieldTwo,
                }
            });

For web developers, browser's developer console is their friend. It was pain in the ass to see that your request is not working. From the console the POST is turning to GET. I started to wonder and feeling lost. Where and Why it's happening?

 Tried using jQuery or $.

I also I tried disabling Larave's security by commenting out \App\Http\Middleware\VerifyCsrfToken::class, at HTTP/Kernel wherein my post started to work. Of course, it wasn't the solution.

Well, it turns out it's Laravel's security feature which states and can be found here.

In addition to checking for the CSRF token as a POST parameter, the VerifyCsrfToken middleware will also check for the X-CSRF-TOKEN request header. You could, for example, store the token in a HTML meta tag:

<meta name="csrf-token" content="{{ csrf_token() }}">
And you have to add this your code too.

$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
});
So with that, I my new ajax POST request would be

      var request = $.post("/api/requestkoto",{
                data: {
                     '_token': window.Laravel.csrfToken, 'fieldOne': fieldOne,
                    'fieldTwo': fieldTwo,
                }
            });
I just added  '_token': window.Laravel.csrfToken to the request and everything works as expected. Spent more than 1 hour tracing from backend to javascript.

Okay. That's it for now.

Welcome to PH Devlog

Hi!

I'm a Web Developer from Philippines and I just created this blog to serve as my logs on web development.

This could be a log of simple or even complex issues that I encountered during the development process. Creation of this blog has been in my mind for quite a long time now but today I decided to start it.

So that's it for now.

Ciao!

- ian v