Php 在其他字段中使用Laravel 5.8计数数据,而不仅仅是unsignedInteger
嗨,我是拉威尔的新朋友 我有两个数据库表(框和项),即hasMany()关系。我正试图让laravel显示boxbarcode列的4个结果,而不是您在屏幕截图中看到的BoxID列的5个结果。基本上,属于TRTB0001框的所有项目 问题是它查看的是框id(1,1,1,1,1),而不是框条形码(TRTB0001)。如何调整模型、控制器和视图以显示此信息?请参阅下面的代码 Box.php(模型) Item.php(模型) boxescocontroller.php(控制器) show.blade.phpPhp 在其他字段中使用Laravel 5.8计数数据,而不仅仅是unsignedInteger,php,mysql,laravel-5,Php,Mysql,Laravel 5,嗨,我是拉威尔的新朋友 我有两个数据库表(框和项),即hasMany()关系。我正试图让laravel显示boxbarcode列的4个结果,而不是您在屏幕截图中看到的BoxID列的5个结果。基本上,属于TRTB0001框的所有项目 问题是它查看的是框id(1,1,1,1,1),而不是框条形码(TRTB0001)。如何调整模型、控制器和视图以显示此信息?请参阅下面的代码 Box.php(模型) Item.php(模型) boxescocontroller.php(控制器) show.blade.p
@extends('layout')
@section('title', 'Show Box')
@section('content')
<h1 class="title">{{ $box->box_barcode }}</h1>
<p> <a href="/projects/{{$box->id}}/edit">Edit Box</a></p>
<h3 class="content">Status: {{ $box->box_quality }}</h3>
<hr>
<h5 class="content">List of Box Items:</h5>
<!-- ONLY SHOW TASK <DIV> IF A TASK EXISTS -->
@if ($box->items->count())
<div>
@foreach ($box->items as $item)
<div>
<form method="POST" action="/items/{{ $item->id }}">
@method('PATCH')
@csrf
<label class="checkbox {{ $item->in ? 'is-complete' : '' }}" for="in" >
<input type="checkbox" name="in" onChange="this.form.submit()" {{ $item->in ? 'checked' : '' }}>
{{ $item->box_id . ' ' . $item->item_barcode . ' ' . $item->boxbarcode }}
</label>
</form>
</div>
@endforeach
</div>
@endif
@endsection
@extends('layout'))
@节(“标题”、“显示框”)
@节(“内容”)
{{$box->box\u条形码}
状态:{{$box->box\u质量}
方框项目清单:
@如果($box->items->count())
@foreach($box->items as$item)
@方法('补丁')
@csrf
在哪儿?”选中“:”}}>
{{$item->box_-id.'.$item->item_-barcode.'.$item->boxbarcode}
@endforeach
@恩迪夫
@端部
您可以在根据如下要求检索特定关系时使用子句:
public function show($boxId)
{
$necessaryItemCheck = ['TRTB0001', 'TRTB0005'];
//retriving related items
$box = Box::with(['items' => function($itemQuery) use($necessaryItemCheck){
//filtering items having boxBarCode required
$itemQuery->whereIn('boxBarCode', $necessaryItemCheck);
}])->where('id', $boxId)->first();
return view('boxes.show', compact('box'));
}
它将返回包含boxBarCode等于TRTB0001的项目的框
您可以根据需要简单地使用where或whereNotIn
编辑1
如果您希望根据用户单击动态显示项目,则需要在href标记内传递boxBarCode,如:
//boxBarCode should be retrieved from database
<a href="box/1?boxBarCode=TRTB0002">
如果我需要避免其他箱码(如TRTB0002)出现此问题,我将如何调整此功能?我使用TRTB0002得到错误的结果。我可能应该提到,该系统的工作原理是单击页面中的超链接(例如TRTB0002链接之前),该链接仅显示属于它的项目条形码。etc TRTB0003当有人单击其框链接时,仅显示属于它的项目的条形码。我给出了答案
@extends('layout')
@section('title', 'Show Box')
@section('content')
<h1 class="title">{{ $box->box_barcode }}</h1>
<p> <a href="/projects/{{$box->id}}/edit">Edit Box</a></p>
<h3 class="content">Status: {{ $box->box_quality }}</h3>
<hr>
<h5 class="content">List of Box Items:</h5>
<!-- ONLY SHOW TASK <DIV> IF A TASK EXISTS -->
@if ($box->items->count())
<div>
@foreach ($box->items as $item)
<div>
<form method="POST" action="/items/{{ $item->id }}">
@method('PATCH')
@csrf
<label class="checkbox {{ $item->in ? 'is-complete' : '' }}" for="in" >
<input type="checkbox" name="in" onChange="this.form.submit()" {{ $item->in ? 'checked' : '' }}>
{{ $item->box_id . ' ' . $item->item_barcode . ' ' . $item->boxbarcode }}
</label>
</form>
</div>
@endforeach
</div>
@endif
@endsection
public function show($boxId)
{
$necessaryItemCheck = ['TRTB0001', 'TRTB0005'];
//retriving related items
$box = Box::with(['items' => function($itemQuery) use($necessaryItemCheck){
//filtering items having boxBarCode required
$itemQuery->whereIn('boxBarCode', $necessaryItemCheck);
}])->where('id', $boxId)->first();
return view('boxes.show', compact('box'));
}
//boxBarCode should be retrieved from database
<a href="box/1?boxBarCode=TRTB0002">
public function show($boxId, Request $request)
{
//retriving related items
$box = Box::with(['items' => function($itemQuery) use($request){
//filtering items having boxBarCode required
$itemQuery->whereIn('boxBarCode', $request->boxBarCode);
}])->where('id', $boxId)->first();
return view('boxes.show', compact('box'));
}