What is difference between use env(‘APP_ENV’), config(‘app.env’) or App::environment() to get app environment?

In Short & up-to-date 2022: use env() only in config files use App::environment() for checking the environment (APP_ENV in .env). use config(‘app.var’) for all other env variables, ex: config(‘app.debug’) create own config files for your own ENV variables. Example: In your .env: MY_VALUE=foo example config/myconfig.php return [ ‘myvalue’ => env(‘MY_VALUE’, ‘bar’), // ‘bar’ is default … Read more

Where can I set headers in laravel

In Laravel 5, using Middleware, creating a new file, modifying an existing file: New file: app/Http/Middleware/AddHeaders.php <?php namespace App\Http\Middleware; use Closure; use Illuminate\Contracts\Routing\Middleware; // If Laravel >= 5.2 then delete ‘use’ and ‘implements’ of deprecated Middleware interface. class AddHeaders implements Middleware { public function handle($request, Closure $next) { $response = $next($request); $response->header(‘header name’, ‘header value’); … Read more

Select Last Row in the Table

You’ll need to order by the same field you’re ordering by now, but descending. As an example, if you have a time stamp when the upload was done called upload_time, you’d do something like this; For Pre-Laravel 4 return DB::table(‘files’)->order_by(‘upload_time’, ‘desc’)->first(); For Laravel 4 and onwards return DB::table(‘files’)->orderBy(‘upload_time’, ‘desc’)->first(); For Laravel 5.7 and onwards return … Read more

How to fix error on Foreign key constraint incorrectly formed in migrating a table in Laravel

When creating a new table in Laravel. A migration will be generated like: $table->bigIncrements(‘id’); Instead of (in older Laravel versions): $table->increments(‘id’); When using bigIncrements the foreign key expects a bigInteger instead of an integer. So your code will look like this: public function up() { Schema::create(‘meals’, function (Blueprint $table) { $table->increments(‘id’); $table->unsignedBigInteger(‘user_id’); //changed this line … Read more

How to use paginate() with a having() clause when column does not exist in table

You can calculate the distance in the WHERE part: DB::table(‘posts’) ->whereRaw($haversineSQL . ‘<= ?’, [$distance]) ->paginate(10); If you need the distance value in your application, you’ll have to calculate it twice: DB::table(‘posts’) ->select(‘posts.*’, DB::raw($haversineSQL . ‘ as distance’)) ->whereRaw($haversineSQL . ‘<= ?’, [$distance]) ->paginate(10);