【发布时间】:2021-04-19 00:06:28
【问题描述】:
我有一个名为parts 的表。主键是part_id,它不会被递增,它是一个VARCHAR(Laravel 字符串)。所有部件 ID 均为 2 个数字、连字符、4 个数字(例如 12-3456)
当查询结果显示在网络浏览器中时,它们与 Dump&Die <?php dd($parts); ?> 所说的正在检索的内容并不完全相同...
这是我通过将测试部分添加到数据库中得出的结论:
- 似乎没有显示单个数字之前的零。 (例如04显示为4,44显示为44)
- 字符
-不显示,后面的任何其他字符也不显示。 (例如04-0001显示为4,44-4444显示为44) - 只有字母的条目显示为 0。 (例如hello显示为0,g显示为0)
- 按预期显示只有数字的字符串。 (例如1234显示为1234,123456789显示为123456789)
任何想法为什么会这样?我完全被难住了......
为了清楚起见,下面的代码和屏幕截图。
Screenshot from browser with dd() function called.
此屏幕截图显示了浏览器中显示的结果1和dd()函数01-2222的结果。
有问题的 Laravel 视图:
@extends ('layouts.default')
@section ('content')
<div class="container">
<h1 class="mb-4 pb-2">Inventory</h1>
<div class="row">
<div class="col-lg-9">
<a href="/states" class="btn btn-uce">States</a>
<div>
<p>DEVELOPMENT TASKS</p>
<div style="background-color: rgb(207, 70, 70)">
<p>- Make each header option have drop down for more options (Sort by etc.)</p>
<p>- Add filter options</p>
<p>- Total quantity = total of that item in all states</p>
</div>
</div>
<table class="parts-table" style="width:100%">
<tr class="parts-column-headers">
<th>Part ID</th>
<th>Part Name</th>
<th>Part Description</th>
<th>Total Quantity</th>
</tr>
@forelse ($parts as $part)
<nav>
<tr class="parts-rows">
<th><a href="/parts/{{ $part->part_id }}">{{ $part->part_id }}</a></th>
<th>{{ $part->part_name }}</th>
<th>{{ $part->part_description }}</th>
<th>0</th>
</tr>
</nav>
@empty
<h1>No Parts Registered Yet!</h1>
@endforelse
</table>
<?php dd($parts); ?>
<div>
<a href="parts/create" class="btn btn-uce">Create Part</a>
</div>
</div>
</div>
</div>
@endsection
我对这张表的迁移:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePartsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('parts', function (Blueprint $table) {
$table->string('part_id', 100);
$table->string('part_name');
$table->string('part_description');
$table->timestamps();
$table->primary('part_id');
$table->unique('part_id');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('parts');
}
}
控制者:
<?php
namespace App\Http\Controllers;
use App\Part;
use Illuminate\Http\Request;
class PartController extends Controller
{
public function index() {
$parts = Part::all();
return view('parts.index', compact('parts'));
}
public function show(Part $part) {
return view('parts.show', compact('part'));
}
public function create() {
return view('parts.create');
}
public function store() {
// validate
$attributes = request()->validate([
'part_id' => 'required',
'part_name' => 'required',
'part_description' => 'required'
]);
// persist
Part::create($attributes);
// redirect
return redirect('/parts');
}
}
模特:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Part extends Model
{
protected $primaryKey = 'part_id';
protected $guarded = [];
public function path() {
return "/parts/{$this->part_id}";
}
}
如果需要更多信息来帮助调试,请让我提供。我确定我错过了一些愚蠢的东西,但我一直在查看代码并且看不到它......
【问题讨论】: