Search code examples
laravelfunctionlaravel-5modelmutators

How to Get Data From Model in Function With Laravel


Is it possible to get data from the model in a function? I want to get the SupplierID data from the Product model.

public function productAjax($id)
{
    $product = new Producttext();
    $products = $product->productexts($id);
    $hitung_products = $products->count();

    $suplierproduct = Company::select('id', 'CompanyName')
        ->where(['id' => $products->SupplierID])
        ->first();
}

However, on execution, I get the following error.

Property [SupplierID] does not exist on this collection instance.


Solution

  • If you have producttext ID then

    $product = Producttext::find($id)
    

    OR

    $product = Producttext::where('id',$id)->first();
    
    
    //test and check that you have it $product->SupplierID
    
    $suplierproduct = Company::select('id', 'CompanyName')->where('id',$product->SupplierID)
    ->first();