I want to create the migrations file in dynamic. I developed a product management system and there are multiple stores in the system. I want to create product tables for the store when registering the store.
my table names should like
store1_products
store2_products
store3_products
table structure are same
want to create these migration files in store create function
I all ready tried schema function on controller .
Schema::create($tableName, function (Blueprint $table) {
$table->string('email')->index();
$table->string('token');
$table->timestamps();
});
It created the table on the database but I want to create the migration file too
I suggest to just create a one table for the store and one table for the product. Then the product table must have a column that identify what store it is belongs to.
Stores Table.
Schema::create('stores', function(Blueprint $table) {
$table->id();
$table->string('store_name');
$table->timestamps();
});
Products Table
Schema::create('products', function(Blueprint $table) {
$table->id();
$table->string('product_name')->unique();
$table->unsignedFloat('price');
$table->unsignedTinyInteger('quantity');
$table->foreignId('store_id')
->constrained('stores')
->cascadeOnDelete();
$table->timestamps();
});
Then you can refer to Laravel Eloquent Relationship to know how to define their relationship.
Just make sure that you have model for them
Related
I have a laravel project working fine but i can't see relationship in sql server.(for which i am using xampp).
I expect the foreign-id in laravel is the foreign id in actual table. It is working fine in laravel, i expect it to work in actual database server also.
This is my model, Student Model and migration:
public function user() {
return $this->belongsTo(User::class);
}
Schema::create('students', function (Blueprint $table) {
$table->id();
$table->string("username")->unique();
$table->foreignId("user_id");
$table->foreignId("course_id");
$table->bigInteger("class_roll");
$table->integer("year");
$table->integer("semester")->nullable();
$table->timestamps();
});
User Model and migration:
public function student()
{
return $this->hasOne(Student::class);
}
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->boolean('is_active');
$table->enum('role', ['admin','instructor','student']);
$table->string("fullname");
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
In designer view, there is no link between students.user_id, and users.id:
There are many better guides out there that explains this, so I will try to keep this simple, there are 2 things you need to understand about how database relationships work within laravel:
1- You can connect your database tables to be related to each other, this has many benefits where you can cascade delete data as an example.
You are currently not telling mysql which tables are being connected, you can see a better way to do this from the following stackoverflow: laravel migration best way to add foreign key
2- You can connect your database tables using Models, this will not show up anywhere in MYSQL designer, because this is mainly code base, which is enough to handle all of the data processing between relationships.
This part you already did.
I need to check if user id exists in foreign table. I have two three tables which are staffs, packaging and cutting. Both the tables packaging and cutting has staff_id column I need to check if the staff belongs to packaging or the cutting. So how do I achieve this.
Packaging table
Schema::create('packaging', function (Blueprint $table) {
$table->increments('id');
$table->integer('staff_id');
$table->string('business_name');
$table->string('tax_id');
$table->string('phone_number');
$table->timestamps();
});
Cutting table
Schema::create('cutting', function (Blueprint $table) {
$table->increments('id');
$table->integer('staff_id');
$table->string('business_name');
$table->string('tax_id');
$table->string('phone_number');
$table->timestamps();
});
I tried check using belongsTo and hasOne relation in Staff Model. but it didn't work.
public function packaging()
{
return $this->hasOne(\App\Models\Admin\PackagingCompany::class,'staff_id');
}
You should apply foreign key constraints. See docs
Schema::create('packaging', function (Blueprint $table) {
$table->increments('id');
$table->foreign('staff_id')->references('id')->on('staff');
$table->string('business_name');
$table->string('tax_id');
$table->string('phone_number');
$table->timestamps();
});
This will prevent you from being able to save a packaging record without a valid staff_id
You can also apply validation anywhere an input stores a staff member against a packaging model. This would go in your form request injected into your controller.
public function rules()
{
return [
'staff_is' => 'exists:staff',
];
}
You also mentioned that you need to check if the staff member is associated with the packaging or cutting table.
With the kind of relationship you have in the migrations you've given, a staff member could belong to both packaging AND cutting. Is that what you want?
In which case you can create packaging and cutting relationships on your User model.
public function packaging()
{
$this-> hasMany(Packaging::class)
}
And then query users that have any packaging records by doing:
// Retrieve all users that have at least one packaging record...
$users = User::has('packaging')->get();
I tried to follow the documentation of laravel and laracasts, however I get an error in my migrations concerning foreign keys.
I have articles about games that are written by authors (users), so I want the user Id for the article.
This is my games table:
public function up()
{
Schema::create('games', function (Blueprint $table) {
$table->id('id');
$table->timestamps();
$table->string('name', 100);
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->text('description');
$table->string('publisher');
$table->string('url');
});
}
And this my user table:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
If I understand correct I name the column in the games table 'user_id', and it references to the Id of the user, which is called 'id' in the user table?
When migrating however i get the error theres no user_id.
SQLSTATE[42000]: Syntax error or access violation: 1072 Key column 'user_id' doesn't exist in table (SQL: alter table gamesadd constraintgames_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
Can someone tell me what I am doing wrong? Am I mixing up where I should refer to 'id' in the user table?
Migrations by default are run with the same order that they are been created, and in your case, the games migration is been executed before the user migration, and so you when migrating the games migration, there is no user table, and so
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Will fail.
In order to solve this problem, rename the user migration with a date before the one that has the games migration
Thanks to #lagbox for having pointed it out:
This line is not creating a new field, but is just adding a constrain
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Instead you should first create the field and then create the constrain:
$table->bigInteger('user_id)->...;
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
The way I usually do it is :
Creating the migrations files (containing all the fields)
Creating alterations files (adding FK)
Like that, when I run php artisan migrate the order of the migrations doesn't block anything.
With your code above you are trying to add a foreign constraint on a field which doesn't exist yet
I wanted to add a new unique column called uuid into an existing table. This is my migration function.
public function up()
{
Schema::table('suppliers', function (Blueprint $table) {
$table->uuid('uuid')->index()->after('id');
});
}
However, the table already has existing records, ie 100+ records. How do I create a function to populate this new column with unique values for the existing records? Thanks.
you have to specify the type of the column so change $table->uuid to $table->string. so try following.
Schema::table('suppliers', function (Blueprint $table) {
$table->integer('uuid')->unique()->after('id');
});
ProductTable
public function up()
{
Schema::create('product', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('description');
$table->string('type');
$table->integer('size');
$table->integer('price');
$table->string('image')->nullable();
$table->timestamps();
});
}
When I hit submit, I got error saying base table not found
Laravel can not find the prular form of the table name that you used, just specify your table name on your model like so; And check your view aswell make sure on your resource/view you have a file named successs.blade.php
public $table = "yourTableName";
may be your table is not available in database so first add the table in
your database. in your model like this
public $table = "yourTableName";
after the define your table in your model just migrate the table in your database
php artisan make:migration