Laravel:使用外键从表中获取数据

时间:2019-06-10 13:19:41

标签: php laravel

我正在使用路由从视图A向视图B发送外键

Route::post('/ultm',  function(){
    $v = Input::get('v');
    if($v != ""){
        $movimentacoes = Movimentacoes::where('id_coop', '=' . $v)
                                        ->get();
            if(count($movimentacoes) > 0)
                return view ('movs.index')->withDetails($movimentacoes)->withQuery($v);
    }
    return view ('movs.index')->withMessage("No data found");

});

这是视图A中负责发送外键的表单

    <form action="/ultm" method="POST">
        @csrf
          <div class="input-group">
            <input type="hidden" class="form-control" name="v" value={{$cooperado->id}}>
              <button type="submit" class="btn btn-primary">
                <span>Ultimas movimentações</span>
              </button>
              </span>
          </div>
      </form>

所以我试图在视图B上显示该特定键的所有记录

@if(isset($details))
        @foreach($details as $mov)
        <tr>
            <td>{{$mov->idMov}}</td>
            <td>{{$mov->id_coop}}</td>
            <td>{{$mov->valor}}</td>
            <td>{{$mov->created_at}}</td>
            <!--<td><a href="{{ route('movs.edit',$mov->idMov)}}" class="btn btn-primary">Edit</a></td>
            <td>
                <form action="{{ route('movs.destroy', $mov->idMov)}}" method="post">
                  @csrf
                  @method('DELETE')
                  <button class="btn btn-danger" type="submit">Delete</button>
                </form>-->
            </td>
        </tr>
  @endforeach
 @endif

$mov->id_coop$cooperado->id的外键,我不知道为什么看到B视图正在获取外键还是不起作用

1 个答案:

答案 0 :(得分:1)

您应该做的是:

Route::post('/ultm',  function(){
    $v = Input::get('v');
    if($v != ""){
        $movimentacoes = Movimentacoes::where('id_coop', '=' . $v)
                                        ->get();
            if(count($movimentacoes) > 0)
                $data['details']=$movimentacoes;
                return view ('movs.index',$data)->withDetails($movimentacoes)->withQuery($v);
    }
    return view ('movs.index')->withMessage("No data found");

});