neo4j migration in laravel doesnot work properly - laravel

I am using Neo4j in my application with /Vinelab/NeoEloquent. Now the problem is that when I run php artisan neo4j:migrate --database=neo4j
, node for the migration table is created.
Not any labels defined in function up() is not created. What should I do to solve this?
Schema class is,
<?php
use Vinelab\NeoEloquent\Schema\Blueprint;
use Vinelab\NeoEloquent\Migrations\Migration;
class CreateTestpostsTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Neo4jSchema::label('Testposts', function(Blueprint $label)
{
$label->unique('tpid');
$label->index('name');
$label->index('content');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Neo4jSchema::drop('Testposts');
}
}

According to your Schema code, no labels should be expected to be created. You are setting a constraint on the tpid to be unique, and indexing the name and content of the nodes with Testposts label.
To check if your migration has taken effect:
Try creating two nodes with the same tpid and you should get an error
Try profiling the queries to the name and content properties, this link should help with that: http://neo4j.com/docs/stable/cypherdoc-basic-query-tuning-example.html
On a side note, if the Label of the node in your model is Post or anything other than Testposts, then you should change Testposts to whatever is in your model for this to work properly.

Related

Password_resets table missing, even after doing php artisan migrate

I am trying to do password recovery in laravel, but after inserting an email to send the reset request, an error appears saying that password_resets doesn't exist.
I've already tried to migrate again but nothing works.
SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "password_resets" does not exist
LINE 1: delete from "password_resets" where "email" = $1 ^ (SQL: delete from "password_resets" where "email" = blabla#gmail.com)
According to this, it seems that the command to generate the migration for password_resets no longer exists, you can try to create a new migration with this:
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePasswordResetsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('password_resets', function (Blueprint $table) {
$table->string('email')->index();
$table->string('token')->index();
$table->timestamp('created_at');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('password_resets');
}
}
In my case, I fixed the issue of the missing table by adding these two lines in AppServiceProvider.php.
Follow these steps:-
Open AppServiceProvider.php (Location: app/Providers/AppServiceProvider.php).
Add this line outside from classes use Illuminate\Support\Facades\Schema;
Add this line inside function boot() Schema::defaultStringLength(191);
Delete all tables from database.
Run this command php artisan migrate
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
Schema::defaultStringLength(191);
}
}

Laravel, copy tables from one database to other

I'm working on a SaaS project. With different client requests for more fields or less etc, i believe it would be helpful instead of just keep growing on already created tables, divide the tables that could indeed grow into separate databases per client. To do so, it would involve recreating the tables to X connection which is already set, and creating a bunch of tables making migrations huge.
My question is if there is a method to copy table example_table from database A to database B keeping the data using the normal migrations, or if i would be better off using some DBMS to make the templates.
You can do this with a raw query
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use DB;
class MyNewTable extends Migration {
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
DB::statement('CREATE TABLE conection2.newtable LIKE system.oldtable; ');
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::connection('conection2')->drop('newtable');
}
}

Custom laravel migration command "[Illuminate\Database\Migrations\MigrationRepositoryInterface] is not instantiable"

