简体   繁体   English

如何使用 laravel 中的 `Shop` model 获取 `users` 表的 `user` 的外键名称?

[英]How can I get the foreign-key name of `user` of `users` table using `Shop` model in laravel?

shops table shops

         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.这是shops的桌子。 And it has user_id which is foreign key of users table.它有user_id ,它是users表的外键。

users table users

         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这是users

Shop model (relatonship) Shop model(关系)

public function seller()    //user --> seller
{
        return $this->belongsTo(User::class, 'user_id');
}

Here I try to make a relationship between Shop and User在这里,我尝试在ShopUser之间建立关系

DashboardController

public function shops()
{
    $shops = Shop::all();

    dd($shops->seller()->name);
}

Here I want to get the name of the seller of the shop这里我要获取店铺seller的名字

What I am getting after run the project

BadMethodCallException Method Illuminate\Database\Eloquent\Collection::seller does not exist. BadMethodCallException 方法 Illuminate\Database\Eloquent\Collection::seller 不存在。

$shops is a collection. $shops 是一个集合。 It's a collection of instances of Shop Model.它是 Shop Model 实例的集合。 And seller is relation of Shop Model.卖家是Shop Model的关系。

foreach($shops as $shop){
    echo $shop->seller->name;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Laravel获取属于外键的用户名? - How to get the name of user that belongs to foreign key using Laravel? 我如何能够使用带有 eloquent 和 laravel 的嵌套预加载获取另一个表中的外键名称 - How would I be able to get the foreign key name in another table using nested eager loading with eloquent and laravel 如何使用laravel雄辩获取已加载关系的外键名称 - How to get foreign key name of loaded relationship using laravel eloquent 我如何在控制器内的 Laravel 的不同表中获取自动递增 id 的值作为外键 - how can i get the value of an an auto incremented id as a foreign key in a different table in Laravel inside a controller 使用ORM在不在数据库中的表上的外键 - foreign-key on a table not in the database, with ORM : 1005 无法创建表 `shop`.`role_user` (errno: 150 “外键约束格式不正确”)") - : 1005 Can't create table `shop`.`role_user` (errno: 150 “Foreign key constraint is incorrectly formed”)") 为什么教义会自动生成n:1关系? 为什么我得到外键引用表的所有实体,而不是单个引用行? - Why Doctrine generates automaticly n:1 Relationships? Why i get all entities of a foreign-key refrenced table instead of the single refrenced row? laravel迁移中如何修改外键引用表名 - How to change foreign key reference table name in laravel migration 如何通过使用laravel 5 eloquent模型获得第三表基础上的第一个表记录 - How can i get First table records on base of third table by using laravel 5 eloquent model 我如何在 Laravel 中修复此错误”1005 无法创建表 `englishcollage`.`role_user`(错误号:150“外键约束的格式不正确” - how can i fix this eror in laravel" 1005 Can't create table `englishcollage`.`role_user` (errno: 150 "Foreign key constraint is incorrectly formed"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM