I'm writing a custom migration and I need to maintain those migrations in a separate migration repository table. I override DatabaseMigrationRepository and replace the migration repository function as follows
public function registerRepository()
{
$this->app->bindShared('migration.repository', function($app)
{
$table = $app['config']['database.cf_custom_migrations'];
return new CustomDatabaseMigrationRepository($app['db'], $table);
});
}
And I have registered my custom migration in artisan.php.
But when I call the custom migration command its execution is based on the Default Migration command.
Have anyone tried this before? How can run the Custom Migration command on Custom Migration table?
create a custom command instead of using migrate which is default to laravel.
use that custom command to run your custom migrations.
hope it helps
Related
I want use a variable from my database to set a folder for custom views.
"class AppServiceProvider extends ServiceProvider"
$ActiveProject = ThemeConfig::where('module_type',"project")->where('active',"1")->first()->file;
After this, I get an active project name (like Nshop), and I want to set it in:
public function register()
{
$this->app['view']->addNamespace('Projects', base_path() . '/Projects/'.$ActiveProject.'/Views');
}
But I get an error.
How can I accomplish this task?
Using ORM Models in the AppServiceProvider doesn't work. This file is part of the boot process of Laravel where your models are not loaded yet. But you can rely on functions that are part of the Laravel core concept.
$ActiveProject = ThemeConfig::where('module_type',"project")->where('active',"1")->first()->file;
Becomes
$ActiveProject = \DB::table('theme_configs')->where('module_type',"project")->where('active',"1")->first()->file;
Something really weird is going on with my Laravel setup. I create some migration files and, when running php artisan migrate after creating each of them, they were successfully run and the tables were created in the database. Now, if I want to run php artisan migrate:refresh --seed, it cannot rollback one of the migrations because it says that migration file doesn't exist.
This is the error:
And this is my migration file:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAssessmentsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('assessments', function (Blueprint $table) {
$table->increments('id');
//TODO - Complete information for this table
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('assessments');
}
}
What's even weirder is that I had to re-create all migrations because it was randomly not creating some of the tables listed on the migrations.
Did anyone face this issue before? Any help is really appreciated.
[UPDATE]
After a while, I realized this is not something from Laravel. For some reason, my Homestead is not viewing those files despite there are there. If I access that folder via SSH (the one inside the vagrant box), the file is not there. If I go to the real folder, it is there. For some reason the box is not synching files properly.
What's even weirder is that I can access and edit the file inside the VMB but it won't list it and won't take into account when running migrations. Here I created a screen-recording showing the problem.
[UPDATE 2]
Just recorded 2 more videos. This is really strange (unless I am missing something).
Video 1.
Video 2.
It seems to be a problem with MacOS High Sierra and Vagrant. In order to fix it, I had to apply this workaround.
Hopefully this will save some time to the ones having the same problem I was having.
Artisan generator seems over-sophisticated, It generates a class extended from Model class!!!
Is there any way to generate model validation rules in a lumen model automatically (based on column definition of a mysql table)?
What about column names?
I am the author of lumen-generators, A collection of generators for Lumen and Laravel 5.
This package contains a Model Generator which supports generating validation rules.
Installation
Add the generators package to your composer.json by running the command:
composer require wn/lumen-generators
Then add the service provider in the file app/Providers/AppServiceProvider.php like the following:
public function register()
{
if ($this->app->environment() == 'local') {
$this->app->register('Wn\Generators\CommandsServiceProvider');
}
}
Don't forget to include the application service provider on your bootstrap/app.php and to enable Eloquent and Facades if you are using Lumen
If you run the command php artisan list you will see the list of added commands:
wn:controller Generates RESTful controller using the RESTActions trait
wn:controller:rest-actions Generates REST actions trait to use into controllers
wn:migration Generates a migration to create a table with schema
wn:model Generates a model class for a RESTfull resource
wn:pivot-table Generates creation migration for a pivot table
wn:resource Generates a model, migration, controller and routes for RESTful resource
wn:resources Generates multiple resources from a file
wn:route Generates RESTful routes.
Generating a model with validation rules
Runing the following command:
php artisan wn:model TestingModel --rules="name=required age=integer|min:13 email=email|unique:users,email_address"
Will generate a model containing the following rules:
public static $rules = [
"name" => "required",
"age" => "integer|min:13",
"email" => "email|unique:users,email_address",
];
Please refer to the Full README for more details.
Hope this helps :)
There is no such command built into laravel or lumen.
I found a package (on a site called google) that provides such a command: https://github.com/jijoel/validation-rule-generator
It's locked to illuminate/support 4.0.x, so won't work with current versions of laravel. If you have lots of models it might be worth to fork, bump the version in composer.json and see if it works.
I want to access session data from database. Currently, I have changed default setting in session.php to database so that I can capture all the session columns in DB. But now I want to access these in my code. Do I need to create some session model or it's present out of the box just like user.php. Please help me.
You should create sessions table with these commands:
php artisan session:table
composer dump-autoload
php artisan migrate
Or you could manually create migration and run it, an example from documentation:
Schema::create('sessions', function ($table) {
$table->string('id')->unique();
$table->text('payload');
$table->integer('last_activity');
});
After doing that, you can just use sessions as usual with Session:: facade or sessions() helper:
session()->flash('message', 'Success!');
Update
If you need to access table data directly, you could do this:
DB::table('sessions')->get();
Or you could create SessionData() model and do this:
SessionData::all();
I made a PHP script in Laravel and now I want to show demo to my buyers. Since I don't want them to make any changes to the database, I was wondering if there is a way to globally disable any saving to the database?
If you have a BaseModel that extends Laravel's default Eloquent model class. And all of your applications models extend that BaseModel you can add the following to it:
public static function boot()
{
parent::boot();
static::saving(function($model)
{
return false;
});
}
This will intercept any creating or updating events from Eloquent.
This may be the technical answer for your question, but ideally just backup your db and restore it after showing to your buyers.
The easiest thing will be to create a mysql user that hasn't permissions to insert. Use the following mysql statement to create the user and use this user as the user in your database setting. I don't think there's a global method for this in laravel.
GRANT SELECT ON [database name].[table name] TO ‘[username]’#'%’;
This way, your user can view everything, but isn't able to save a bit.