laravel migration best way to add foreign key

Firstly you have to make your user_id field an index: $table->index(‘user_id’); After that you can create a foreign key with an action on cascade: $table->foreign(‘user_id’)->references(‘id’)->on(‘users’)->onDelete(‘cascade’); If you want to do that with a new migration, you have to remove the index and foreign key firstly and do everything from scratch. On down() function you have … Read more

Clone an Eloquent object including all relationships?

tested in laravel 4.2 for belongsToMany relationships if you’re in the model: //copy attributes $new = $this->replicate(); //save model before you recreate relations (so it has an id) $new->push(); //reset relations on EXISTING MODEL (this way you can control which ones will be loaded $this->relations = []; //load relations on EXISTING MODEL $this->load(‘relation1′,’relation2’); //re-sync everything … Read more

Modify existing Authorization module (email to username)

Laravel search the variable $username in the file : Illuminate\Foundation\Auth\AuthenticatesUsers public function loginUsername() { return property_exists($this, ‘username’) ? $this->username : ’email’; } As you can see, by default it will be named as ’email’. However you can override it in your AuthController by adding : protected $username=”username”;

Laravel 4 : Route to localhost/controller/action

If you are looking for a more automated routing, this would be the Laravel 4 way: Route: Route::controller(‘users’, ‘UsersController’); Controller (in this case UsersController.php): public function getIndex() { // routed from GET request to /users } public function getProfile() { // routed from GET request to /users/profile } public function postProfile() { // routed from … Read more

Error Field doesn’t have a default value in laravel 5.3

You should add ->nullable() or ->default(‘somethingHere’) to fields which you send empty values. $table->string(‘family’)->nullable(); //this means that if you send empty value this field will become MySQL NULL Or set default value: $table->string(‘family’)->default(‘default value here’); Than remigrate: php artisan migrate:rollback and php artisan migrate