I'm getting a weird error trying to use PHP Artisan. I don't know the cause, or even which file is causing it.
Just a few moments ago I ran php artisan migrate:reset to wipeout all the tables in my db. Worked fine.
Then I made a change to a file completely unrelated to the "stages" table, and when I try to run php artisan migrate it throws an error saying:
SQLSTATE[42S02]: [Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid object name 'stages'. (SQL: select * from [stages] where [stages].[deleted_at] is null order by [opord] asc)
It throws that error no matter what I try to do, even just typing php artisan causes that fatal error.
Also, if I manually add a table called "stages" to the DB, artisan doesn't fail immediately, it tries to migrate the tables, then fails when it gets to the stages table, and it says it failed because "stages" already exists.
I don't even understand why artisan is trying to perform a select statement when I'm just trying to migrate my tables; and I especially don't understand why it's happening when running the php artisan command alone.
EDIT:
I traced the problem to some code that I had added to app\Providers\AppServiceProvider
public function boot(){
View::share('nav_stages', Stage::opord()->get());
}
I found a SO question about how to check if a table exists and I'm currently trying to make this work:
public function boot(){
if (Schema::hasTable('stages'))
{
View::share('nav_stages', Stage::opord()->get());
}else{
View::share('nav_stages', null);
}
}
The problem was caused by a bit of code I had in AppServiceProvider.php In the code I was trying to query the DB for data required by the navigation menu used on the entire site (a master page, layout, partial view, etc).
I replaced
public function boot(){
View::share('nav_stages', Stage::opord()->get());
}
With
public function boot(){
if (Schema::hasTable('stages'))
{
View::share('nav_stages', Stage::opord()->get());
}else{
View::share('nav_stages', null);
}
}
And added
use Illuminate\Support\Facades\Schema;
at the top of the file
Related
enter image description here Create a table in Laravel, when trying to migrate, this error occurs to me, someone can help me
Error trying to migrate with Laravel
try put in your app/Providers/AppServiceProvider.php
public function boot()
{
\Schema::defaultStringLength(191);
}
I am using Laravel 4.2 and am calling a Stored procedure from a database on the server. Locally it works fine, but on the server, using the same DB, it gives error. The call is as follows (I just want to make select):
$result = DB::select('CALL sp_special_prices("'.$codClient.'", "'.$codProduct.'", "'.$quantity.'", "'.$grup.'", "'.$FirmCode.'")');
When running a product listing on the server, it works except for two articles, giving the following error:
Caught exception: SQLSTATE[HY000]: General error: 2053 (SQL: CALL sp_special_prices("C000000", "445706049", "1", "146", "75");)
I already checked the php versions, and am using 5.6 on both sites. Could it be some special configuration? Is that the DB is the same, the place to call the SP, is that it is different.
If the procedure doesn't return anything, you need to use DB::statement instead of DB::select. If it does return something, you need to use DB::select
If the procedure has variable behaviour (may or may not return data depending on input), I suggest you change the procedure to return some data for all input combinations
Laravel 5.3
I know what happens. In my boot function of AppServiceProvider I have code that shares data for all views:
$unread_messages = count(Message::where('status', 0)->get());
View::share('unread_messages', $unread_messages);
But, if there is no such a table ( after DB reset ), that throws an Exception
[Illuminate\Database\QueryException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'lara53.messages' doesn't
exist (SQL: select * from `messages` where `status` = 0)
[PDOException]
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'lara53.messages' doesn't
exist
If I comment that code in the boot function it is okay, and all php artisan commands are working fine.
I have tried the following:
try
{
$unread_messages = count(Message::where('status', 0)->get());
View::share('unread_messages', $unread_messages);
} catch (Exception $e)
{
$e->getMessage();
}
It throws same error. I've also tried this:
if (Schema::hasTable('messages')){
but then it shares nothing.
How do I handle this?
You can use view composer so your query is not executed instantly when provider is loaded. Instead register view composer, and query will be executed only when view actually needs it.
Read more: https://laravel.com/docs/5.2/views#view-composers
This isn't a good use of service providers, which are generally meant to bootstrap requirements of your app such as 3rd party packages. As you can see, placing queries to share data with views isn't efficient in a service provider because they are run on quite literally every single request (why should it need to run that query if you are just using the console?)
Instead, you should place this code into a middleware while considering if you should also take Robert TrzebiĆski's advice and put it in a view composer as well inside of that middleware.
I have created a console command and I need to truncate a table.
Reading the Class reference: http://www.yiiframework.com/doc-2.0/yii-db-command.html#truncateTable()-detail I am not able to understand what files I need to include in order to execute this command.
I am including:
use yii\db\Command;
use yii\db\Connection;
but not sure which one is correct.
And I have tried to execute:
$command = Yii::$app->db->truncateTable('user');
which gives me the following error:
Exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\db\Connection::truncateTable()'
and:
Yii::$connection->createCommand()->truncateTable('user');
which gives me the following error:
PHP Fatal Error 'yii\base\ErrorException' with message 'Access to undeclared static property: Yii::$connection'
I really don't understand what I need to do.
Yii::$app->db->createCommand()->truncateTable('user')->execute();
Using yii2 migrate that default function
yii2 migrate
Step 1. Create a migrate
yii migrate/create truncate_table_xxx
Step2. Edit file xxx_truncate_table_xxx
Some thing like that
class m150101_185401_truncate_table_xxx extends Migration
{
$this->dropTable('xxx')
}
Alternatively one may use:
User::deleteAll();
assuming User is active model class.
This command shows the number of deleted records.
Note, that unlike truncate deleting all records will NOT reset autoincrement counter to 1.
I'm following along with this set of tutorials on laravel (They're excellent), and I keep running into this error:
PHP error: Use of undefined constant Article
whenever I enter this line of code:
$article = new App/Article;
I've followed everything to the letter, and used the command:
$ php artisan make:model Article
Model created successfully.
Created Migration: 2015_04_11_152306_create_ar
What I find unusual is that in the tutorial, no migration was created when this command was called. Anyways, I'm trying to enter some data into my sqlite database through tinker, but I'm unable to create an Article object, and therefore cannot insert into the database. How would I got about either solving or bypassing this issue?
You should use backslash \:
$article = new App\Article;