Показать 2 таблицы в 1 виде laravel - PullRequest
1 голос
/ 03 июня 2019

Я пытаюсь отобразить 2 переменные в одном представлении

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

но работает только $ match, в то время как $ distance возвращает ноль, когда I dd (); это.

Я пытался сделать это, но $ расстояния по-прежнему дает мне ноль

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

Моя модель BikeGame:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BikeGame extends Model
{
   protected $guarded = [];

   public function distances(){
    return $this->hasMany('App\Distance');   
   }

}

Моя модель расстояния:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Distance extends Model
{
    protected $guarded = [];

    public function bikegames(){
        return $this->belongsTo('App\BikeGame');
    }

}

Мой контроллер:

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

Я хочу, чтобы мой вид отображал это

<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>

1 Ответ

0 голосов
/ 03 июня 2019

попробуйте использовать отношение, которое вы определили в модели.

В вашем контроллере:

public function bikeGamesMatch(Request $request) 
{ 
    $match = BikeGame::find($request->id); // note is just one object

    return view('bike_games.match', compact("match"));
}

и в блейде

<th scope="col" class="text-center">
    <strong>Game Title:</strong>
    {{ $match->title }}
</th>

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

РЕДАКТИРОВАТЬ

Модель BikeGame :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class BikeGame extends Model
{
   protected $guarded = [];

   public function distance(){
    return $this->belongsTo('App\Distance', 'distance_id');   
   }

}

Модель расстояния :

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Distance extends Model
{
    protected $guarded = [];

    public function bikegames(){
        return $this->hasMany('App\BikeGame'); // this could be hasOne, depending on if you are going to use the same distance for other games or not
    }

}

Контроллер:

public function bikeGamesMatch(Request $request){ 
    $match = BikeGame::find($request->id);

    return view('bike_games.match', compact("match"));
}

Просмотр bike_games/match.blade.php

<th scope="col" class="text-center"><strong>Game Title:</strong>
    {{ $match->title }}
</th>

<th scope="col" class="text-center"><strong>Target: Distance</strong>
    @isset($match->distance)
    {{ $match->distance->distance }} {{ $match->distance->unit }}
    @endisset
</th>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...