[英]How to use pluck to get specific data from other table in Laravel?
From SalesController
, I want to get specific name
from table Item
and passed to HTML.从
SalesController
,我想从表Item
中获取特定name
并传递给 HTML。
My controller looks like我的 controller 看起来像
public function index()
{
$items=Item::orderBy('name','ASC')->get()->pluck('name','id');
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales,'items',$items);
}
My HTML is我的 HTML 是
<thead>
<tr>
<th>No</th>
<th>Name</th>
<th>Quantity</th>
<th>Total Price</th>
<th>Detail</th>
</tr>
</thead>
<tbody>
@foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->items->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
</tr>
@endforeach
</tbody>
But I get the following error after trying to access the page:但是尝试访问该页面后出现以下错误:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\inventoryApp\resources\views\Sale\index.blade.php)
试图获取非对象的属性“名称”(查看:C:\xampp\htdocs\inventoryApp\resources\views\Sale\index.blade.php)
there are two way有两种方式
1.Using relationship 1.使用关系
class Sale extends Model
{
public function item()
{
return $this->belongsTo('App\Item','item_id','id');
}
}
so you can use like below所以你可以像下面这样使用
<td>{{ $sale->item->name }}</td>
2. Using array data 2.使用数组数据
public function index()
{
$items=Item::orderBy('name','ASC')->pluck('name','id')->toArray();
$sales=Sale::all();
return view('Sale.index')->with('sales',$sales,'items',$items);
}
<td>{{ $items[$sale->item_id] }}</td>
$items=Item::orderBy('name','ASC')->pluck('name','id');
and use $items
directly without sale
object并直接使用
$items
不sale
object
If you have relationship with table Sale.如果您与表销售有关系。
Add this function to your Sale Class.将此 function 添加到您的销售 Class。
public function item()
{
return $this->belongsTo('App\Item');
}
In your controller you can use compact在您的 controller 中,您可以使用紧凑型
public function index()
{
$items=Item::orderBy('name','ASC')->get();
$sales=Sale::all();
return view('Sale.index')->compact('sales','items');
}
And you can use Eager load to your HTML.您可以使用 Eager 加载到您的 HTML。
<tbody>
@foreach($sales as $sale)
<tr>
<td>{{ $sale->id }}</td>
<td>{{ $sale->item->name }}</td>
<td>{{ $sale->quantity }}</td>
<td>RM {{ $sale->price }}</td>
<td>{{ $sale->created_at }}</td>
</tr>
@endforeach
</tbody>
You are getting this error because you are trying to get a property of item name that does not exist on the sale object.您收到此错误是因为您试图获取销售 object 上不存在的项目名称属性。 you can simply do it by eloquent relationship as follows: On your sale model:
您可以通过 eloquent 关系简单地做到这一点,如下所示:
public function item()
{
return $this->belongsTo('App\Item','item_id');
}
On your controller:在您的 controller 上:
public function index()
{
$sales=Sale::all();
return view('Sale.index',compact('sales'));
}
Then on your blade you can easily get related Item name:然后在您的刀片上,您可以轻松获得相关的项目名称:
@foreach($sales as $sale)
<tr>
<td>{{ $sale->item->name }}</td>
</tr>
@endforeach
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.