schema builder laravel migrations unique on two columns - laravel-4

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`)");

Related

Laravel save HasMany realtion

I have a set that has a list of group and each group has a list of attributes like this
//sets
Schema::create('sets', function (Blueprint $table) {
$table->id();
$table->string('name');
});
//groups
Schema::create('groups', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->foreignId('set_id')->constrained('sets')->onDelete('cascade');
});
//attributes
Schema::create('attributes', function (Blueprint $table) {
$table->id();
$table->string('name');
});
//set_group_attributes
Schema::create('set_group_attributes', function (Blueprint $table) {
$table->foreignId('set_id')->constrained('sets')->onDelete('cascade');
$table->foreignId('group_id')->constrained('groups')->onDelete('cascade');
$table->foreignId('attribute_id')->constrained('attributes')->onDelete('cascade');
$table->integer('position');
$table->unique(['set_id', 'attribute_id']);
});
I need to save the set with groups and attributes, this is my code :
$set = Set::create(['name' => $request->name]); ---> save the set
$set->groups()->createMany($request->groups); ---> save the group
//set model
public function groups(): HasMany
{
return $this->hasMany(Group::class, 'set_id','id');
}
My question is how to save the attributes with laravel eloquent?
Here we can use attach like in Docs with belongsToMany
//group model
/**
* #return BelongsToMany
*/
public function attributes(): BelongsToMany
{
return $this->belongsToMany(Attribute::class, 'set_group_attributes', 'group_id', 'attribute_id')->withPivot('position');
}
// save attributes of groups
foreach ($attributes as $attribute) {
$group->attributes()->attach($attribute['id'], ['set_id' => $model->id, 'position' => $attribute['position']]);
}

How to set indexes when adding columns to a table in 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');
});
}

Laravel 8 : SoftDeletes not updated when deleting a user linked to a form

I have a problem on Laravel 8 when I delete a user I have my 'deleted_at' which is updated in my 'user' table but not the 'deleted_at' of my 'forms' table because I want to delete the forms linked to this user otherwise I have an error because I display his info when he does not exist anymore.
How do I fix this problem please?
I have used the soft delete in the Models
and the 'deleted_at' is updated when I delete the form.
users migration :
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('firstname');
$table->string('lastname');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->foreignId('current_team_id')->nullable()->onDelete('cascade');
$table->string('profile_photo_path', 2048)->nullable();
$table->timestamps();
$table->foreignId('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->softDeletes();
});
}
public function down()
{
Schema::dropIfExists('users', function (Blueprint $table) {
$table->dropColumn('deleted_at');
});
}
forms migration :
public function up()
{
Schema::create('forms', function (Blueprint $table) {
$table->id();
$table->string('title', 100);
$table->text('message');
$table->datetime('date');
$table->string('color');
$table->softDeletes();
$table->timestamps();
});
}
public function down()
{
// Schema::dropIfExists('forms');
Schema::table('forms', function (Blueprint $table) {
$table->dropColumn('deleted_at');
});
}
add user to forms migration :
public function up()
{
Schema::table('forms', function (Blueprint $table) {
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
public function down()
{
Schema::table('forms', function (Blueprint $table) {
$table->dropForeign('user_id')->onDelete('cascade');
});
}
Soft delete won't work on cascading foreign keys. Cascading foreign keys is a feature of sql servers and soft delete is a feature of laravel and needs the laravel context to work.
When you delete it, for the soft deletes to work, you need to call delete on each model of the relation. This is quite easy with collections and higher order functions.
{
$user = User::firstOrFail($id);
$user->forms->each->delete();
$user->delete();
...
}
Thank you very much mrhn,
Thanks to you I have learned a lot!
For those which want the function in the controller which thus makes it possible to remove the user to which belongs thus the id and one thus removes also what is related to him in my case a forms (function in the Models for the cardinalities between entity) :
public function destroy($id)
{
// $users = User::findOrFail($id);
// $users->delete();
$user = User::findOrFail($id);
$user->forms->each->delete();
$user->delete();
return redirect()->route('admin');
}
findOrFail and not firstOrFail ^^'

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 migration use auto increment when its not primary

I'm trying to create a table with Laravel migrations but I'm having some trouble. I just need to create a table with a primary pair ('user_id' and 'media_id'), being 'inc' an auto increment. I can do it in MySQL, but I can't manage to do it with Laravel Migrations since increments() also set the field as primary. error i get
SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
thats i hae done so far
public function up()
{
Schema::create('tagtables', function (Blueprint $table) {
$table->integer('media_id');
$table->integer('user_id');
$table->boolean('approved')->default(false);
$table->increments('inc')->unsigned();
$table->timestamps();
$table->dropPrimary('tagtables_inc_primary');
$table->primary(array('user_id','media_id'));
// $table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
// Schema::table('tagtables', function (Blueprint $table) {
// //$table->increments('id');
// $table->primary(array('user_id','media_id'));
// $table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
// });
}
I think you need this:
public function up()
{
Schema::create('tagtables', function (Blueprint $table) {
$table->increments('inc');
$table->integer('media_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->boolean('approved')->default(false);
$table->timestamps();
$table->index(['user_id', 'media_id']);
$table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
}
and put this in the model:
protected $primaryKey = 'inc';
I found solution to problem which is ti define that column inc (which auto increments) after creating schema.
That how it should be
public function up()
{
Schema::create('tagtables', function (Blueprint $table) {
$table->integer('media_id');
$table->integer('user_id');
$table->boolean('approved')->default(false);
// $table->increments('inc');
$table->timestamps();
// $table->dropPrimary( 'tagtables_inc_primary' );
$table->primary(array('user_id','media_id'));
// $table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
});
DB::statement('ALTER Table tagtables add id INTEGER NOT NULL UNIQUE AUTO_INCREMENT;');
// Schema::table('tagtables', function (Blueprint $table) {
// //$table->increments('id');
// $table->primary(array('user_id','media_id'));
// $table->foreign('media_id')->references('id')->on('media')->onUpdate('cascade')->onDelete('cascade');
// $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade');
// });
}
chema

Resources