laravel-8 user table seeder does not exist - laravel

I am trying to make a login from laravel 8 but at the begging I faced an error which I cannot find a solution. The UsersTablesSeeder is created but still the compiler cannot find it
Illuminate\Contracts\Container\BindingResolutionException
Target class [UsersTablesSeeder] does not exist.
at C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:832
828▕
829▕ try {
830▕ $reflector = new ReflectionClass($concrete);
831▕ } catch (ReflectionException $e) {
➜ 832▕ throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
833▕ }
834▕
835▕ // If the type is not instantiable, the developer is attempting to resolve
836▕ // an abstract type such as an Interface or Abstract Class and there is
1 C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:830
ReflectionException::("Class "UsersTablesSeeder" does not exist")
2 C:\xampp\htdocs\pary\vendor\laravel\framework\src\Illuminate\Container\Container.php:830
ReflectionClass::__construct("UsersTablesSeeder")
the following code shows DatabaseSeeder.php
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
$this->call(UsersTablesSeeder::class);
}
}
this is my user table
<?php
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
use App\User;
class UsersTablesSeeder extends Seeder
{
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
User::create([
'name' => 'John Smith',
'email' => 'john_smith#gmail.com',
'password' => Hash::make('password'),
'remember_token' => str_random(10),
]);
}
}
I am following this link

Add namespace Database\Seeders; to your class. As said in laravel 8
Seeders and factories are now namespaced. To accommodate for these
changes, add the Database\Seeders namespace to your seeder classes. In
addition, the previous database/seeds directory should be renamed to
database/seeders:

Related

Laravel 9 - Error Class BooksSeeder does not exist

When i run php artisan make:seeder --class=BooksSeeder I am getting the following error:
The "--class" option does not exist.
Can anyone help me out?
I'm still new to Laravel
My DatabaseSeeder Class:
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* #return void
*/
public function run()
{
// \App\Models\User::factory(10)->create();
}
}
My BooksSeeder Class:
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
use App\Models\Books;
class BooksSeeder extends Seeder
{
public function run()
{
$faker = \Faker\Factory::create();
for ($i = 0; $i < 50; $i++) {
Books::create([
'name' => $faker->sentence,
'author' => $faker->name,
'publish_date' => $faker->date,
]);
}
}
}
You are using wrong syntax.
To create seeder, use:
php artisan make:seeder BooksSeeder
To run seeder, use:
php artisan db:seed --class=BooksSeeder

Laravel: Contract file is not instantiable while building Controller

I am trying to add a shopping cart function to my Laravel application. I installed darryldecode/laravelshoppingcart package from GitHub and have been following instructions in these two websites.
TECHPOOL-Create a Shopping Cart with Laravel 6
LARASHOUT-Laravel E-Commerce Application Development – Checkout
I was able to create most of the shopping cart function with the first website but it didn't cover checkouts and placing orders so I found the second website.
The problem is that the contract file is not working. Here is the error I got.
Illuminate\Contracts\Container\BindingResolutionException
Target [App\Contracts\OrderContract] is not instantiable while building [App\Http\Controllers\CheckoutController].
http://localhost:8000/checkout
Where I use the contract file is in the checkout process witch is explained in the second website. I made few changes in the codes so that it will be consistent with the first website but mostly I followed what the website says.
Here are the codes that are mentioned in the error.
OrderContract.php
<?php
namespace App\Contracts;
interface OrderContract
{
public function storeOrderDetails($params);
}
CheckoutController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Contracts\OrderContract;
use App\Http\Controllers\Controller;
class CheckoutController extends Controller
{
protected $orderRepository;
public function __construct(OrderContract $orderRepository)
{
$this->orderRepository = $orderRepository;
}
public function getCheckout()
{
return view('checkout');
}
public function placeOrder(Request $request)
{
// Before storing the order we should implement the
// request validation which I leave it to you
$order = $this->orderRepository->storeOrderDetails($request->all());
dd($order);
}
}
OrderRepository.php
<?php
namespace App\Repositories;
use Cart;
use App\Models\Order;
use App\Product;
use App\Models\OrderItem;
use App\Contracts\OrderContract;
class OrderRepository extends BaseRepository implements OrderContract
{
public function __construct(Order $model)
{
parent::__construct($model);
$this->model = $model;
}
public function storeOrderDetails($params)
{
$order = Order::create([
'order_number' => 'ORD-' . strtoupper(uniqid()),
'status' => 'pending',
'grand_total' => Cart::getSubTotal(),
'item_count' => Cart::getTotalQuantity(),
'table_number' => $params['table_number'],
'name' => $params['name'],
'notes' => $params['notes']
]);
if ($order) {
$items = Cart::getContent();
foreach ($items as $item) {
// A better way will be to bring the product id with the cart items
// you can explore the package documentation to send product id with the cart
$product = Product::where('name', $item->name)->first();
$orderItem = new OrderItem([
'product_id' => $product->id,
'quantity' => $item->quantity,
'price' => $item->getPriceSum()
]);
$order->items()->save($orderItem);
}
}
return $order;
}
}
RepositoryServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Contracts\OrderContract;
use App\Repositories\OrderRepository;
class RepositoryServiceProvider extends ServiceProvider
{
protected $repositories = [
OrderContract::class => OrderRepository::class,
];
/**
* Register services.
*
* #return void
*/
public function register()
{
foreach ($this->repositories as $interface => $implementation) {
$this->app->bind($interface, $implementation);
}
}
/**
* Bootstrap services.
*
* #return void
*/
public function boot()
{
//
}
}
I'm not really familiar with the contract concept since I only started learning Laravel recently and I'm completely lost here. Maybe the problem is that I haven't created another file that is necessary or maybe something else.
Any help would be appreciated as I have tried multiple methods with no success.
Thank you in advance.
Yes, this is expected...your contract should point to a Solid class else it's going to fail while trying to resolve it out of the container. So this is what you should do:
Create a class that implements that trait.
Go to your AppServiceProvider and bind it to that contract like this:
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
$this->app->bind(\App\Contracts\OrderContract::class, App\Repositories\ClassImplementingOrderContract::class);
}
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
//
}
}
this should fix your problem.
This error could caused even if you have forget to add "RepositoryServiceProvider" into "config/app.php" 's serviceProviders array.

