【问题标题】:Why am i getting all my products and not only one in my view?为什么我得到了我所有的产品,而不仅仅是我认为的一种?
【发布时间】:2022-01-10 08:08:31
【问题描述】:

在我的详细票证视图中,我想显示已加载产品的所有数据及其所有字段,但是当我想查看详细信息时,我得到了我的所有产品,而不仅仅是属于该票证的一个产品。

以下是我存储门票的方式:

 public function store(Request $request){
        /*dd($request->all());*/
        $ticket = new Ticket();
        

        if($request->file('file')){
            $file = $request->file('file');
            $filename = time().'.'.$file->getClientOriginalName();
            $request->file->move('storage/', $filename);
            $ticket->file = $filename;
        }
        
        /*$ticket->cuenta_id = $request->cuenta_id;*/
        $ticket->contact_id = $request->contact_id;
        $ticket->statusTicket_id = $request->statusTicket_id;
        $ticket->typeTicket_id = $request->typeTicket_id;
        $ticket->idOwner = Auth::user()->id;        
        
        $ticket->product_id = $request->product_id;
        $ticket->serial_number = $request->serial_number;
        $ticket->accesories = $request->accesories;
                                
        $ticket->entry = Carbon::parse ( $request->entry)->toDateString();
        $ticket->deliver = Carbon::parse ($request->deliver)->toDateString();       
               
      
                    
        $ticket->save();            
                        
          

        Session::flash('success');
        return redirect()->route('tickets.view');
    }

这是我的详细方法:

public function detail($id){
        $detailData = Ticket::find($id);
        $detailData['contacts'] = Contact::all();
        $detailData['products'] = Product::all();
        $detailData['ptypes'] = Ptype::all();
        $detailData['brands'] = Brand::all();
        $detailData['models'] = ModelP::all();

        return view('backend.ticket.detail-ticket', compact('detailData'));
    }

所以在我看来,我将我的产品传递如下:

@foreach($detailData['products'] as $product)
       <div class="form-group col-md-3">
          <label for="product_id">Producto</label>
          <input type="text" name="product_id" value="{{$product->ptype->productType . ", " . $product->marca->brandName . " " . $product->modelo->modelName}}" class="form-control" readonly>
                                </div>

@endforeach

这是它如何为我带来数据的图像:

应该只有一种产品,但我有我所有的产品列表。

知道在将数据传递到我的视图时我做错了什么吗?

【问题讨论】:

    标签: laravel laravel-7


    【解决方案1】:

    在这一行:

     $detailData['products'] = Product::all();
    

    您正在加载所有产品,而不仅仅是想要的产品...

    应该是:

     $detailData['products'] = Product::find($detailData->product_id);
    

    【讨论】:

    • 但现在我发现另一个错误,Trying to get property 'ptype' of non-object
    • 因为不需要@foreach($detailData['products'] as $product)
    • $detailData['products'] 将只有一条记录
    • 删除 foreach 并像这样传递它我仍然有同样的错误。 value="{{$detailData-&gt;ptype-&gt;productType . ", " . $detailData-&gt;marca-&gt;brandName . " " . $detailData-&gt;modelo-&gt;modelName}}"
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-27
    • 2019-06-07
    相关资源
    最近更新 更多