[Symfony\Component\Debug\Exception\FatalThrowableError] - laravel-5

I am not able to seed the db and having this error. [Symfony\Component\Debug\Exception\FatalThrowableError]
Class 'Channel' not found
This is for laravel 5.3 running mysql.
function run()
{
$channel1=['title'=>'Laravel'];
$channel2=['title'=>'Css3'];
$channel3=['title'=>'Vue.Js'];
$channel4=['title'=>'CakePhp'];
$channel5=['title'=>'Node.js'];
$channel6=['title'=>'Javascript'];
Channel::create($channel1);
Channel::create($channel2);
Channel::create($channel3);
Channel::create($channel4);
Channel::create($channel5);
Channel::create($channel6);
}
I am expecting to seed the db

Related

error when executing php artisan migrate seed in console

I write php artisan migrate:fresh --seed in the console of the root folder of a project, when I run this command, it takes near to 1 minute then it returns \
In PackageServiceProvider.php line 14:
syntax error, unexpected 'Package' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST)
PackageServiceProvider.php:
namespace Spatie\LaravelPackageTools;
use Carbon\Carbon;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Str;
use ReflectionClass;
use Spatie\LaravelPackageTools\Exceptions\InvalidPackage;
abstract class PackageServiceProvider extends ServiceProvider
{
protected Package $package; /* line 14 */
abstract public function configurePackage(Package $package): void;
public function register()
{
$this->registeringPackage();
$this->package = new Package();
$this->package->setBasePath($this->getPackageBaseDir());
$this->configurePackage($this->package);
if (empty($this->package->name)) {
throw InvalidPackage::nameIsRequired();
}
foreach($this->package->configFileNames as $configFileName) {
$this->mergeConfigFrom($this->package->basePath("/../config/{$configFileName}.php"), $configFileName);
}
$this->packageRegistered();
return $this;
}
.
.
.
.
}
the project's author's PHP version: 7.4.19
my PHP version: 7.3.27
I'm noob in laravel, so if I have to show up with more info about the issue tell me. \
edit
after updating the PHP version to 7.4.21
I wrote the command and it returned
C:\xampp\htdocs\Business-Manager>php artisan migrate:fresh --seed
**************************************
* Application In Production! *
**************************************
Do you really wish to run this command? (yes/no) [no]:
> y
Illuminate\Database\QueryException
SQLSTATE[HY000] [1045] Access denied for user 'forge'#'localhost' (using password: NO) (SQL: SHOW FULL TABLES WHERE table_type = 'BASE TABLE')
at C:\xampp\htdocs\Business-Manager\vendor\laravel\framework\src\Illuminate\Database\Connection.php:692
688▕ // If an exception occurs when attempting to run a query, we'll format the error
689▕ // message to include the bindings with SQL, which will make this exception a
690▕ // lot more helpful to the developer instead of just the database's errors.
691▕ catch (Exception $e) {
➜ 692▕ throw new QueryException(
693▕ $query, $this->prepareBindings($bindings), $e
694▕ );
695▕ }
696▕
1 C:\xampp\htdocs\Business-Manager\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDO\Exception.php:18
Doctrine\DBAL\Driver\PDO\Exception::("SQLSTATE[HY000] [1045] Access denied for user 'forge'#'localhost' (using password: NO)")
2 C:\xampp\htdocs\Business-Manager\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOConnection.php:43
Doctrine\DBAL\Driver\PDO\Exception::new(Object(PDOException))
my MySQL accounts :
enter image description here
Your problem is that protected Package $package; is PHP 7.4, it should be like protected $package;.
As you can see in the source code, it required php ^7.4 or ^8.0, so you have to change your PHP to either of those.
This is another place to see the composer package you are downloading, to see more info about it...
import suitable class in the top.
use Facade\Ignition\Support\Packagist\Package;

Generating models from existing database at once

I'm trying to generate models from an existing database at once without having to do it separately for all tables. I have tried to do this with reliese/laravel. I have executed:
php artisan -v code:models
However, I'm getting the following error.
ErrorException : mkdir(): Invalid path
at C:\xampp\htdocs\schaden\vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php:466
462| if ($force) {
463| return #mkdir($path, $mode, $recursive);
464| }
465|
466| return mkdir($path, $mode, $recursive);
467| }
468|
469| /**
470| * Move a directory.
Exception trace:
1 mkdir("")
C:\xampp\htdocs\schaden\vendor\laravel\framework\src\Illuminate\Filesystem\Filesystem.php:466
I'm not posting the full error stack here. Can anyone help?
Probably the best solution is using the package Eloquent Model Generator that you can find on github at https://github.com/krlove/eloquent-model-generator.
Then you can easily use, for example, php artisan krlove:generate:model User --table-name=users or php artisan krlove:generate:model MyModel --table-name=my_models and use some of the package options.

Laravel PHPUnit Call to a member function connection() on null

I am learning testing on Laravel and I am unable to continue testing Eloquent by this error:
Error: Call to a member function connection() on null
This is my code:
public function test_users_can_follow_each_other()
{
$this->userOne = User::find(1);
$this->userTwo = User::find(2);
$this->userOne->followedBy($this->userTwo->id);
$this->assertTrue($this->userTwo->isFollowedBy($this->userOne->id));
}
I am also using the trait use DatabaseTransactions;
The database is already created with 2 records on users table. Do I need to config anything else, thanks!
replace
PHPUnit\Framework\TestCase
with
Tests\TestCase

Call to a member function connection() on null Laravel 5.4

Try write a unit test and i need do sql query
class UpdateThrowsTest extends TestCase
{
protected $bgame;
protected $game_id = 95;
public function setUp(){
$game = new Game();
$game = $game::find($this->game_id);
}
}
and then i write "phpunit" in console and try exception
Call to a member function connection() on null.
If anyone bounce to this error during test with Laravel 6 project.
Try to check if the extends TestCase is using the right TestCase.
It could be due to Laravel 6 make:test generated test using the wrong TestCase.
Change
use PHPUnit\Framework\TestCase;
To
use Tests\TestCase;
The problem should solve.
I had this error on laravel 7 (nothing worked even php artisan serve) and fixed it with
composer dumpautoload
Before that update my vendor with composer update and then everything worked fine.

laravel DB::connection()->getPdo()::PARAM_STR not working

The function getPdo() with PARAM_STR in laravel
DB::connection()->getPdo()::PARAM_STR
is working fine with php 7.0.0 but not working with php 5.6.16 or lesser versions. How can I get the PARAM_STR from PDO instance in laravel with php 5.6.16 or less?
I have tried
DB::connection()->getPdo()->PARAM_STR
but not working for me..
The solution which worked for me is this..
static function db ()
{
try {
$db = DB::connection()->getPdo();
}
catch (PDOException $e) {
self::fatal(
"An error occurred while connecting to the database. ".
"The error reported by the server was: ".$e->getMessage()
);
}
return $db;
}
By calling..
$db=self::db();
$db::PARAM_STR
I got it solved. All inside class & method

Resources