Faker get streetAddress throwing ErrorException

I'm building a laravel application, and I've created a FakerServiceProvider to populate factories for testing and local dev.
<?php
namespace App\Providers;
use Faker\Factory;
use Faker\Generator;
use Faker\Provider\en_GB\Address;
use Faker\Provider\en_GB\Person;
use Faker\Provider\en_GB\PhoneNumber;
use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
/**
* Class FakerServiceProvider
* #package App\Providers
*/
class FakerServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
*
*/
public function register()
{
$this->app->singleton(Generator::class, function ($app) {
$factory = Factory::create('en_GB');
$factory->addProvider(Person::class);
$factory->addProvider(Address::class);
$factory->addProvider(PhoneNumber::class);
return $factory;
});
}
/**
* #return array
*/
public function provides()
{
return [Generator::class];
}
}
I have created an address factory:
<?php
use App\Address;
use App\Country;
$factory->define(Address::class, function (Faker\Generator $faker) {
return [
'line_1' => $faker->secondaryAddress,
'line_2' => $faker->streetAddress,
'town' => $faker->city,
'county' => $faker->county,
'country_id' => factory(Country::class)->make()->id,
'postcode' => $faker->postcode,
'phone' => $faker->phoneNumber,
];
});
When I try to use this factory I get the following error:
ErrorException: call_user_func_array() expects parameter 1 to be a valid callback, non-static method Faker\Provider\Address::streetAddress() should not be called statically
I have checked the source for the Faker library and there is a streetAddress method here
I have tried calling both $faker->streetAddress and $faker->streetAddress()with no luck. I would expect$faker->streetAddressto produce something like ` or something similar.
Can anyone shed a bit of light on this for me
Removing the added providers in the Faker Service Provider fixed the issue

Laravel: seeding tables other than Users

UPDATE: I am going to include my full file replacing the partial view I had. The seeder for the User table works, but the one for the Groups table does not. I do have those tables produced by Sentry but I only created a Model for Groups that has nothing in it other than the declaration of the class. Don't know what else to include.
<?php
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* #return void
*/
public function run()
{
Eloquent::unguard();
//User::create(array('email' => 'foo#bar.com'));
// $this->call('UserTableSeeder');
$this->command->info('User table seeded!');
}
}
class UserTableSeeder extends Seeder {
public function run()
{
User::create(array(
'username' => 'alvaro',
'permissions' =>'{"user":1}'
));
$this->command->info('User table seeded!');
}
}
class GroupTableSeeder extends Seeder {
public function run()
{
Group::create(array(
'name' => 'usuario',
'permissions' =>'{"user":1}'
));
$this->command->info('Group table seeded!');
}
}
But actually, the one I want is the Groups tables (I am on Sentry). Yes, I have created the Model for Group, as Group.php but I don't know how to define its contents. Sometimes I have seen on other occasions that it suffices with just defining the class, but here I dont know, it doesn't work that easily.
Just doing something like
class GroupTableSeeder extends Seeder
will not work as it says that such class does not exist.
The only thing I needed to do was to create a separate file having that name GroupTableSeeder.php
and include the code in there. For some reason, while UserTableSeeder can be inside a file called DatabaseSeeder and it works, it does not work for other tables.
class GroupTableSeeder extends Seeder {
public function run()
{
Group::create(array(
'name' => 'basicuser',
'permissions' =>'{"user.create" :-1,"user.delete" :-1,"user.view":1,"user.update":-1,"post.create":1}'
));
$this->command->info('Group table seeded!');
}
}

Laravel 4 DatabaseSeed.php throws class not found

I have tried so much to get this database seed to work but I still get Class 'Account' not found even though I have namespaced where I should.
There error is thrown when running php artisan db:seed on $accountOut = Account::create(array( where Account is what is throwing the error. Am stating the using incorrectly? If I were to remove all the namespacing I have no issues at all.
My Account.php file:
<?php namespace App\Models;
class Account extends \Eloquent {
/**
* The database table used by the model.
*
* #var string
*/
protected $table = 'account';
/**public function user()
{
return $this-belongsTo('User');
}*/
}
My seed file:
<?php
use App\Models;
class TransactionSeeder extends Seeder {
public function run()
{
DB::table('transaction')->delete();
DB::table('account')->delete();
$accountOut = Account::create(array(
'name' => 'Checking',
'origin' => 'Bank'
));
$accountIn = Account::create(array(
'name' => 'Stuff',
'origin' => 'Expense'
));
$adminUser = Sentry::getUserProvider()->findByLogin('admin#admin.com');
Transaction::create(array(
'account_id_in' => $accountIn->id,
'account_id_out' => $accountOut->id,
'amount' => 300.00
));
}
}
I feel really stupid but instead of calling out use App\Models you would call out use App\Models\Account and it works as it should.
Then remember to run php composer.phar dump-autoload
I've had similar issues and prefixing my classes with the namespace operator solved them.
Try \Account

Resources