Eloquent Parent-Child relationship on same model

You should use with(‘children’) in the children relation and with(‘parent’) in the parent relations. For your code to be recursive: public function parent() { return $this->belongsTo(‘App\CourseModule’,’parent_id’)->where(‘parent_id’,0)->with(‘parent’); } public function children() { return $this->hasMany(‘App\CourseModule’,’parent_id’)->with(‘children’); } Note: Make sure your code has some or the other exit conditions otherwise it will end up in a never ending … Read more

disable csrf in laravel for specific route

Since version 5.1 Laravel’s VerifyCsrfToken middleware allows to specify routes, that are excluded from CSRF validation. In order to achieve that, you need to add the routes to $except array in your App\Http\Middleware\VerifyCsrfToken.php class: <?php namespace App\Http\Middleware; use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as BaseVerifier; class VerifyCsrfToken extends BaseVerifier { protected $except = [ ‘payment/*’, ]; } See the … Read more

How to execute raw queries with Laravel 5.1?

I found the solution in this topic and I code this: $cards = DB::select(“SELECT cards.id_card, cards.hash_card, cards.`table`, users.name, 0 as total, cards.card_status, cards.created_at as last_update FROM cards LEFT JOIN users ON users.id_user = cards.id_user WHERE hash_card NOT IN ( SELECT orders.hash_card FROM orders ) UNION SELECT cards.id_card, orders.hash_card, cards.`table`, users.name, sum(orders.quantity*orders.product_price) as total, cards.card_status, max(orders.created_at) … Read more

SQLSTATE[HY000] [2002] A connection attempt failed.. – When attempting to connect from Local to remote server

Hardly a surprise. The mysql socket is rarely if ever left open for connections from the public facing interface. usually mysql port (3306) can only be accessed from the private network interface. Even if the socket was open, there are so many things that that go wrong including firewalls getting in the way and simple … Read more

Custom user authentication base on the response of an API call

By following the steps below, you can setup your own authentication driver that handles fetching and validating the user credentials using your API call: 1. Create your own custom user provider in app/Auth/ApiUserProvider.php with the following contents: namespace App\Auth; use Illuminate\Contracts\Auth\UserProvider; use Illuminate\Contracts\Auth\Authenticatable as UserContract; class ApiUserProvider implements UserProvider { /** * Retrieve a user … Read more

Laravel: connect to databases dynamically

The simplest solution is to set your database config at runtime. Laravel might expect these settings to be loaded from the config/database.php file, but that doesn’t mean you can’t set or change them later on. The config loaded from config/database.php is stored as database in Laravel config. Meaning, the connections array inside config/database.php is stored … Read more