Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
development:php:laravel [2020/02/27 14:44]
kalenpw created
development:php:laravel [2021/06/29 15:15] (current)
kalenpw [Add column to existing table]
Line 10: Line 10:
 php artisan key:generate --ansi php artisan key:generate --ansi
 </code> </code>
 +
 +----
 +===== CORS =====
 +[[https://github.com/barryvdh/laravel-cors | Laravel-cors repo]]
 +<code bash>
 +composer require barryvdh/laravel-cors
 +</code>
 +
 +To allow CORS for all routes, add the HandleCors middleware in the ''$middleware'' property of ''app/Http/Kernel.php'' class:
 +<code php>
 +protected $middleware = [
 +    // ...
 +    \Barryvdh\Cors\HandleCors::class,
 +];
 +</code>
 +
 +To allow CORS on a specific middleware group or route, add the HandleCors middleware to your group:
 +<code php>
 +protected $middlewareGroups = [
 +    'web' => [
 +       // ...
 +    ],
 +
 +    'api' => [
 +        // ...
 +        \Barryvdh\Cors\HandleCors::class,
 +    ],
 +];
 +</code>
 +
 +Configure CORS
 +<code bash>
 +php artisan vendor:publish --provider="Barryvdh\Cors\ServiceProvider"
 +</code>
 +
 +----
 +===== Database =====
 +** Add column to existing table ** \\
 +<code bash>
 +php artisan make:migration add_paid_to_users
 +</code>
 +<code php>
 +public function up()
 +{
 +    Schema::table('users', function($table) {
 +        $table->integer('paid');
 +        // to set default value:
 +        $table->integer('paid')->default(0);
 +    });
 +}
 +</code>
 +and a rollback option
 +<code php>
 +public function down()
 +{
 +    Schema::table('users', function($table) {
 +        $table->dropColumn('paid');
 +    });
 +}
 +</code>
 +then run migration
 +<code bash>
 +php artisan migrate
 +</code>
 +
 +----
 +===== Order By =====
 +<code php>
 +$users = DB::table('users')
 +                ->orderBy('name', 'desc')
 +                ->get();
 +// or
 +
 +$projects = \App\Project::orderBy('order')->get();
 +</code>
 +
 +----