Summer Sale! All accounts are 50% off this week.

raloseq's avatar

You requested 1 items, but there are only 0 items available.

I have problem for example this test returns You requested 1 items, but there are only 0 items available. I was trying to dd($car) but i dont see anything in console what looks like dd of variable.

	public function test_car_details_is_rendering_properly()
    {
        $user = User::factory()->create();
        $this->actingAs($user);
        $car = Car::factory()->create();

        $response = $this->get("/cars/$car->id");

        $response->assertStatus(200);
    }
0 likes
15 replies
raloseq's avatar

@Sinnbeck

public function show(Car $car)
    {
        Gate::authorize('view', $car);

        $orders = ServiceOrders::with('cars')->get();
        $doneOrders = [];

        foreach($orders as $order) {
            if(($order->is_done === 1) && ($car->id === $order->car_id)) {
                $doneOrders[] = $order;
            }
        }

        return view('cars.show', [
            'car' => $car,
            'doneOrders' => $doneOrders
        ]);
    }
Sinnbeck's avatar

@raloseq What part of your code is giving that error "You requested 1 items, but there are only 0 items available"

raloseq's avatar

@Sinnbeck hard to say im just doing php artisan test

• Tests\Feature\CarTest > car details is rendering properly
   PHPUnit\Framework\ExceptionWrapper 

  You requested 1 items, but there are only 0 items available.

  at C:\xampp\htdocs\inzynierka\vendor\laravel\framework\src\Illuminate\Collections\Arr.php:632
    628▕
    629▕         $count = count($array);
    630▕
    631▕         if ($requested > $count) {
  ➜ 632▕             throw new InvalidArgumentException(
    633▕                 "You requested {$requested} items, but there are only {$count} items available."
    634▕             );
    635▕         }
    636▕

  1   C:\xampp\htdocs\inzynierka\vendor\phpunit\phpunit\phpunit:98
      PHPUnit\TextUI\Command::main()
raloseq's avatar

@tykus What do you mean by rest of the stacktrace? I don't see anything more in console.

tykus's avatar

@raloseq you mean there is nothing after?

  1   C:\xampp\htdocs\inzynierka\vendor\phpunit\phpunit\phpunit:98
      PHPUnit\TextUI\Command::main()
raloseq's avatar

@tykus Oh this is the same error but for another test

• Tests\Feature\CarTest > car update form is rendering properly
   PHPUnit\Framework\ExceptionWrapper 

  You requested 1 items, but there are only 0 items available.

  at C:\xampp\htdocs\inzynierka\vendor\laravel\framework\src\Illuminate\Collections\Arr.php:632
    628▕
    629▕         $count = count($array);
    630▕
    631▕         if ($requested > $count) {
  ➜ 632▕             throw new InvalidArgumentException(
    633▕                 "You requested {$requested} items, but there are only {$count} items available."
    634▕             );
    635▕         }
    636▕

  1   C:\xampp\htdocs\inzynierka\vendor\phpunit\phpunit\phpunit:98
      PHPUnit\TextUI\Command::main()
raloseq's avatar

@Sinnbeck yes. i'm using

class CarFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */

    public function definition()
    {
        return [
            'user_id' => User::all()->random()->id,
            'type' => Type::all()->random()->name,
            'model' => CarModel::all()->random()->name,
            'VIN' => $this->faker->bothify('#################'),
            'registration_number' => $this->faker->bothify('#######'),
            'year' => $this->faker->date(),
        ];
    }
}
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

@raloseq So either Type or CarModel is empty (the tables), so you cannot get a random item. Create at least one of each before the factory for Car is run

1 like
tykus's avatar

@raloseq why are you using name properties of Type and Model on the Car model; why are you not using ID foreign keys?

tykus's avatar

@raloseq you do have relationships, you just have not defined them!

Also, fetching all records is not particularly efficient; when you only need one, fetch only one:

'type' => Type::inRandomOrder()->first()?->name,
'model' => CarModel::inRandomOrder()->first()?->name,
1 like
raloseq's avatar

@tykus I have DatabaseSeeder like this

$this->call([
            TypeSeeder::class,
            CarModelSeeder::class,
            UserSeeder::class,
            EmployeeSeeder::class,
            ClientSeeder::class,
            CarsSeeder::class,
            ServiceOrdersSeeder::class
        ]);

and if i do

'type' => Type::inRandomOrder()->first()?->name,
'model' => CarModel::inRandomOrder()->first()?->name,

it still gives me nothing

Please or to participate in this conversation.