Store multiple images - php

Hello how can i upload multiple images to my table, for now i have this table
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->string('title', 70);
$table->string('slug', 100)->unique();
$table->string('images');
$table->text('content');
$table->softDeletes();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
i want to store multiple images in "images" field, how could i do that ?

If you want to store the images data directly in a database column, it's not the best practice at all. Read this https://stackoverflow.com/a/6472268/830130
Differently if you want to do the right way in my opinion it's better to delete that string column you called "images" and create an images table, with a foreign key referencing the id on articles.
In Laravel the relation can be done using the Eloquent relationships.
In your case you can decide to have a one-to-many: https://laravel.com/docs/5.1/eloquent-relationships#one-to-many
or a many-to-many relationship: https://laravel.com/docs/5.1/eloquent-relationships#many-to-many

Related

laravel onDelete('cascade')

I have users table and session_requests table. When a user delete from user table, all session_requests related to him want to delete. I used laravel onDelete('cascade').
Schema::create('session_requests', function (Blueprint $table) {
$table->id();
$table->string('headline');
$table->string('objective');
$table->integer('status')->default(0);
$table->string('datetime');
$table->unsignedBigInteger('user_mentor_id');
$table->unsignedBigInteger('user_mentee_id');
$table->foreign('user_mentor_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('user_mentee_id')->references('id')->on('users')->onDelete('cascade');
$table->timestamps();
});
When I delete a user from users table, why don't delete data record in session_requests related to him. Could you please help me anyone? Thanks.

Laravel 9 adding foreign key constraint returns 150 error

I have created Roles table with Users table, I am trying to add foreign key to users table, but when I migrate, it returns 150 error.
I have googled a lot, I got a lot of answers, but no answer worked for me.
create_users_table Migration:
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->foreignId('role_id')
->constrained()
->onUpdate('cascade')
->onDelete('cascade');
$table->rememberToken();
$table->timestamps();
});
create_roles_table Migration:
Schema::create('roles', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
Can someone tell me what's wrong? I am stuck..
The problem is that roles table doesn't exist when you're creating a foreign key inside the users table.
So, your roles migration must be executed before you create a foreign key inside the users table.
There are 2 ways you can do this.
1- You either rename the roles and change the timestamp in name to be before the users migration. ( Not a good way though )
2- First create the tables and then create a new migration to insert the foreign key inside the users table.

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint Error

I want to add role column on users table. But I got this error.
My Users migration
Schema::create('users', function (Blueprint $table) {
$table->increments("id");
$table->string('name');
$table->string("company_name")->unique();
$table->string('email')->unique();
$table->string("phone")->unique();
$table->boolean("status");
$table->integer("role")->unsigned();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->foreign('role')->references('id')->on('roles')->cascadeOnUpdate()->cascadeOnDelete();
});
And my Role migration
Schema::create('roles', function (Blueprint $table) {
$table->increments("id");
$table->string('name');
});
The problem is occurring most probably from wrong migration order. As you are storing roles table id in your users table, you will have to migrate roles table first then your users table. As I don't know the order of your tables, I will assume from your post's order that the users table is being migrated first then the roles table, which is why users table is not being able to get the id reference of the roles table.
Try renaming the date of the roles table to a previous date than the users table and run php artisan cache:clear then php artisan migrate:fresh.
Just a couple of suggestion, as you have specified you are using Laravel 8, please use the id(); function instead of using increments("id");. To follow Laravel's foreign key rule please use role_id as the foreign key instead of role.
Instead of using: $table->foreign('role')->references('id')->on('roles')->cascadeOnUpdate()->cascadeOnDelete();
use:
$table->foreignId('role_id')->constrained()->onUpdate('cascade')->onDelete('cascade');
You need to change your code as follows,
Schema::create('users', function (Blueprint $table) {
$table->increments("id");
$table->string('name');
$table->string("company_name")->unique();
$table->string('email')->unique();
$table->string("phone")->unique();
$table->boolean("status");
$table->unsignedBigInteger("role");
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
$table->foreign('role')->references('id')->on('roles')->cascadeOnUpdate()->cascadeOnDelete();
});
Based on the documentation here, you have used integer instead of unsignedBigInteger.
Try changing
$table->integer("role")->unsigned();
To
$table->foreignId('role_id');
$table->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade')->onDelete('cascade');
Make sure that role migrations has pre order in migrations before users migrations,
you can edit the migration name of roles migrations( edit timestamp to make it migrate before the users)

Add cascade/delete functionality to existing migration

I have 2 tables in my database. One for Courses and one for Course Chapters.
The migration for the courses looks like this:
Schema::create('courses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
The migration for the chapters looks like this:
Schema::create('course_chapters', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('course_id');
$table->timestamps();
});
I want the course and the chapter to cascade down, so when i delete a course, the chapter also will be deleted.
Some examples i saw make use of deleting the foreign key but I never signed my column as a foreign key.
For example, normally, I could:
$table->dropForeign('course_id');
$table->foreign('course_id')
->references('id')->on('courses')
->onDelete('cascade');
How can i accomplish this in a (preferably) new migration and on what table should i add the foreign key?
This as it is should go on your course_chapters table:
$table->foreign('course_id')->references('id')->on('courses')->onDelete('cascade');
You don't need to add $table->dropForeign('course_id'); because that will drop the foreign key from the column.
NOTE: and this:
$table->unsignedInteger('course_id');
Should be this:
$table->unsignedBigInteger('course_id');
Because it will throw an error of using different data types.

(Laravel) tables relationship and usage of foreign key

I have a migration with three tables and i have a question which decide the how the tables will connect to each other and do i really a foreign key on them so what i want to do I have two roles 1 admin - 2 user and i want to assign one of them to each user because i want only admin users to login into backend dashboard as for now i make the relationship between roles and users as one(role)-to-many(users) Also the relationship between posts and users is one(user)-to-many(posts) since the post in only related to one user only but the user can have multiple posts so i need you help here to understand those thing in better way and when i need to setup a Foreign Key in the table.
This is my tables:
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('fullname');
$table->string('username')->unique();
$table->string('email')->unique();
$table->string('password');
$table->integer('role_id')->unsigned();
$table->foreign('role_id')->references('id')->on('roles');
$table->rememberToken();
$table->timestamps();
});
Schema::create('roles', function (Blueprint $table) {
$table->increments('id');
$table->string('name')->unique();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('content');
$table->string('image');
$table->timestamps();
});
From what I am understanding, you have three tables: roles, users, and posts.
Whenever you have a relationship like a one-to-many, the table corresponding to the many side will contain the foreign key to the table corresponding to the one side. You do not put any foreign key in the other table.
Now the roles and user table share a one-to-many relationship respectively, thus you put the role_id in the user table and do not put any foreign key in the roles table. Similarly, the posts table is on the many side of the relationship (one user can have MANY posts) so you have to put user_id in the posts table but no foreign key in the user table.
After you do create the migrations, to be able to query in between the tables, you need to define model files. These model files will contain methods used to retrieve associated records. For a one-to-many relationship, you will be required to have two models (for both tables) and in those models use functions as: belongsTo for the table with the foreign key, and a hasMany function for the table without the foreign key. For your case, the user model will have a hasMany function for the posts model, and a belongsTo function for the roles model.
Hope that clears it up

Categories