【问题标题】:problem with id does not show the correct and repeatsid 问题不显示正确并重复
【发布时间】:2021-10-26 02:52:26
【问题描述】:

我的在线轮班申请有问题:

用户可以查看他们的轮班请求。问题是点击视图(...)总是重定向到两个寄存器中的移位ID 93

控制器:

public function inicio() {
    if (Auth::guest()) {
      return redirect()->route("login");
    }

    $config = Configuraciones::findOrFail(0);

    // Solicitudes = REQUEST

    $solicitudes = Solicitudes::where('user_id', Auth::User()->id)->orderByDesc('created_at')->take(5)->get();

    // GET THE ID OF THE REQUEST FROM THE LOGED USER
    $solicitud = Solicitudes::where('user_id', Auth::User()->id)->first();

    // GET THE SHIFT 'id' RELATED TO THE 'id' OF THE request
    $eventos = Eventos::where('solicitud_id', $solicitud->id)->first();


    return view('solicitudes.inicio', compact('solicitudes', 'config', 'eventos'))->with($data);
}

刀片:

@foreach($solicitudes as $solicitud)
    @if(Auth::user()->id == $solicitud->user_id)
        <tr>
            <td>{{ $solicitud->fecha }}</td>
            <td>{{ $solicitud->hora }}</td>
            <td class="text-center">
                @if($solicitud->estado == '0')
                    <span class="badge badge-primary">Pendiente</span>
                @elseif($solicitud->estado == '1')
                    <span class="badge badge-success">Aceptada</span>
                @elseif($solicitud->estado == '2')
                    <span class="badge badge-danger">Rechazada</span>
                @endif
            </td>
            <td class="text-center">
                <div class="dropdown custom-dropdown">
                    <a class="dropdown-toggle" href="#" role="button" id="dropdownMenuLink1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-more-horizontal">
                            <circle cx="12" cy="12" r="1"></circle>
                            <circle cx="19" cy="12" r="1"></circle>
                            <circle cx="5" cy="12" r="1"></circle>
                        </svg>
                    </a>
                    <div class="dropdown-menu" aria-labelledby="dropdownMenuLink1">
                        <a class="dropdown-item" href="/turno/{{ $eventos->id }}">Ver</a>
                    </div>
                </div>
            </td>
        </tr>
    @endif
@endforeach

尝试使用:$eventos = Eventos::where('solicitud_id', $solicitud-&gt;id)-&gt;get();,但报错:

此集合实例上不存在属性 [id]。

错误:

我能做些什么来获得正确的班次 ID 并且同一件事不会总是重复?

【问题讨论】:

  • $eventos 是一个集合。您需要遍历集合以访问其属性。错误可能来自这一行:&lt;a class="dropdown-item" href="/turno/{{$eventos-&gt;id}}"&gt;Ver&lt;/a&gt;
  • 是的,在那一行是'id'的错误。我该如何通过它?
  • 循环播放。 foreach。如果只有一条记录属于请求 solcitud->id,您可以将get() 更改为first(),您可以继续使用$eventos-&gt;id,但请确保处理空情况; optional($eventos)-&gt;id.
  • 这没有修复错误? (指你刚刚删除的评论)
  • 是的,它修复了它,但它仍然只显示'id' 93

标签: php laravel laravel-5 laravel-blade laravel-8


【解决方案1】:

正如 cmets 中强调的那样,首先修复你的关系方法。

//Solicitudes model
public function eventos(){
  return $this->hasMany(Eventos::class, 'solicitud_id');
}

现在,当您通过执行此操作查询Solicitudes 时可以包含所有事件(注意with('eventos') 的使用:

 $solicitudes = Solicitudes::with('eventos')->where('user_id', Auth::User()->id)->orderByDesc('created_at')->take(5)->get();

在你的刀片中:

@foreach($solicitudes as $solicitud)
    //@if(Auth::user()->id == $solicitud->user_id) => this is redundant since you are already getting solicitudes belonging to the authenticated user so it would be okay to remove
    {{ $solicitud->fecha }}
    @foreach($solictud->eventos as $evento)
       <a class="dropdown-item" href="/turno/{{ $evento->id }}">Ver</a>
    @endforeach
@endforeach

这里的想法是每个孤独都会加载其相关事件。 当您有适当的关系时,您可以使用with 轻松获取相关模型。更多信息在这里:https://laravel.com/docs/8.x/eloquent-relationships

请注意,我没有检查其他方法,但我相信这应该可以解决您突出显示的主要错误或为您指明正确的方向。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 2018-08-25
    • 2022-01-04
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2017-10-18
    相关资源
    最近更新 更多