如何使用laravel 5.2访问带有json值的数组?

时间:2022-09-30 14:22:49

I am having a difficulty accessing the array with json values in it. The values came from another table where I have established a relationship with another table. However, I cannot figure out how to access it correctly. Here is my code.

我在访问具有json值的数组时遇到了困难。值来自另一个表,我已经与另一个表建立了关系。但是,我无法弄清楚如何正确访问它。这是我的代码。

Model relationship between product and inventory.

产品和库存之间的模型关系。

Product

 public function inventory(){
    return $this->hasMany('App\InventoryRecord\InventoryRecord');
}

Inventory

public function products(){
     return $this->belongsTo('App\Product\Product','product_id');
}

View

 @foreach($products as $val)

    <?php //$quantity[$i++] = $val->id; ?>

       <tr class="tbl-prod-row">
         <td><input type='checkbox' style='width:30px; height:20px;' class='radio_check_all prod-id-checkbox' id='radio_check_all prod-id-checkbox' value="{{ $val->id }}"></td>
         <td style="display:none;">{{ $val->id }}</td>
         <td style="display:none;">{{ $val->category }}</td>
         <td>{{ $val->pharmaceutical }}</td>
         <td>{{ $val->description }}</td>
         <td>{{ $val->type }}</td>
         <td>{{ $val->unit }}</td>
         <td>{{ $val->price }}</td>
         <td>{{ $val->created_at }}</td>
         <td>{{ $val->po_status }}</td>
         <td>{{ $val->inventory }}</td>

        </tr>

    @endforeach

如何使用laravel 5.2访问带有json值的数组?

There is no error shown here but everytime I wanted to access some values in the array by changing the $val->inventory to $val->inventory->quantity, it return an error.

这里没有显示错误,但每次我想通过将$ val-> inventory更改为$ val-> inventory-> quantity来访问数组中的某些值时,都会返回错误。

如何使用laravel 5.2访问带有json值的数组?

Please help me with this. Thanks a lot.

请帮我解决一下这个。非常感谢。

1 个解决方案

#1


1  

That happen because product has many inventory as defined in product model :

发生这种情况是因为产品具有产品型号中定义的许多库存:

$this->hasMany('App\InventoryRecord\InventoryRecord');

So when you call $val->inventory it will return a collection of inventories as described in the error returned in log Undefined property ../Collection::$quantity because you're trying to get attribute from collection.

因此,当您调用$ val-> inventory时,它将返回一个库存集合,如日志未定义属性../Collection::$quantity中返回的错误中所述,因为您正在尝试从集合中获取属性。

You have to specify the inventory you want to get the quentity from, e.g :

您必须指定要从中获取遗产的库存,例如:

$val->inventory->first()->quantity
$val->inventory[0]->quantity

Or you could define new method in the model that return the total of product inventory quantities, e.g :

或者您可以在模型中定义返回产品库存总量的新方法,例如:

public function inventoriesQuantity(){
   $quantite = 0

   foreach($this->inventory as $inventory){
       $quantity += $inventory->quantity;
   }

   return $quantite;
}

Then call it instead of $val->inventory :

然后调用它而不是$ val-> inventory:

<td>{{ $val->inventoriesQuantity() }}</td>

NOTE : function inventory() in product model should be inventories() because it will return a collection of inventories.

注意:产品模型中的功能库存()应该是库存(),因为它将返回库存集合。

Hope this helps.

希望这可以帮助。

#1


1  

That happen because product has many inventory as defined in product model :

发生这种情况是因为产品具有产品型号中定义的许多库存:

$this->hasMany('App\InventoryRecord\InventoryRecord');

So when you call $val->inventory it will return a collection of inventories as described in the error returned in log Undefined property ../Collection::$quantity because you're trying to get attribute from collection.

因此,当您调用$ val-> inventory时,它将返回一个库存集合,如日志未定义属性../Collection::$quantity中返回的错误中所述,因为您正在尝试从集合中获取属性。

You have to specify the inventory you want to get the quentity from, e.g :

您必须指定要从中获取遗产的库存,例如:

$val->inventory->first()->quantity
$val->inventory[0]->quantity

Or you could define new method in the model that return the total of product inventory quantities, e.g :

或者您可以在模型中定义返回产品库存总量的新方法,例如:

public function inventoriesQuantity(){
   $quantite = 0

   foreach($this->inventory as $inventory){
       $quantity += $inventory->quantity;
   }

   return $quantite;
}

Then call it instead of $val->inventory :

然后调用它而不是$ val-> inventory:

<td>{{ $val->inventoriesQuantity() }}</td>

NOTE : function inventory() in product model should be inventories() because it will return a collection of inventories.

注意:产品模型中的功能库存()应该是库存(),因为它将返回库存集合。

Hope this helps.

希望这可以帮助。