【问题标题】:Property [costo] does not exist on this collection instance此集合实例上不存在属性 [costo]
【发布时间】:2021-09-14 03:20:31
【问题描述】:

我需要打印服务的价格,但找不到价格列。 也许我在人际关系上做错了什么,但我无法自己解决

数据库:

控制器:

  public function details(Request $request,$id){

    $datax = [
      'category_name' => 'apps',
      'page_name' => 'calendar',
      'has_scrollspy' => 0,
      'scrollspy_offset' => '',

  ];



  
    $evento = Eventos::find($id);
    $servicio = \App\Models\Eventos::select("servicio_id")->where('servicio_id', $evento->id)->get('servicio_id');
    $event = Eventos::find($id);
    $event->asistencia = $request->asistencia;
    $event->cancelado = $request->cancelado;
    $event->save();

    return view("evento",[
      "event" => $event,
      "servicio" => $servicio
    ])->with($datax);

  }

blade.php

  <div class="input-group mb-4">
  <div class="input-group-prepend">
    <span class="input-group-text">$</span>
  </div>
  <input type="text" value="{{$servicio->costo}}" class="form-control col-md-3" aria-label="Amount (to the nearest dollar)">
  <div class="input-group-append">
    <span class="input-group-text"></span>
  </div>
</div>

我需要打印与 service_id 相关的“costo”列

请帮忙

【问题讨论】:

    标签: laravel laravel-5 eloquent laravel-8


    【解决方案1】:

    您可以一对一地使用 laravel elequent 关系。 如果您在 App/Models 中有这两个 Servicio.phpEvento.php 或任何您的模型名称,请仅在以下代码中替换您的模型:

    1-App/Models/Servicio.php 中定义这种关系:

     public function evento()
        {
            return $this->hasOne(Evento::class);
        }
    

    App/Models/Evento.php 中定义这种关系:

     public function servicio()
        {
            return $this->belongsTo(Servicio::class);
        }
    

    现在在控制器中添加这个:

    $evento = Eventos::where('id' , $id)->with('servicio')->first();
    

    在刀片中使用这个:

      <input type="text" value="{{$evento->servicio->costo}}" >
    

    2- 你也可以这样做,但我建议你第一个: 仅在您的代码中更改此: $servicio = \App\Models\Servicio::where('id', $evento->servicio_id)->first();

    【讨论】:

      猜你喜欢
      • 2020-11-12
      • 2019-03-23
      • 2020-07-11
      • 2017-05-12
      • 2018-10-12
      • 2021-02-08
      • 2021-12-16
      相关资源
      最近更新 更多