I'm trying to create a custom laravel (5.2) migration command that basically works the same as migrate:status except it just lists the pending migrations instead of all the migrations.
To do this i've very simply copied the migrate:status into another class within my app/console directory and adjusted the code to suit my needs. However whenever I try to run it I get an error:
[Illuminate\Contracts\Container\BindingResolutionException]
Target [Illuminate\Database\Migrations\MigrationRepositoryInterface] is not instantiable while building [App\Console\Commands\PendingMigrations, Illuminate\Database\Migrations\Migrator].
The contents of the class itself and the fire() method doesn't seem to matter as it doesn't get that far, it fails within the __construct() method.
<?php namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Database\Migrations\Migrator;
class PendingMigrations extends Command
{
/**
* The console command name.
*
* #var string
*/
protected $name = 'migrate:pending';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Shows a list of pending migrations';
/**
* The migrator instance.
*
* #var \Illuminate\Database\Migrations\Migrator
*/
protected $migrator;
/**
* Create a new migration rollback command instance.
*
* #param \Illuminate\Database\Migrations\Migrator $migrator
* #return \Illuminate\Database\Console\Migrations\StatusCommand
*/
public function __construct(Migrator $migrator)
{
parent::__construct();
$this->migrator = $migrator;
}
/**
* Execute the console command.
*
* #return void
*/
public function fire()
{
}
}
The reason for it is likely to be something to do with the IoC container and the order with which things are loaded, but I don't know enough about the inner workings of Laravel to figure out any more than that.
It surely must be possible?
I am currently stuck on 5.2, so i'm not sure if this problem exists in more recent versions.
The only thing i've attempted so far is added the migration service provider to the top of the list in config/app.php however it didn't seem to have an affect and it was just a random guess anyway.
providers' => [
Illuminate\Database\MigrationServiceProvider::class,`
]
I got around this using:
$this->migrator = app('migrator');
but it is not necessarily the best way to do this
The Migrator instance is not bound to the class name in the IoC container, it is bound to the migrator alias.
From Illuminate\Database\MigrationServiceProvider:
/**
* Register the migrator service.
*
* #return void
*/
protected function registerMigrator()
{
// The migrator is responsible for actually running and rollback the migration
// files in the application. We'll pass in our database connection resolver
// so the migrator can resolve any of these connections when it needs to.
$this->app->singleton('migrator', function ($app) {
$repository = $app['migration.repository'];
return new Migrator($repository, $app['db'], $app['files']);
});
}
Since the class name is not bound in the IoC container, when Laravel resolves your command and attempts to resolve the Migrator dependency, it attempts to build a new one from scratch and fails because the Illuminate\Database\Migrations\MigrationRepositoryInterface is also not bound in the IoC container (hence the error you're receiving).
Since Laravel can't figure this out itself, you need to either register the binding for the Migrator class name, or you need to register the binding for your command. Laravel itself registers all the bindings for the commands in the Illuminate\Foundation\Providers\ArtisanServiceProvider. An example of the command.migrate binding:
/**
* Register the command.
*
* #return void
*/
protected function registerMigrateCommand()
{
$this->app->singleton('command.migrate', function ($app) {
return new MigrateCommand($app['migrator']);
});
}
So, in your AppServiceProvider, or another service provider you setup, you can add one of the following:
Register the command in the IoC:
$this->app->singleton(\App\Console\Commands\PendingMigrations::class, function ($app) {
return new \App\Console\Commands\PendingMigrations($app['migrator']);
});
Or, register the Migrator class name in the IoC:
$this->app->singleton(\Illuminate\Database\Migrations\Migrator::class, function ($app) {
return $app['migrator'];
});
As I don't want to register the migrator everywhere in the app, but I still want to extend the MigrateCommand itself, I came up with this approach to maintain my app as it is:
public function __construct()
{
app()->singleton(\App\Console\Commands\PendingMigrations::class, function ($app) {
return new \App\Console\Commands\PendingMigrations($app['migrator']);
});
parent::__construct(app('migrator'));
}

Laravel Many-to-Many Pivot Table

I have a fairly simple question about the use of Pivot tables in laravel. Firstly ill give some information about my situation, I have two tables name "Vehicles", and "Events". Now I would like to create a table that will be used to hold vehicles that have registered for an event. Now the relationship that between these two tables would be that "Many Vehicles can register for Many Events" and vice versa. would a Pivot table be the best way to accomplish this, and if so could more singular values be in the same table?
You can associate an event with multiple vehicles and a vehicle to multiple events by doing something like this with your models (not tested):
Vehicle.php
<?php
namespace App;
use App\Event;
use Illuminate\Database\Eloquent\Model;
class Vehicle extends Model
{
...
/**
* Get the events that this vehicle belongs to.
*
* #return \App\Event
*/
public function events()
{
return $this->belongsToMany(Event::class, 'vehicle_event');
}
}
Event.php
<?php
namespace App;
use App\Vehicle;
use Illuminate\Database\Eloquent\Model;
class Event extends Model
{
...
/**
* Get the vehicles that this event has.
*
* #return \App\Vehicle
*/
public function events()
{
return $this->hasMany(Vehicle::class, 'vehicle_event');
}
}
You'll also need a migration file for the pivot table:
...
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('vehicle_event', function(Blueprint $table)
{
$table->integer('vehicle_id')->unsigned()->index();
$table->foreign('vehicle_id')->references('id')->on('vehicles');
$table->integer('event_id')->unsigned()->index();
$table->foreign('event_id')->references('id')->on('events');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('vehicle_event');
}
...
Then you can to use attach() and detach() to associate vehicles with events or vice versa.

Laravel 5.0 - Rename multiple columns in 1 migration

We recently made a switch of service provider such that multiple columns in a DB table for this project will need to be renamed.
I am aware of this post which shows how to rename 1 column from 1 table:
php artisan migrate:make rename_stk_column --table="YOUR TABLE" --create
Is there a way to execute this same migration with multiple columns ? (1 migration, not more than 1...trying to minimize number of migration files created)
You can just add multiple renameColumn(); statements for each column that needs to be updated in that given table. Just need to come up with a whatever name you guys/gals use for your migration files.
Just a sample of what I ran
class MultipleColumnUpdate extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('users', function ($table) {
$table->renameColumn('name', 'user_name');
$table->renameColumn('email', 'work_email');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('users', function ($table) {
$table->renameColumn('user_name', 'name');
$table->renameColumn('work_email', 'email');
});
}
}

Resources