【问题标题】:how to display a single result ? Lumen如何显示单个结果?流明
【发布时间】:2021-09-28 09:12:38
【问题描述】:

我无法显示在我的 show 函数中调用的项目。 有谁知道为什么什么都没有显示?

路线:

$router->get('/contrats/{id}', [
    'as' => 'contrats.show', 'uses' => 'ContratController@show'
]);

我的公共函数 show() :

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show(Contrat $contrat)
{
    return view('show', compact('contrat'));
}

视图部分:

<p><strong>Contrat n° :</strong> {{ $contrat->id }}</p>
<hr>
<p><strong>Type d'énergie :</strong> {{ $contrat->enrgy }}</p>
<hr>
<p><strong>Numéro gsrn :</strong> {{ $contrat->gsrn }}</p>
<hr>
<p><strong>Durée du contrat : {{ $contrat->duration }} mois</strong></p>
<hr>
<p><strong>Code promotionnel utilisé :</strong> {{ $contrat->codePromo }}</p>

the Result

【问题讨论】:

  • 你能分享 dd 的输出吗?{{dd( $contrat-&gt;gsrn)}}

标签: php laravel lumen


【解决方案1】:

我认为路线应该是

$router->get('/contrats/{contrat}', [...])

让路由模型绑定工作。


另一种可能是手动查询模型。

$router->get('/contrats/{id}', [...])
public function show($id)
{
    $contrat = Contrat::findOrFail($id);

    return view('show', compact('contrat'));    
}

这似乎是the official Lumen framework documentation中使用的方法

【讨论】:

  • 如果像这样的更改我有这个错误:传递给 App\Http\Controllers\ContratController::show() 的参数 1 必须是 App\Models\Contrat 的实例,给定字符串,调用C:\Users\Cédric\Desktop\lumen\lumen_REST\client\vendor\illuminate\container\BoundMethod.php 在第 36 行
  • 我的错。我认为这只适用于laravel。我在 lumen 文档中没有看到任何关于路由模型绑定的信息。也许您需要在控制器内部的 show 方法中手动进行查询。
  • 我添加了解决方案的替代方案。这对你有用吗?
猜你喜欢
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 2019-08-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-14
相关资源
最近更新 更多