如何使用 laravel 中的 Shop 模型获取 users table 的 user 的外键名称?
How can I get the foreign-key name of `user` of `users` table using `Shop` model in laravel?
shops
table
Schema::create('shops', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->boolean('is_active')->default(false);
$table->text('description')->nullable();
$table->float('rating')->nullable();
$table->unsignedBigInteger('location_id');
$table->foreign('location_id')->references('id')->on('locations')->onDelete('cascade');
$table->timestamps();
});
Here is the shops
table. And it has user_id
which is foreign key of users
table.
users
table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('photo')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->longText('cartitems')->nullable();
$table->longText('wishlist')->nullable();
$table->unsignedBigInteger('discount')->default(0);
$table->rememberToken();
$table->timestamps();
});
This is the users
table
Shop
模型(关系)
public function seller() //user --> seller
{
return $this->belongsTo(User::class, 'user_id');
}
Here I try to make a relationship between Shop
and User
DashboardController
public function shops()
{
$shops = Shop::all();
dd($shops->seller()->name);
}
Here I want to get the name of the seller
of the shop
What I am getting after run the project
BadMethodCallException
Method Illuminate\Database\Eloquent\Collection::seller does not exist.
$shops 是一个集合。它是商店模型实例的集合。和seller是Shop Model的关系。
foreach($shops as $shop){
echo $shop->seller->name;
}
shops
table
Schema::create('shops', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->boolean('is_active')->default(false);
$table->text('description')->nullable();
$table->float('rating')->nullable();
$table->unsignedBigInteger('location_id');
$table->foreign('location_id')->references('id')->on('locations')->onDelete('cascade');
$table->timestamps();
});
Here is the
shops
table. And it hasuser_id
which is foreign key ofusers
table.
users
table
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('email')->unique();
$table->string('photo')->nullable();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->longText('cartitems')->nullable();
$table->longText('wishlist')->nullable();
$table->unsignedBigInteger('discount')->default(0);
$table->rememberToken();
$table->timestamps();
});
This is the
users
table
Shop
模型(关系)
public function seller() //user --> seller
{
return $this->belongsTo(User::class, 'user_id');
}
Here I try to make a relationship between
Shop
andUser
DashboardController
public function shops()
{
$shops = Shop::all();
dd($shops->seller()->name);
}
Here I want to get the name of the
seller
of the shop
What I am getting after run the project
BadMethodCallException Method Illuminate\Database\Eloquent\Collection::seller does not exist.
$shops 是一个集合。它是商店模型实例的集合。和seller是Shop Model的关系。
foreach($shops as $shop){
echo $shop->seller->name;
}