Laravel Settings Service Provider Database Migration - mysql

I created the settings table via a migration and implemented a SettingsServiceProvider (based on this post Laravel - Set global variable from settings table). Now it says that the base table can't be found. Of course not, because I need to run the migration first.
[Illuminate\Database\QueryException] SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.settings' doesn't exist (SQL : select value, name from settings)
Any ideas what to do? I course I could create the table manually but this would destruct the ideas of migrations...
Any help would be appreciated. I'm using Laravel 5.3.
Update 25/11/2016 - added code snippets
Service Provider
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
class SettingsServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* #return void
*/
public function boot()
{
config()->set('settings', \App\Setting::pluck('value', 'name')->all());
}
/**
* Register the application services.
*
* #return void
*/
public function register()
{
//
}
}
Migration
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSettingsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('value');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('settings');
}
}

Related

Can't change data type in database using Laravel migration

I'm trying to change the data type (from string to boolean) of 5 columns in my database, but it's showing an error that i have no idea what is the meaning.
My migration is this:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeFieldDataType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->boolean('categoria_pneu')->change();
$table->boolean('categoria_dicas')->change();
$table->boolean('categoria_servicos')->change();
$table->boolean('categoria_dicas_gerais')->change();
$table->boolean('categoria_variados')->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('categoria_pneu');
$table->dropColumn('categoria_dicas');
$table->dropColumn('categoria_servicos');
$table->dropColumn('categoria_dicas_gerais');
$table->dropColumn('categoria_variados');
});
}
}
The erros that's showing, is this:
I don't know what is the meaning of the error, since i'm trying to change the type to BOOLEAN and not TINYINT (i don't actually know if it's the same...)

im getting this error while creating fake factory model data in Laravel 8

\App\Models\Testt::factory()->create();
PHP Error: Class "Database\Factories\TesttFactory" not found in C:\xampp\htdocs\Project1\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Factories\Factory.php on line 683
im using cmd and using migrations to generate fake data
below is the mentioned code of factory, migrations
<?php
namespace Database\Factories;
use App\Models\Testt;
use Illuminate\Database\Eloquent\Factories\Factory;
**class TesttfactoryFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* #var string
*/
protected $model = \App\Models\Testt::class;
/**
* Define the model's default state.
*
* #return array
*/
public function definition()
{
return [
//
];
}
}**
and for migrations the code is
**<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Str;
class CreateTesttsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('\App\Models\testts', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->mediumText('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('testts');
}
}**

How to change database column 'null' to 'nullable' using laravel migration in mysql?

This is my vehicles table. I want to change my database structure by using a migration
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateVehiclesTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('vehicles', function (Blueprint $table) {
$table->increments('id');
$table->string('image')->nullable();
$table->string('reg_no')->unique();
$table->string('fuel_type');
$table->string('capacity');
$table->double('rate');
$table->boolean('req_carrier');
$table->date('service_date');
$table->integer('service_freq_km');
$table->integer('service_freq_months');
$table->date('insurance_date');
$table->integer('insurance_freq_months');
$table->date('eco_date');
$table->integer('eco_freq_months');
$table->date('licence_date');
$table->integer('licence_freq_months');
$table->integer('current_company');
$table->string('status')->default("available");
$table->timestampsTz();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('vehicles');
}
}
I want to give nullable values to these columns.
1.service_date
2.service_freq_km
3.service_freq_months
How can I alter these columns as nullable in mysql?
You can read the docs about Modifying Columns.
If you want these feature, you need to install this package first:
composer require doctrine/dbal
Then, you need to create another migration, for example:
2019_10_24_xxxxxx_change_columns_to_nullable_in_vehicles.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class ChangeColumnsToNullableInVehicles extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable()->change();
$table->integer('service_freq_km')->nullable()->change();
$table->integer('service_freq_months')->nullable()->change();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable(false)->change();
$table->integer('service_freq_km')->nullable(false)->change();
$table->integer('service_freq_months')->nullable(false)->change();
});
}
}
install the package in order to update the tables composer require doctrine/dbal
Since you have migrated the migration files, you now need to create a new migration file using artisan command:
php artisan make:migration change_nullable_field_columns_to_vehicles_tables --table=vehicles
In newly created migration file, add the following codes
public function up() {
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable()->change();
$table->integer('service_freq_km')->nullable()->change();
$table->integer('service_freq_months')->nullable()->change();
});
}
//For php artisan down
public function down(){
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable(false)->change();
$table->integer('service_freq_km')->nullable(false)->change();
$table->integer('service_freq_months')->nullable(false)->change();
});
}
Now you can execute migrate command
php artisan migrate
You need to create new migration file that is name "2019_10_24_00000_update_vehicle_tables"
if(Schema::hasTable('vehicles')) {
Schema::table('vehicles', function($table)
{
$table->date('service_date')->nullable();
$table->integer('service_freq_km')->nullable();
$table->integer('service_freq_months')->nullable();
});
}
create new migration class for alter table and use this up function
public function up()
{
Schema::table('vehicles', function (Blueprint $table) {
$table->date('service_date')->nullable();
$table->integer('service_freq_km')->nullable();
$table->integer('service_freq_months')->nullable();
});
}
Schema::table use for alter table and Schema::create is use for create new table
$table->string('first_name')->default('DEFAULT');
If the default value is supposed to be null, make it nullable instead.
$table->string('name')->nullable();
$table->string('name')->nullable()->default('NULL');
$table->string('name')->nullable()->default(NULL);
$table->string('name')->nullable()->default();

Using UUID as default value in a laravel migration

I have the following migration:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateRatingGameUserAnswersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->uuid('answer_token')->default(DB::raw('UUID()'));
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->dropColumn('answer_token');
});
}
}
As you can see I'm trying to set UUID as default value. I've seen it here
But when I run php artisan migrate I see the following:
What is wrong?
As I came across this question when having the same issue:
With MySQL 8 you can use functions as default values. But you need to put paranthesis around them. So this works:
$table->uuid('uid')->default(DB::raw('(UUID())'));
But this won't be compatible with MySQL 5.7!
You cannot use mySQL function as default value it should be either set or you use a trigger.
try please like this:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class UpdateRatingGameUserAnswersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$uuid = DB::raw('select UUID()');
$table->uuid('answer_token')->default($uuid);
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('rating_games_user_answers', function (Blueprint $table) {
$table->dropColumn('answer_token');
});
}
}

Laravel Migration - table not getting created

I'm trying to learn Laravel. I'm following the Quickstart documentation but have gotten into a problem with Migrations. I'm at this step: http://laravel.com/docs/quick#creating-a-migration
When I run the command php artisan migrate, the command line displays the following:
c:\wamp\www\laravel>php artisan migrate
Migration table created successfully.
Migrated: 2013_09_21_040037_create_users_table
In the database, I see a migrations table created with 1 record. However, I do not see a users table. So, I can't continue on to the ORM portion of the tutorial.
Any ideas what I might be doing wrong? Why isn't the users table getting created?
EDIT 1 (original migration file):
<?php
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function($table)
{
$table->increments('id');
$table->string('email')->unique();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
The updated Laravel should use Blueprint for database schema creation.
So try to change your user migration file content like this,
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->integer('id', true);
$table->string('name');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
Then run,
php artisan migrate:rollback
and then migrate again.
Read the API documentation here http://laravel.com/api/class-Illuminate.Database.Schema.Blueprint.html
I faced the same problem, after searching at Laravel documentation (link to documentation), i found an artisan command that resolve it :
php artisan migrate:refresh
The migrate:refresh command will roll back all of your migrations and then execute the migrate command. This command effectively re-creates your entire database