zaster
118
1
Laravel

joins and where clause?

Posted 2 months ago by zaster

CostItem hasOne POItem

POItemsController.php

public function create(Request $request)
    {   
      
        //Get the vendor_company_id and get the related cost_items       
        $vendor_company_id = $request['vendor_company'];        

        $cost_items = CostItem::where('vendor_company_id', $vendor_company_id)                        
                                ->get();
            

        $cost_items->each(function($cost_item){
            $po_item = new POItem();
            $po_item->cost_item_id = $cost_item->id;
            $po_item->po_id = $cost_item->po_id;
            $po_item->qty = $cost_item->qty;
            $po_item->rate = $cost_item->rate;
            $po_item->description = $cost_item->description;
            $po_item->save();
        });
        
        return view('employees.poitems.create', compact('cost_items'));
    }

What i need is to get the po_item_ids related to cost_item_ids

to display something like this

poitems.create.blade.php

@foreach($po_items as $po_item)


<input type="text" class="form-control" name="vendor_name" id="vendor_name" 
value="{{$po_item->cost_item->vendor_company->name}}" readonly>   
                                         

<input type="text" class="form-control" name="item" id="item"
value="{{$po_item->description}}">

@endforeach

Please assume that all relationships are defined

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.