【发布时间】:2020-12-03 20:02:31
【问题描述】:
我是 Laravel 的新手,这是我的第一个应用程序。我有 2 张桌子:
艺术
-id
-code
-designation
-prix
-qte
-gamme_id
游戏
-id
-type
他们之间有关系
我想按“代码”搜索文章
我尝试将<td>{{ $row->gamme->type }}</td> 替换为<td>{{ $row->gamme_id}}</td>
它可以工作,但我不想看到游戏的 id:
【问题讨论】:
-
欢迎来到SO ...请不要放代码图片,代码是文本,将其作为代码块中的文本而不是图像添加到问题中
-
你没有在那个查询中使用你的模型你正在使用查询生成器,所以没有关系也没有返回模型,查询生成器返回
stdClass对象 -
这是我的游戏迁移 public function up() { Schema::create('gammes', function (Blueprint $table) { $table->bigIncrements('id'); $table->时间戳(); $table->string('type'); }); }
-
lagbox x 我需要做什么?
-
您的模型和迁移看起来不错,但您可能希望在检索艺术数据时获得 Art 和 Gamme 之间的关系。请查看 Eager Loading 文档
https://laravel.com/docs/7.x/eloquent-relationships#eager-loading。像Art::where('code', 'like', '%'.$search.'%')->with('gamme')->paginate(5);这样的东西应该可以解决问题。