Base table or view not found error - laravel

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

Related

Undefined table: 7 ERROR: relation "users" does not exist

Friends I have the following problem with laravel migrations using postgres, and when I make changes to a migration, in this case the users table, but I get an error trying to remove an index from a key, can you help me please with this problem.
This is my migration code:
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->integer('idProfile');
$table->string('name');
$table->string('surname');
$table->string('email')->unique();
$table->string('photo')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
public function down() {
Schema::dropIfExists('users');
Schema::table('users', function (Blueprint $table){
$table->dropPrimary('id');
$table->dropIndex('users_pkey');
});
}
response from my console:
These are the indices that list me:
This is the structure of the final table:
Comments, things to improve I am all ears
When you are running migrate:refresh, you are running the down method. In your down method, you are dropping the table, and then trying to make edits to it, which is why you are getting "users" doesn't exist.
If this is just in a development env, make your changes in the up method, and remove everything apart from dropIFExists() from the down method.
It is highly recommended that don't change the migration file...
If you need to add a new row to your table (Consider you have mytable and you want to add myrow to the table), you can write in terminal :
php artisan make:migration add_myrow_to_mytable_table
after that , edit new added migration file!
remember to add the following code in down function :
Schema::table('mytable', function (Blueprint $table) {
$table->dropColumn('myrow');
});
after all, run :
php artisan migrate
If you want to remove a column from your table just follow this one :
Laravel Migrations - Dropping columns

Laravel 8 - Foreign key constraint is incorrectly formed

I don't know what's wrong because I'm very new to this.
// Product Model
class Product extends Model
{
use HasFactory;
public function store()
{
return $this->belongsTo(Store::class);
}
}
// Store Model
class Store extends Model
{
use HasFactory;
public function products()
{
return $this->hasMany(Product::class);
}
}
// Products table migration
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->float('price');
$table->string('description');
$table->timestamps();
$table->foreignId('store_id')->constrained()->onDelete('cascade');
});
// Stores table migration
Schema::create('stores', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('image_url');
$table->string('phone');
$table->timestamps();
});
When I run the migration, it gives me this error
I've tried changing the data type of the 'id' but still not working. I've also tried with
$table->foreign('store_id')->references('id')->on('stores')->onDelete('cascade');
but still not working.
What I want is a relation so that when I delete a store, all products that belong the store are also deleted.
Thanks 🙏
Change the name of the stores migration file to a date prior to 2021-07-28 so the table stores is migrated before the table products
Example: 2021_07_27_004700_create_stores_table
Laravel uses the name of the migration files for the order of migration. With the format of the date as the start of the file name, it is dependant on the date of the creation of the file.

Laravel 5.6 set migration nullable foreign id

This error popup:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`ltfrbr10infosystem`.`franchises`, CONSTRAINT `franchises_operator_id_foreign` FOREIGN KEY (`operator_id`) REFERENCES `operators` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
Laravel Migrration:
public function up()
{
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->integer('operator_id')->nullable()->unsigned();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});
}
I have tried this but still it gives me error
$table->integer('operator_id')->nullable()->unsigned()->change();
I also tried this
$table->integer('operator_id')->unsigned()->default(null);
How do I make operator_id foreign key default to null?
If the data on your database is not important you could refresh your migrations and your database using
php artisan migrate:refresh
This will rollback and migrate all your migrations again. Make sure you wrote the down method right,
also, you migration should look like this:
public function up()
{
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('operator_id')->nullable();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});
}
Other way to do it is creating a new migration like this:
public function up()
{
Schema::table('franchises', function (Blueprint $table) {
$table->unsignedInteger('operator_id')->nullable()->change();
});
}
I think you get this error because the record you are trying to insert contains a wrong value for the column operator_id. This value is not a correct operator id and is not null (it could be 0, "null" or empty string, ...)
Can you paste here the exact SQL query which rises this error ?
Add this inside up function and run php artisan migrate:refresh --seed
public function up(){
Schema::disableForeignKeyConstraints();
Schema::create('franchises', function (Blueprint $table) {
$table->increments('id');
$table->integer('operator_id')->unsigned()->nullable();
$table->foreign('operator_id')->references('id')->on('operators')->onDelete('cascade')->onUpdate('cascade');
$table->string('case_number')->nullable();
$table->string('business_address')->nullable();
$table->date('date_granted')->nullable();
$table->date('expiry_date')->nullable();
$table->string('route_name')->nullable();
$table->string('deno')->nullable();
$table->integer('authorize_units')->nullable();
$table->string('remarks')->nullable();
$table->timestamps();
});Schema::enableForeignKeyConstraints();}
you should use this
$table->foreign('operator_id')->references('id')->on('operators')->nullable()->onDelete('cascade')->onUpdate('cascade');

How can I add field using alter table on the migration laravel?

I use laravel 5.3
My migration like this :
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('api_token')->nullable();
$table->string('email',100)->unique();
$table->string('password')->nullable();
$table->string('avatar',100)->nullable();
$table->string('full_name',100)->nullable();
$table->date('birth_date')->nullable();
$table->smallInteger('gender')->nullable();
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::dropIfExists('users');
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
}
I want to add new field like this :
$table->string('mobile_number',20)->nullable();
But I don't want to add it on the schema. I want to use alter table
On my staging server and live server had set automatic migration
So if I use alter table, it will automatic migration. So on the table in database will automatic add mobile_number field if the code merge to development or master
If I add on the schema, it will not automatically migrate
How can I add the field using alter table?
You can use the schema to update an existing table. Create a new migration using the artisan command then add something like
Schema::table('users', function (Blueprint $table) {
$table->string('mobile_number',20)->nullable();
});
If you really wanted to do RAW sql you can do something like
DB::statement("ALTER TABLE users .....");
However the schema way is much better if you can get it to work
In the command line, execute artisan command to add new migration for your table:
php artisan make:migration add_mobile_number_to_users_table --table=users
Then you can put your code inside the newly created migration file:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->string('mobile_number',20)->nullable();
}
}
public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('mobile_number');
}
}

How to set a comment on table using Laravel Schema Builder

How to set a comment on table using Laravel Schema Builder?
Column set:
public function up()
{
Schema::create('vendors', function (Blueprint $table)
{
$table->comment('not working - error'); // not working - error
$table->increments('id');
$table->string('vendor', 255)->comment('Some comment.');
$table->timestamps();
});
}
But for the table?
Well I don't have a nice answer for you, but at least it works.
Here it is:
public function up()
{
$tableName = 'vendors';
Schema::create($tableName, function (Blueprint $table)
{
$table->increments('id');
$table->string('vendor', 255)->comment('Some comment.');
$table->timestamps();
});
DB::statement("ALTER TABLE `$tableName` comment 'My comment'");
}
Just add a DB statement after creating your table.
Before Laravel 9, only columns were allowed to comment. Since laravel 9 the ability to comment on the table itself has been added.See this PR and the Blog article
If you would like to add a "comment" to a database table, you may invoke the comment method on the table instance. Table comments are currently only supported by MySQL and Postgres:
Schema::create('calculations', function (Blueprint $table) {
$table->comment('Business calculations');
// ...
});

Resources