
Define variable and use in Laravel controller method
Define variable and use in Laravel controller method
Hello everyone, in this post we will examine how to solve the "Define variable and use in Laravel controller method" programming puzzle.
How can i define a variable in my laravel controller and use in controller's method. In my case i have a api key in my env file and i want to get this api key value in controller method.-
Define variable and use in controller's method
Create a private variable and get the value of api key from .env file using
$_ENV['APP_API_KEY']
and pass the value to variable.app\Http\Controllers<SomeController>.php
class SomeController extends Controller{ private $apiKey; public function __construct() { $this->apiKey = $_ENV['APP_API_KEY']; } public function index() { $apikeyval = $this->apiKey; }
.env
APP_API_KEY = "5d1c88f1-bd63-428c-acce-eae8d1713445"
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- OrderBy on Eloquent relationships method in Laravel
- Method Illuminate\Http\Request::validated does not exist
- Create record with unique slug in laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- Display data in table using foreach in Laravel
- Target class [admin] does not exist.
- Fatal error: Uncaught Error: Class "Illuminate\Foundation\Application" not found
- How to check duplicate entry in laravel
- How to get only time from created_at in laravel
- Remove several global scope from query
- Send post data from controller to view
- How to show data by ID in laravel?
- Laravel hasmany select not working
- Argument 1 passed to App\Http\Controllers\Auth\LoginController::authenticated() must be an instance of App\Http\Controllers\Auth\Request
- Redirect to another view from controller in laravel
- Symlink(): No such file or directory
- Create model with migration and seeder
- Display first n record from collection in laravel view
- Get posts belongs to a specific user in Laravel
- After image selected get validation error in laravel
- Session Doesn't Work on Redirect
- Update last created record in Laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Undefined property: stdClass::$title
- Automatically remove records using Prunable trait in Laravel