I have the following code in my AppServiceProvider.php file:
<?php
namespace App\Providers;
use App\Models\Setting;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
if (Schema::hasTable('settings')) {
foreach (Setting::all() as $setting) {
Config::set('settings.'.$setting->key, $setting->value);
}
}
}
}
Which does it's job fine locally, but when I deploy via DeployHQ, it kills the process with the following error:
SQLSTATE[HY000] [2002] No such file or directory (SQL: select * from
information_schema.tables where table_schema = giga and table_name =
settings and table_type = 'BASE TABLE')
Which kinda makes sense, the database doesn't exist on the build server, so the check cannot run as there's nothing to check. Is there a different way to hydrate a settings config with values from a database on boot which doesn't affect the running of php artisan package:discover?
I know it'll probably be asked, but the .env file etc is all set up correctly. This issue is to do with the fact the build server doesn't have the database, but the server the files get piped to does.
Edit:
To give some more context, and perhaps some advice could be given on this, I'm only really using this config value in this code inside a Service class:
public function __construct()
{
$this->domain = config('api.domain');
$this->apiVersion = config('api.version');
$this->bearerToken = config('settings.bearer_token');
$this->clientId = config('api.client_id');
$this->clientSecret = config('api.client_secret');
}
Everything online suggests putting these values into the config, however if it's only being called here would it be okay to retrieve it from the Database directly?
I have two environment files: .env and .env.dusk.testing (Because .env gets overwritten constantly if I'm not explicit with the environment handler).
I've got two development servers:
One in port 8080 that uses .env (using command php artisan serve --port=8080)
One in port 8000 that uses .env.dusk.testing (using command php artisan serve --env=dusk.testing)
I'm trying a very basic test. Logging in an user. It's not working because it's not using the correct database.
.env
APP_NAME=DEV
APP_ENV=local
APP_DEBUG=true
DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5433
DB_DATABASE=db
DB_USERNAME=dbuser
DB_PASSWORD=****
.env.dusk.testing
APP_NAME=DUSK
APP_ENV=testing
APP_DEBUG=true
DB_CONNECTION=sqlite
phpunit.dusk.xml
...
<php>
<env name="APP_ENV" value="testing"/>
<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>
</php>
...
I've also defined the sqlite connection in config/database.php as follows:
'sqlite' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
My test uses a setup method to wipe up an user and persist it in the database.
namespace Tests\Browser;
use App\User;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Laravel\Dusk\Browser;
use Tests\DuskTestCase;
class ExampleTest extends DuskTestCase
{
use DatabaseMigrations;
protected $user;
public function setUp(): void
{
parent::setUp();
$this->user = factory(User::class)->create([
'nombre' => 'John Doe',
'email' => 'john.doe#testing.com',
'password' => bcrypt('password')
]);
}
/** #test */
public function guest_user_can_authenticate_with_valid_credentials()
{
$this->browse(function (Browser $browser) {
$browser->assertGuest()
->visit('/login')
->type('#email', $this->user->email)
->type('#password', 'password')
->click('#login-button')
->assertAuthenticatedAs($this->user)
->assertUrlIs('/dashboard')
->logout();
});
}
}
Using the DatabaseMigrations trait wipes out my PostgreSQL database instead of using the SQLite one. I tried to use RefreshDatabase instead but it's the same result. My environment database gets wiped out.
And the test fails anyways in both cases. The user is persisted into the SQLite testing database but it's still looking in the other one in the tests and destroying it in the process.
I'm this close to just drop this package and browser tests altogether. There is nothing simple about it. The documentation is shallow and it just doesn't work as expected.
My issue was fixed by
Not using an in-memory SQLite database (I used a .sqlite file instead).
Using the DatabaseMigrations trait instead of RefreshDatabase.
Being explicit about the environment file when running dusk. php artisan dusk --env=dusk.testing.
I setup a test database:
phpunit.xml:
<phpunit>
<!... other stuff ...>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="sqlite_testing"/>
</php>
</phpunit>
database.php:
'connections' => [
'sqlite_testing' => [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
],
DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
$this->call([
MyTableSeeder::class
]);
}
}
MyTableSeeder.php:
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use App\My\Model;
class MyTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
DB::table('model')->insert([
'id' => 1,
'created_at' => \Carbon\Carbon::now(),
'updated_at' => \Carbon\Carbon::now()
]);
/**
* create random data
*/
factory(Model::class, 50)->create();
}
}
ModelFactory.php:
<?php
use Faker\Generator as Faker;
$factory->define(\App\My\Model::class, function (Faker $faker) {
return [
'id' => $faker->unique()->randomNumber(),
'created_at' => $faker->dateTime('now'),
'updated_at' => $faker->dateTime('now')
];
});
MyTest.php:
<?php
namespace Tests\Unit;
use App\My\Model;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class MyTest extends TestCase
{
use RefreshDatabase;
public function testGettingModel() {
var_dump(Model::all()); // <-- returns a collection without any items
$model = Model::where('id', 1)->first();
$this->assertEquals('1', $model->id); // <-- trying to get property of non-object
}
}
So at the test run it seems the database is migrated by the trait but not seeded and thus nothing is returned.
The documentation does not state however (or I couldn't find it) how to seed the database upon testing.
It states how to manually seed by running "php artisan db:seed" but that's not working in a test obviously as the database doesn't exist anymore after the test. And I can't run it manually as the database doesn't exist before the test. Also that would make testing impractical.
Some examples state running the seeding before testing in the setup method of the test like so:
public function setUp() {
Artisan::call('db:seed');
}
But including this statement leads to the error:
RuntimeException : A facade root has not been set. (in /vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:218)
Alternatively running it like so:
public function setUp()
{
$this->artisan('db:seed');
}
Leads to:
Error : Call to a member function call() on null (in /vendor/laravel/framework/src/Illuminate/Foundation/Testing/Concerns/InteractsWithConsole.php:18)
How do I actually do this? Is there any fully working example anywhere? So far I couldn't find any :(
You have to call $this->seed(); in the setUp method. But you need to ensure to call the parents setUp method before or it will fail.
<?php
namespace Tests\Unit;
use App\My\Model;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
class MyTest extends TestCase
{
use RefreshDatabase;
public function setUp()
{
parent::setUp();
$this->seed();
}
public function testGettingModel() {
$model = Model::where('id', 1)->first();
$this->assertEquals('1', $model->id); // will assert to true if the id actually exists
}
}
In Laravel I have a table settings and i have fetched complete data from the table in the BaseController, as following
public function __construct()
{
// Fetch the Site Settings object
$site_settings = Setting::all();
View::share('site_settings', $site_settings);
}
Now i want to access $site_settings. in all other controllers and views so that i don't need to write the same code again and again, so anybody please tell me the solution or any other way so i can fetch the data from the table once and use it in all controllers and view.
Okay, I'm going to completely ignore the ridiculous amount of over engineering and assumptions that the other answers are rife with, and go with the simple option.
If you're okay for there to be a single database call during each request, then the method is simple, alarmingly so:
class BaseController extends \Controller
{
protected $site_settings;
public function __construct()
{
// Fetch the Site Settings object
$this->site_settings = Setting::all();
View::share('site_settings', $this->site_settings);
}
}
Now providing that all of your controllers extend this BaseController, they can just do $this->site_settings.
If you wish to limit the amount of queries across multiple requests, you could use a caching solution as previously provided, but based on your question, the simple answer is a class property.
At first, a config file is appropriate for this kind of things but you may also use another approach, which is as given below (Laravel - 4):
// You can keep this in your filters.php file
App::before(function($request) {
App::singleton('site_settings', function(){
return Setting::all();
});
// If you use this line of code then it'll be available in any view
// as $site_settings but you may also use app('site_settings') as well
View::share('site_settings', app('site_settings'));
});
To get the same data in any controller you may use:
$site_settings = app('site_settings');
There are many ways, just use one or another, which one you prefer but I'm using the Container.
Use the Config class:
Config::set('site_settings', $site_settings);
Config::get('site_settings');
http://laravel.com/docs/4.2/configuration
Configuration values that are set at run-time are only set for the current request, and will not be carried over to subsequent requests.
In Laravel, 5+ you can create a file in the config folder and create variables in that and use that across the app.
For instance, I want to store some information based on the site.
I create a file called site_vars.php,
which looks like this
<?php
return [
'supportEmail' => 'email#gmail.com',
'adminEmail' => 'admin#sitename.com'
];
Now in the routes, controller, views you can access it using
Config::get('site_vars.supportEmail')
In the views if I this
{{ Config::get('site_vars.supportEmail') }}
It will give email#gmail.com
Hope this helps.
EDiT-
You can also define vars in .env file and use them here.
That is the best way in my opinion as it gives you the flexibility to use values that you want on your local machine.
So, you can do something this in the array
'supportEmail' => env('SUPPORT_EMAIL', 'defaultmail#gmail.com')
Important - After you do this, don't forget to do this on production env
php artisan config:cache
In case, there's still some problem, then you can do this (usually it would never happen but still if it ever happens)
php artisan cache:clear
php artisan config:cache
In your local env, always do this after this adding it
php artisan config:clear
It's always a good practice not to cache config vars in local. in case, it was cached, this would remove the cache and would load the new changes.
I see, that this is still needed for 5.4+ and I just had the same problem, but none of the answers were clean enough, so I tried to accomplish the availability with ServiceProviders. Here is what i did:
Created the Provider SettingsServiceProvider
php artisan make:provider SettingsServiceProvider
Created the Model i needed (GlobalSettings)
php artisan make:model GlobalSettings
Edited the generated register method in \App\Providers\SettingsServiceProvider. As you can see, I retrieve my settings using the eloquent model for it with Setting::all().
public function register()
{
$this->app->singleton('App\GlobalSettings', function ($app) {
return new GlobalSettings(Setting::all());
});
}
Defined some useful parameters and methods (including the constructor with the needed Collection parameter) in GlobalSettings
class GlobalSettings extends Model
{
protected $settings;
protected $keyValuePair;
public function __construct(Collection $settings)
{
$this->settings = $settings;
foreach ($settings as $setting){
$this->keyValuePair[$setting->key] = $setting->value;
}
}
public function has(string $key){ /* check key exists */ }
public function contains(string $key){ /* check value exists */ }
public function get(string $key){ /* get by key */ }
}
At last I registered the provider in config/app.php
'providers' => [
// [...]
App\Providers\SettingsServiceProvider::class
]
After clearing the config cache with php artisan config:cache you can use your singleton as follows.
$foo = app(App\GlobalSettings::class);
echo $foo->has("company") ? $foo->get("company") : "Stack Exchange Inc.";
You can read more about service containers and service providers in Laravel Docs > Service Container and Laravel Docs > Service Providers.
This is my first answer and I had not much time to write it down, so the formatting ist a bit spacey, but I hope you get everything.
I forgot to include the boot method of SettingsServiceProvider, to make the settings variable global available in views, so here you go:
public function boot(GlobalSettings $settinsInstance)
{
View::share('globalsettings', $settinsInstance);
}
Before the boot methods are called all providers have been registered, so we can just use our GlobalSettings instance as parameter, so it can be injected by Laravel.
In blade template:
{{ $globalsettings->get("company") }}
View::share('site_settings', $site_settings);
Add to
app->Providers->AppServiceProvider file boot method
it's global variable.
Most popular answers here with BaseController didn't worked for me on Laravel 5.4, but they have worked on 5.3. No idea why.
I have found a way which works on Laravel 5.4 and gives variables even for views which are skipping controllers. And, of course, you can get variables from the database.
add in your app/Providers/AppServiceProvider.php
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
// Using view composer to set following variables globally
view()->composer('*',function($view) {
$view->with('user', Auth::user());
$view->with('social', Social::all());
// if you need to access in controller and views:
Config::set('something', $something);
});
}
}
credit: http://laraveldaily.com/global-variables-in-base-controller/
In Laravel 5+, to set a variable just once and access it 'globally', I find it easiest to just add it as an attribute to the Request:
$request->attributes->add(['myVar' => $myVar]);
Then you can access it from any of your controllers using:
$myVar = $request->get('myVar');
and from any of your blades using:
{{ Request::get('myVar') }}
In Laravel 5.1 I needed a global variable populated with model data accessible in all views.
I followed a similar approach to ollieread's answer and was able to use my variable ($notifications) in any view.
My controller location: /app/Http/Controllers/Controller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use App\Models\Main as MainModel;
use View;
abstract class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
public function __construct() {
$oMainM = new MainModel;
$notifications = $oMainM->get_notifications();
View::share('notifications', $notifications);
}
}
My model location: /app/Models/Main.php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use DB;
class Main extends Model
{
public function get_notifications() {...
I have found a better way which works on Laravel 5.5 and makes variables accessible by views. And you can retrieve data from the database, do your logic by importing your Model just as you would in your controller.
The "*" means you are referencing all views, if you research more you can choose views to affect.
add in your app/Providers/AppServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Contracts\View\View;
use Illuminate\Support\ServiceProvider;
use App\Setting;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
// Fetch the Site Settings object
view()->composer('*', function(View $view) {
$site_settings = Setting::all();
$view->with('site_settings', $site_settings);
});
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
}
}
If you are worried about repeated database access, make sure that you have some kind of caching built into your method so that database calls are only made once per page request.
Something like (simplified example):
class Settings {
static protected $all;
static public function cachedAll() {
if (empty(self::$all)) {
self::$all = self::all();
}
return self::$all;
}
}
Then you would access Settings::cachedAll() instead of all() and this would only make one database call per page request. Subsequent calls will use the already-retrieved contents cached in the class variable.
The above example is super simple, and uses an in-memory cache so it only lasts for the single request. If you wanted to, you could use Laravel's caching (using Redis or Memcached) to persist your settings across multiple requests. You can read more about the very simple caching options here:
http://laravel.com/docs/cache
For example you could add a method to your Settings model that looks like:
static public function getSettings() {
$settings = Cache::remember('settings', 60, function() {
return Settings::all();
});
return $settings;
}
This would only make a database call every 60 minutes otherwise it would return the cached value whenever you call Settings::getSettings().
You can also use Laravel helper which I'm using.
Just create Helpers folder under App folder
then add the following code:
namespace App\Helpers;
Use SettingModel;
class SiteHelper
{
public static function settings()
{
if(null !== session('settings')){
$settings = session('settings');
}else{
$settings = SettingModel::all();
session(['settings' => $settings]);
}
return $settings;
}
}
then add it on you config > app.php under alliases
'aliases' => [
....
'Site' => App\Helpers\SiteHelper::class,
]
1. To Use in Controller
use Site;
class SettingsController extends Controller
{
public function index()
{
$settings = Site::settings();
return $settings;
}
}
2. To Use in View:
Site::settings()
A global variable for using in controllers; you can set in AppServiceProvider like this :
public function boot()
{
$company=DB::table('company')->where('id',1)->first();
config(['yourconfig.company' => $company]);
}
usage
config('yourconfig.company');
using middlwares
1- create middlware with any name
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Support\Facades\View;
class GlobalData
{
public function handle($request, Closure $next)
{
// edit this section and share what do you want
$site_settings = Setting::all();
View::share('site_settings', $site_settings);
return $next($request);
}
}
2- register your middleware in Kernal.php
protected $routeMiddleware = [
.
...
'globaldata' => GlobalData::class,
]
3-now group your routes with globaldata middleware
Route::group(['middleware' => ['globaldata']], function () {
// add routes that need to site_settings
}
In file - \vendor\autoload.php, define your gobals variable as follows, should be in the topmost line.
$global_variable = "Some value";//the global variable
Access that global variable anywhere as :-
$GLOBALS['global_variable'];
Enjoy :)
I know I am super late to the party, but this was the easiest way I found.
In app/Providers/AppServiceProvider.php, add your variables in the boot method. Here I am retrieving all countries from the DB:
public function boot()
{
// Global variables
view()->composer('*',function($view) {
$view->with('countries', Country::all());
});
}
There are two options:
Create a php class file inside app/libraries/YourClassFile.php
a. Any function you create in it would be easily accessible in all the views and controllers.
b. If it is a static function you can easily access it by the class name.
c. Make sure you inclued "app/libraries" in autoload classmap in composer file.
In app/config/app.php create a variable and you can reference the same using
Config::get('variable_name');
Hope this helps.
Edit 1:
Example for my 1st point:
// app/libraries/DefaultFunctions.php
class DefaultFunctions{
public static function getSomeValue(){
// Fetch the Site Settings object
$site_settings = Setting::all();
return $site_settings;
}
}
//composer.json
"autoload": {
"classmap": [
..
..
..
"app/libraries" // add the libraries to access globaly.
]
}
//YourController.php
$default_functions = new DefaultFunctions();
$default_functions->getSomeValue();
Hey I am just starting to get moving with laravel and I can't seem to make the seeder work. The code that follows fails silently:
DatabaseSeeder.php
<?php
class DatabaseSeeder extends Seeder {
public function run()
{
Eloquent::unguard();
$this->call('SettingsTableSeeder');
// $this->call('GenericTableSeeder');
// $this->call('UserTableSeeder');
}
}
SettingsTableSeeder.php
<?php
class SettingsTableSeeder extends Seeder {
public function run()
{
$settings = Settings::create(array(
'adPriceStandard' => 9
));
}
}
Settings model (Settings.php) :
<?php
class Settings extends Eloquent {
protected $table = 'settings';
}
the model is there and the DB is in place, and I can access the settings table. I run the seeder and nothing happens. I AM STUMPED. Thanks!
Two things to keep in mind for anyone who finds this post and encounters a similar issue.
First, make sure you run 'composer dump-autoload'.
Second make sure that you either:
A) have $table->timestamps(); inside of your model's database migration, or
B) Set public $timestamps = false; in your Model class.
If you leave out $table->timestamps(); and forget to set public $timestamps to false, db:seed will fail silently.
If you're still having an issue after the above, create a new route at /test and include your Model::create([..]) code inside of it. This should throw a visible QueryException.
Can you post your Settings model? Are you extending Eloquent? It is possible that Eloquent is looking for the table settingss based on your class name. You can set your table's name in your Settings model like this :
protected $table = 'settings';
This is how I write my seeders - its a bit different to yours - see if this works;
<?php
class SettingsTableSeeder extends Seeder {
public function run()
{
$settings = array('adPriceStandard' => 9);
DB::table('settings')->insert($settings);
}
}
I assume you're using early laravel 5.1 build. You're missing Eloquent::reguard(). Also when using ::create([]) make sure all fields that added using this function is fillable. Please refer to the docs more regarding migrations.
ps. this is random guess, could you include namespace for Settings class in Settings::create(array(. although i'm sure such problem throws class not found exception.