【问题标题】:Display 2 tables in 1 view在 1 个视图中显示 2 个表格
【发布时间】:2019-06-07 20:15:54
【问题描述】:

我想在 1 个视图中显示 2 个表格

像这样:

{{ $match->title }}
{{ $distance->distance }}

但我无法让它工作。

这是我的控制器:

public function bikeGamesMatch(Request $request){ 
    $matches = BikeGame::find($request->id);
    $distances = Distance::find($request->id);
    dd($matches);                          
    return view('bike_games.match', compact("matches","distances"));
}

为什么当我dd()$distances又回到null:它?

这是我的看法:

<th scope="col" class="text-center"><strong>Game Title:</strong>
             @foreach($matches as $match)
             {{ $match->title }}
             @endforeach
              </th>
                <th scope="col" class="text-center"><strong>Target: Distance</strong>
              @foreach($distances as $distance) 
             {{ $distance->distance }} {{ $distance->unit }}
             @endforeach
</th>

如果我这样做,视图会给我 404

$matches = BikeGame::find($request->id)->firstOrFail();
$distances = Distance::find($request->id)->firstOrFail();

我在这里做错了什么?

【问题讨论】:

  • 您确定要发送的 id 记录存在于表 Distance 中吗?
  • 这不是答案。但是从这个角度来看,bike_game 似乎有一个相对的距离。然后你应该使用 Laravel 关系。变量名是复数,你得到一个对象。您正在搜索的一个查询参数在它们的主键中找到两个对象。想象你在做什么真的很复杂。你的同事不会对你满意。
  • 不要在视图中使用循环。您从 Find 或 First 或 firstOrFail 函数中获取单个对象。
  • 我不会在视野中看到?

标签: laravel


【解决方案1】:

尝试改变:

$matches = BikeGame::find($request->id);
$distances = Distance::find($request->id);

$matches = BikeGame::find($request->get('id'));
$distances = Distance::find($request->get('id');

【讨论】:

  • “试图获取非对象的属性 'id'(查看:C:\xampp\htdocs\bike_sprint\resources\views\bike_games\match.blade.php)”我收到此错误
  • 当我评论导致错误的行时( Leave Room ) 我收到此错误“为 foreach() 提供的参数无效(查看:C:\xampp\htdocs\bike_sprint\resources\views\bike_games\ match.blade.php)" 即使我的 foreach 看起来像这样 ``` @foreach($matches as $match) {{ $match->title }} @endforeach ``` 和这个 ``` @foreach($distances as $distance) {{ $distance->distance }} {{ $distance->unit }} @endforeach ``
【解决方案2】:

在查看之前使用foreach试试检查

isset($matches) && count(matches) >0

如果此条件为真,则使用foreach

【讨论】:

  • 使用未定义的常量匹配 - 假定为“匹配”(这将在 PHP 的未来版本中引发错误)(查看:C:\xampp\htdocs\bike_sprint\resources\views\bike_games\match .blade.php) 我得到了这个错误,在改变'matches'和'distances'之后我得到了这个错误count(): Parameter must be an array or an object that implements Countable (View: C:\xampp\htdocs\bike_sprint \resources\views\bike_games\match.blade.php)
【解决方案3】:

试试这个改变

$matches = BikeGame::where('id',$request->id)->first();
$distances = Distance::where('id',$request->id)->first();

希望对你有用。

【讨论】:

  • bike_game_id distance_id 这是哪里?我的数据库中没有这样的表,两个表上只有“id”
  • @Hacki 检查我更新的答案。我写了作为例子。
【解决方案4】:

use 在两个模型之间有一种关系 :: BikeGame & Distance 。

然后给出唯一ID。比如 Bike_id 和 Distance_id。

Bikegame 模型 =>

public function distance(){
    return $this->hasOne('App\Distance','Distance_id', 'id');
}

距离模型 =>

public function bike()
{
    return $this->belongsTO('App\Bikegame', 'Distance_id','id');
}

在你的控制器前。

 $bike = BikeGame::with('distance')->find($id);
 $bike->bikename    = request('bikename');
 $bike->distance->distacekm = $request->distacekm;
 $bike->distance->dismm = $request->dismm; 

试试这一步

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-05
    • 2011-01-15
    • 2015-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多