Reputation: 161
the error appears when i try to run the seeds.
this is the error:
at vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/QueriesRelationships.php:49 45| $method = $this->canUseExistsForExistenceCheck($operator, $count) 46| ? 'getRelationExistenceQuery' 47| : 'getRelationExistenceCountQuery'; 48|
49| $hasQuery = $relation->{$method}( 50| $relation->getRelated()->newQueryWithoutRelationships(), $this 51| ); 52| 53| // Next we will call any given callback as an "anonymous" scope so they can get the
+3 vendor frames
4 database/factories/UserFactory.php:59 Illuminate\Database\Eloquent\Model::__callStatic()
+6 vendor frames
11 [internal]:0 Illuminate\Database\Eloquent\FactoryBuilder::Illuminate\Database\Eloquent{closure}()
this is the factory:
use App\User;
use App\Category;
use App\Product;
use App\Transaction;
use App\Seller;
use Faker\Generator as Faker;
use Illuminate\Support\Str;
$factory->define(User::class, function (Faker $faker) {
static $password;
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'email_verified_at' => now(),
'password' => $password ?: $password = bcrypt('secret'),
'remember_token' => Str::random(10),
'verified' => $verificado = $faker->randomElement([User::USUARIO_VERIFICADO, User::USUARIO_NO_VERIFICADO]),
'verification_token'=> $verificado == User::USUARIO_VERIFICADO ? null : User::generarVerificationToken(),
'admin' => $faker->randomElement([User::USUARIO_ADMINISTRADOR, User::USUARIO_REGULAR]),
];
});
$factory->define(Category::class, function (Faker $faker) {
return [
'name' => $faker->word,
'description'=> $faker->paragraph(1),
];
});
$factory->define(Product::class, function (Faker $faker) {
return [
'name' => $faker->name,
'description'=> $faker->paragraph(1),
'quantity'=> $faker->numberBetween(1, 10),
'status'=> $faker->randomElement([Product::PRODUCTO_DISPONIBLE, Product::PRODUCTO_NO_DISPONIBLE]),
'image'=> $faker->randomElement(['1.jgp', '2.jgp', '3.jgp']),
'seller_id'=> User::all()->random()->id,
];
});
$factory->define(Transaction::class, function (Faker $faker) {
$vendedores= Seller::has('products')->get()->random();
$comprador= User::all()->except($vendedores->id)->random();
return [
'quantity'=> $faker->numberBetween(1, 3),
'buyer_id'=> $comprador->id,
'product_id'=> $vendedores->products->random()->id,
];
});
this is the seed:
use Illuminate\Database\Seeder;
use App\User;
use App\Category;
use App\Product;
use App\Transaction;
public function run()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
User::truncate();
Product::truncate();
Category::truncate();
Transaction::truncate();
DB::table('category_product')->truncate();
$cantidadusuarios= 200;
$cantidadcategorias= 30;
$cantidadproductos= 1000;
$cantidadtransacciones= 1000;
factory(User:: class, $cantidadusuarios)->create();
factory(Category:: class, $cantidadcategorias)->create();
factory(Product:: class, $cantidadtransacciones)->create()->each(
function($producto)
{
$categorias= Category::all()->random(mt_rand(1, 5))->pluck('id');
$producto->categories()->attach($categorias->first());
}
);
factory(Transaction:: class, $cantidadtransacciones)->create();
}
Upvotes: 5
Views: 13567
Reputation: 149
In my case. The Relations are defined accurately. The Problem is Previously I am using with clause like this which is working fine on Laravel 9
$slug = "monthly-subscription"
$this->subscriptions()->with(['plan' => function (Builder $query) use ($slug) {
$query->whereIn('slug', $slug);
}])->where('status', Subscription::ACTIVE)->count();
but later I have to update my business logic and I have to use whereHAs so I updated my code like below snippet
$slug = "monthly-subscription"
$this->subscriptions()->whereHas(['plan' => function (Builder $query) use ($slug) {
$query->whereIn('slug', $slug);
}])->where('status', Subscription::ACTIVE)->count();
which is wrong and gave me error Call to a member function getRelationExistenceQuery() the correct syntax is this
$slug = "monthly-subscription"
$this->subscriptions()->whereHas('plan', function (Builder $query) use ($slug) {
$query->whereIn('slug', $slug);
})->where('status', Subscription::ACTIVE)->count();
Upvotes: 2
Reputation: 775
This error occurs mainly because you use relation in Eloquent which don't exists
Upvotes: 0
Reputation: 1659
In my case, because I am apparently not very smart, I got this error because in my relationship function I just did $this->belongsTo(TheModel::class);
and forgot to actually RETURN it.
Upvotes: 25
Reputation: 131
I think we are doing the same course in Udemy. I encountered this problem and in my case problem was in Seller Model, indeed this problem occurs when some Model relationships is not same as database level (in your migration). So check Seller model and make sure you have
public function products() {
return $this->hasMany(Product::class);
}
inside it and other model too.
Upvotes: 2