How to set indexes when adding columns to a table in Laravel? - laravel

The column I added appears after created_at and other columns.
public function addColumnTable($table_name,$field_name,$field_type){
Schema::table($table_name, function (Blueprint $table) use($field_name,$field_type) {
$table->{$field_type}($field_name);
});
}

I had to use it:
public function addColumnTable($table_name,$field_name,$field_type){
Schema::table($table_name, function (Blueprint $table) use($field_name,$field_type) {
$table->{$field_type}($field_name)->after('id');
});
}

Related

how to update mysql database column charset or collation with laravel?

This is my migration code. This is not working not changing column collation.
public function up()
{
Schema::table('product', function (Blueprint $table) {
$table->string('hash_id')->collate('utf8mb4_bin')->change();
});
}
public function up()
{
Schema::table('product', function (Blueprint $table) {
$table->string('hash_id')->collation('utf8mb4_bin')->change();
});
}

Laravel - How to add foreign key reference to already existing migration

In my Larave-8, I created a table using this migration:
public function up()
{
Schema::create('profiles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('user_id');
$table->string('first_name',50);
$table->string('last_name',50);
$table->string('other_name',50)->nullable();
$table->string('gender',20);
$table->string('user_photo',350)->nullable();
$table->integer('nationality_id');
$table->string('marital_status',50);
$table->date('dob')->nullable();
$table->string('address', 300)->nullable();
$table->integer('country_id')->nullable();
$table->integer('state_id')->nullable();
$table->integer('city_id')->nullable();
$table->string('cv_file',350)->nullable();
$table->text('summary')->nullable();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
How do I alter the table through migration and add these:
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
Thanks
create another migration like to alter existing table
public function up()
{
Schema::table('profiles', function (Blueprint $table) {
$table->foreign('nationality_id')->references('id')->on('countries');
$table->foreign('country_id')->references('id')->on('countries');
$table->foreign('state_id')->references('id')->on('state_origins');
$table->foreign('city_id')->references('id')->on('cities');
});
}
public function down() {
Schema::table('profiles', function (Blueprint $table) {
$table->dropForeign('nationality_id');
$table->dropForeign('country_id');
$table->dropForeign('state_id');
$table->dropForeign('city_id');
});
}

BadMethodCallException: Trying to list all partners with the categories laravel

I'm experiencing the following:
Goal: I would like to list all partners and the categories they belong to. Basically, it's a many to many relationship.
Below is the code:
Partner Categories Table Migration
public function up()
{
Schema::create('partcats', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('partcatnameP')->unique();
$table->unsignedBigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
Partners Table Migration
public function up()
{
Schema::create('partners', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('partnername');
$table->string('regnumber')->unique();
$table->unsignedBigInteger('activestatus_id')->unsigned();
$table->foreign('activestatus_id')->references('id')->on('activestatuses');
$table->unsignedBigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
Partner_Category Migration
public function up()
{
Schema::create('partner_partcat', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('partner_id')->unsigned();
$table->foreign('partner_id')->references('id')->on('partners')->onDelete('cascade');
$table->unsignedBigInteger('partcat_id')->unsigned();
$table->foreign('partcat_id')->references('id')->on('partcats')->onDelete('cascade');
$table->timestamps();
});
}
Models are as shown below:
Partcat Model
public function partners()
{
return $this->belongsToMany('App\Partcat','partner_partcat');
}
Partner Model
public function partcats()
{
return $this->belongsToMany('App\Partcat','partner_partcat');
}
and the Partners Controller is as shown below:
public function index()
{
//
$partners = Partner::all()->partcats();
// dd(Partner::all()->partcats());
return view('partners.index',['partners'=>$partners]);
}
This is where I'm trying to retrieve the list of partners and its related categories. However, I get a BadMethod call error.
You can use the following method
$partners = Partner::with('partcats')->get();
https://laravel.com/docs/5.6/eloquent-relationships#eager-loading
A many to many pivot table typically has the id for both tables it is relating.
The Partner_Category migration you have posted only seems to contain a partner_id. You may need to add in the category_id.
public function up()
{
Schema::create('partner_partcat', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('partner_id')->unsigned();
$table->unsignedBigInteger('partcat_id')->unsigned();
$table->timestamps();
$table->foreign('partner_id')->references('id')->on('partners')->onDelete('cascade');
$table->foreign('partcat_id')->references('id')->on('partcats')->onDelete('cascade');
});
}

Laravel: migrations change default value(boolean) of column

This is my current code.
public function up()
{
Schema::table('render', function (Blueprint $table) {
$table->boolean('displayed')->default(1);
});
}`
How to change the default value to 0 as below?
public function up()
{
Schema::table('render', function (Blueprint $table) {
$table->boolean('displayed')->default(0);
});
}
public function up()
{
Schema::table('render', function (Blueprint $table) {
$table->boolean('displayed')->default(0)->change();
});
}
Added ->change(). Please see Link
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->boolean('is_active')->unsigned()->nullable();
$table->timestamps();
});
is_active column is defined as a nullable boolean column using the boolean data type and the unsigned modifier. This will allow you to use the true and false keywords in your database queries instead of 0 and 1.

schema builder laravel migrations unique on two columns

How can I set a unique constraints on two columns?
class MyModel extends Migration {
public function up()
{
Schema::create('storage_trackers', function(Blueprint $table) {
$table->increments('id');
$table->string('mytext');
$table->unsignedInteger('user_id');
$table->engine = 'InnoDB';
$table->unique('mytext', 'user_id');
});
}
}
MyMode::create(array('mytext' => 'test', 'user_id' => 1);
// this fails??
MyMode::create(array('mytext' => 'test', 'user_id' => 2);
The second param is to manually set the name of the unique index. Use an array as the first param to create a unique key across multiple columns.
$table->unique(array('mytext', 'user_id'));
or (a little neater)
$table->unique(['mytext', 'user_id']);
Simply you can use
$table->primary(['first', 'second']);
Reference: http://laravel.com/docs/master/migrations#creating-indexes
As an example:
Schema::create('posts_tags', function (Blueprint $table) {
$table->integer('post_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
$table->foreign('tag_id')->references('id')->on('tags');
$table->primary(['post_id', 'tag_id']);
});
If you have a default unique index with one column and you will change it with two columns, or create a new one with two columns, this script will do that for you:
public function up()
{
Schema::table('user_plans', function (Blueprint $table) {
$table->unique(["email", "plan_id"], 'user_plan_unique');
});
}
public function down()
{
Schema::table('user_plans', function (Blueprint $table) {
$table->dropUnique('user_plan_unique');
});
}
DB::statement("ALTER TABLE `project_majr_actvities`
ADD UNIQUE `unique_index`(`activity_sr_no`, `project_id`)");

Resources