Something wrong with my Laravel - php

there's defenitely something wrong with my Laravel.
I have the Laravel 5.4 installed. I have installed Composer on my windows globally. I have php artisan working, but I'm getting errors everywhere:
php artisan tinker
When I type in php artisan make:model Brand it says Model created succesfully.
Awesome, right? When I type in php artisan tinker and App/Brand::all() next, it says Class 'Brand' not found.
While I have Brand.php inside my app folder:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Brand extends Model
{
//
}
By the way, I'm following this series at laracast.
mySQL
I can't run the mysql command, though I have manually installed mySQL on my windows (globally as well).
It says
'mysql' is not recognized as an internal or external command,
operable program or batch file.
migrate
php artisan migrate gives me this error:
[Illuminate\Database\QueryException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key
length is 767 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`))
[PDOException]
SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key
length is 767 bytes
Besides this php artisan migrate is working very well, it creates two tables for me: migrations and users. I thought that was because I have two files in my \database\migrations folder. But...
When I say php artisan make:migration create_brands_table --create=brands, it creates a file inside this folder indeed. Like it should. I add a few lines inside the up() function so it looks like this:
Schema::table('brands', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->text('description');
$table->timestamps();
});
But it does not create a table.
Please note that I did edit my .env file to make a proper database connection, otherwise php artisan migrate would not work on the other two tables I did create with the php artsian migrate command.
It only gives a alreadt exists error. I tried migrate:rollback and migrate:refresh as well. I tried doing this after the composer dump-autoload command, but it came with the same result.
I'm lost in the dark here, I am obviously new to Laravel, but some friends of mine who are familiar with Laravel have never experienced the problems I have.
I have tried to reinstall and create a new project and all... guess what: same results.

You got namespaces wrong, use backslash: App\Brand
To create a table use Schema::create and not Schema::table
The mysql part is not Laravel's concern and should not be in this question. Anyway you should check your environment PATH variable.

Regarding the already existing table issue, I ran into that before and here's what I've done, first delete your database file, in my case I was using sqlite so my file was database.sqlite, I deleted that and created a new one(don't delete it permanently just in case), run your migrations normally to be sure everything is working for the normal tables, also delete your old Brand migration, then when you are creating a new migration for your Brands table, lose the --create flag,
Just like this php artisan make:migration create_brands_table, at this point do as mentioned before and use the Schema::create instead of Schema::table, hopefully everything should be working fine now.

Related

Laravel migration did not work

When i give command "php artisan migrate" it gives me error :
base table or view already exists: 1050 table 'users' already exists.
And does not update my DB's table. I rolled back and migrate it again but did not have any effect on DB, I create new project but it gives error too on the this command.
In your migrations make this check first
Schema::hasTable('users');
if you don't have data, you can drop the columns manually first.
I suppose your migration is creating a table users in DB. This error simply means that table users already exits in such DB, so it can't be created.
What you can actually do, is to create a new empty DB or re-create your whole DB in case you can afford loosing your data, which I assume from your post.
Rollback undo only last migration step. You could possibly run:
$ php artisan migrate:rollback --step=2 # rollback two last migrations
or
$ php artisan migrate:reset # rollback all migrations
I suggest you to go through Database: Migrations, so you will gain more knowledge about DB migrations.
Because you ran the migration, there is a reference of the ran migration in the migrations table in your database. You need to manually delete that record in order for you to run the same migration again.
migration table keeps a track of the migration you run. before the migration actually happen Laravel writes a record to migration table.
If an error occur, migration fails but the reference record exists. hence the error
base table or view already exists: 1050 table 'users' already exists.
The easiest way to handle this is, fix the issue with the migration file, remove the reference record from migration table and run the migration again.
Please follow the steps below:
first of all check your current migration status
php artisan migrate:status
skip already imported tables and look for new migration files and remove create statement from your new migration files.
The issue here is that your new migration files may contain create statement rather then alter/drop etc.
once you fix that statement try running
php artisan migrate

php artisan:rollback error in laravel 5.3

I tried building a database where I forgot to place the index of the table. Now I'm trying to rollback the migration but its not working out. It is throwing some error
[ErrorException] Undefined index: 2017_01_06_195028_create_users_themes_table
Now I tried deleting it manually so I deleted the migration file from the database folder and then did composer dump-autoload, and then did rollback it is still showing same error. Also when this didn't happened I tried placing index in the table through phpmyAdmin, it ain't helped! Still i deleted the table manually and tried doing composer dump-autoload and rollback it still has same error.
Help me out with this.
You can backup DB data (if any), drop DB and create it again and run php artisan migrate command. It's the easiest way to fix this I guess.
Another thing you could try is to delete last batch from migrations table and drop tables from last batch and run php artisan migrate
When you do php artisan migrate, migration table is created and it records the order of the migration you run. And when you rollback the list get cleared in the order of rollbacks..
In your case since you got an error the record corresponding to your migration probably still in the migration table.
Now if you wish to update your migration file and migrate again, you needs to manually clear the corresponding record from the migration table. Most probably it would be the last record in the migration table.
Clear out that record, fix your migration file and run your migration. You should be good to go.
I forgot to mention that I'm having my migration files in different folder so everytime I'm doing a php artisan migrate rollback I need to specify the path of those migration file that is why it is showing undefined Index error.

artisan migrate results: SQLSTATE[HY000]: General error: 1005 on LUMEN / LARAVEL

I'm trying to run php artisan migrate command but I am getting the following error.
I have looked at many forums and tried several solutions but nothing solved my problem.
What is going on?
-
Migrations:
http://pastebin.com/mDFa1suK
http://pastebin.com/tFWj9bEd
You can not create "produtos" table with a foreign key reference before creating "categorias" table
i suggest to change to change date in migration files name to re-order migration
so that migration of "categorias" table will run first
i hope that answered the question.
I notice that in your migration files, there are no table name cremasco.#sql-3076_1e but the error showing cannot create that table.
pls check your migration table in you database see if it has the cremasco.#sql-3076_1e. and run this in your command line :
composer dumpautoload then try again to migrate the database php artisan migrate
if the problem still exist then you need to drop your databases, and migrate again.
This problem exist because we deleted the files migration but the
table migration in database still has it
.

Error on php artisan migrate

I have read many post on this subject, but have not found the right answer. When i write any command php artisan migrate returns a result:
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'adtmart1.shop_categories' doesn't exist (SQL: select * f
rom `shop_categories`)
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'adtmart1.shop_categories' doesn't exist
I would like to move the finished website made using Laravel on the local web server. I use Web server - opensever. There php version - 5.5, Mysql - 5.5. All commands i write of the console opensever. While replying, please, take into consideration that I am new in this field
You use Schema::table to alter an existing table, you are looking for Schema::create which is used to create a new table.
Alter your migrations to use Schema::create and you'll have no trouble running your migrations:
Schema::create('name_of_table', function(Blueprint $table) {
{
$table->increments("id",true);
$table->string("username")->nullable()->default(null);
$table->string("password")->nullable()->default(null);
$table->string("email")->nullable()->default(null);
$table->timestamps();
});
I have of course used dummy columns and you would use your own.
More info on the problem here.
First of all, you'll need to post migration code that causes the error. To do this, try to run migrations one-by-one. Remove all migrations from /database/migrations folder and then add first one (first by date it was created) back into /database/migrations. Then run php artisan migrate. If migrations was successful, do all steps for secons migration etc. When you see an error, you'll know that this one migrations causes it. Post it here please, so we could help.

Laravel: Base table or view not found: 1146 Table 'database.pages doesn't exist

I'm working on a CMS and I have a little problem with my migrations. I added a new migration file and I wanted to add that one. That didn't work so I ran this bit:
php artisan migrate:reset
After that I ran this bit:
php artisan migrate:install
php artisan migrate
And now I get this error:
{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S02]: Base table or
view not found:1146 Table 'cms.pages' doesn't exist (SQL: select * from `pages`)"
The error kinda tells me that it can't find the database, because that's true.
I also have a command that runs the migrate and I run that one like this:
php artisan app:install
But that shows the same error...
Remove any lines requesting data from your model from these files to be sure artisan is not trying to load data from your non-existent table:
bootstrap/start.php
app/start/global.php
app/start/local.php
app/routes.php
Also be sure to un-register any service providers that utilize data from that table in their register or boot methods inside of app/config/app.php.
The issue is that these files not only get executed for browser (web) requests, but for all requests, including command-line artisan invocations (e.g. php artisan migrate). So if you try to use something before it is available in any of these files, you are going to have a Bad Time.
You can use this to dictate when your app is running from the console.
I believe this issue only occurs when you run a command
if( !App::runningInConsole() ){
//allow laravel-menu to run
}
This way you will prevent data load from your non-existent table

Categories