Eloquent Tinker - Undefined Constant - php

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;

Related

Laravel - Creating Custom Classes throws error of undefined

I've been following this short tutorial and wanted to make my own class and call it simple like classname::methodname, but without injection.
I get the following error:
exception 'Symfony\Component\Debug\Exception\FatalErrorException' with
message 'Call to undefined method
App\Facades\LiveSearch::getServiceList()'
Link to Tutorial: here
It makes sense and that is why i cannot figure out where the problem lies ..
Hey you also need to do the follow up steps to create laravel classes.
I hope this reference on How to create a laravel class is helpful.

Handle Laravel 5.3 QueryException?

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.

yii2 - how to truncate a table from console

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.

Work with laravel tinker

I'm new to laravel .
I want to work with php artisan tinker in cygwin but I am getting some errors in making an object. I watched some videos and the guy did this:
$article = new app\articles;
but when I do this it says:
PHP Fatal error: Class 'app\articles' not found in eval()'d code on line 1
I tried this too:
$article = new c://xampp/htdocs/laravel/app/articles;
but it says this:
PHP Parse error: Syntax error, unexpected ':' on line 1
Tinker is case sensitive so if you are trying to create the object the way you are doing it it will fail so instead try placing the whole namespace in the exact way it is on your project
$article = new App\Article;
Use terminal to go to your project, and type :
php artisan tinker
Then type the following command for example to create a new Article:
$article = new Article;
After that you can manipulate the article like any Article object :
$article->title = "foo";
Maybe you should take a look at Laravel Fundamentals at laracasts.com, it's a greate serie for Laravel beginners.
Regards.

PHP Artisan throwing strange error [42s02]

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

Categories