I tried to change id field from Integer auto increment to string but after I run migrate, the structure of MYSQL's not change.
This is my code:
Schema::table('table', function ($table) {
$table->dropPrimary();
$table->string('id', 50)->change()->primary();
});
Please help me, thank you so much.
You should write this. Hopefully this will solve your problem
Schema::table('table', function ($table) {
$table->dropPrimary('id');
$table->string('id', 50)->change()->primary();
});
Also you should check if doctrine/dbal is installed successfully
Related
I must create new table in octobercms project and I followed documentation and added new migration file inside plugin update file I have create_currency_rates_table.php file and it has this codes
<?php namespace RainLab\User\Updates;
use Schema;
use October\Rain\Database\Updates\Migration;
class CreateCurrencyRateTable extends Migration
{
public function up()
{
Schema::create('currency_rates', function($table)
{
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('currency');
});
}
public function down()
{
Schema::drop('currency_rate');
}
}
when I used php artisan october:up it is not detecting new migration. How can I create new table?Can anyone help me?
You also need to update plugins\<author>\<plugin_name>\updates\version.yaml this file and add your file name there as well.
So, in your case, you added a file like create_currency_rates_table.php then you need to add details of your file in version.yaml
for ex:
1.0.1: First version of Demo
1.0.2:
- Description About what is this update about?
- create_currency_rates_table.php
now when you next time just login to backend this table will be created automatically.
if any doubt please comment.
I'm trying to add index to Algolia using Laravel Scout based on a condition. For example I have a Article model and I only want to add this article to Algolia if the article is active. My first approach was this:
public function toSearchableArray()
{
if($this->active) return $record;
return [];
}
this only adds the active records but still attempts to add empty arrays which is considered as Operation in algolia ( I will be charged for it). The second approach was to use shouldBesearchable() function from scout:
public function shouldBeSearchable()
{
if($this->active) return true;
return false;
}
This doesn't work with php artisan scout:import "App\Article". Has anyone faced a similar problem?
It was a bug in Laravel Scout, shouldBeSearchable is not release yet (on master branch) so you may experience some issue like this one.
Although, good news: it was just fixed by this PR.
https://github.com/laravel/scout/pull/250
I have two models with one-to-many connection. Let it be, for example, User ans UserComment models. When I'm calling User::destroy method, I also want to destroy each UserComment instance with user_id field equal to id of destroyed User. I may simply overload User::destroy method, but I wonder if there is better ways to achieve this.
Yes, the best way (or more efficient) is to do it through migrations. Let's have a little example with User and UserComment. Leave user migration as is. For UserComment do:
Schema::create('usercomments', function(Blueprint $table){
$table->integer('user_id')->unsigned();
....
$table->foreign('user_id', 'fk_user_id')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade'); //not needed in your case.
});
Ok, I haven't used this for a long time, so you may check the syntax online. But that's the gist of it.
When you delete a user, all his comments will be delete as well automatically, by your DB.
Goodluck mate!
I would use the database for this (as suggested above by EddyTheDove), but IF you don't want to use the database for this... you can add something to the deleting event of the User model.
EXAMPLE:
protected static function boot() {
parent::boot();
static::deleting(function($user) {
$user->user_comments()->delete();
});
}
I tried to rewrite a primary key in laravel like as:
public function setIdAttribute($value)
{
$this->attributes['id'] = $value; // or instead $value $this->attributes['idCity']
}
So I need to replace value of id field on value of idCity field
Rename a columns you should create a new migration
To create migration for this change write on console
php artisan make:migration swapKeyId
then go to database->migrations->...swapkey....php
in method called function up() write the code below but replace your table name in my case i use users
Schema::table('users', function ($table) {
$table->renameColumn('id', 'userId');
});
now its time to push migrates to database
php artisan migrate
and its done. this method is renamin it via laravel, also you can change it directly to the database(MySQL,MariaDB,SQL Server etc)
Setting an attribute in your model to define how php should store a value is something entirely different from creating a primary key in MySQL. Setup your migrations following the documentation and there isn't a lot that can go wrong:
Migrations in Laravel 5.2
This is mine
public function down()
{
Schema::table('customer_plans', function (Blueprint $table) {
$table->dropUnique('customer_plans_code_unique');
$table->string('code',255)->change();
$table->string('name',255)->change();
});
}
I do this as laravel.com but i got this error
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'customer_plans_code_unique'; check that column/
key exists
Anyone knows this error, please tell me!
Thanks for reading!
Try removing $table->dropUnique('customer_plans_code_unique'); from your code.
Without seeing the rest of the code I can't tell if having that line is important or not, but it's definitely the reason you're seeing the error.
Show me your up() function and I can give you more feedback.