【问题标题】:Laravel: Why are results from the database being displayed in the view differently than what Dump&Die says is retrieved?Laravel:为什么视图中显示的数据库结果与 Dump&Die 所说的检索结果不同?
【发布时间】:2021-04-19 00:06:28
【问题描述】:

我有一个名为parts 的表。主键是part_id,它不会被递增,它是一个VARCHAR(Laravel 字符串)。所有部件 ID 均为 2 个数字、连字符、4 个数字(例如 12-3456

当查询结果显示在网络浏览器中时,它们与 Dump&Die <?php dd($parts); ?> 所说的正在检索的内容并不完全相同...

这是我通过将测试部分添加到数据库中得出的结论:

  • 似乎没有显示单个数字之前的零。 (例如04显示为444显示为44
  • 字符- 不显示,后面的任何其他字符也不显示。 (例如04-0001显示为444-4444显示为44
  • 只有字母的条目显示为 0。 (例如hello显示为0g显示为0
  • 按预期显示只有数字的字符串。 (例如1234显示为1234123456789显示为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}";
    }
}

如果需要更多信息来帮助调试,请让我提供。我确定我错过了一些愚蠢的东西,但我一直在查看代码并且看不到它......

【问题讨论】:

    标签: php html mysql laravel


    【解决方案1】:

    因为您使用 part_id 作为主键。这是一个糟糕的选择,而且对于数据库中的一列来说是不必要的。

    您需要在模型上设置public $incrementing=false; 以告诉 Eloquent 不要将该列转换为无符号整数。

    【讨论】:

    • 感谢@Snapey 的工作。你能详细说明为什么 part_id 是一个糟糕的主键选择吗?
    • 因为你遇到了这样的问题,而且你也依赖于它的独特性。现在在零件号的情况下,这可能是您想要的,但我认为您将有更多的战斗。我只是不喜欢任意字符串作为主键
    猜你喜欢
    • 2020-05-01
    • 1970-01-01
    • 2020-11-14
    • 2014-10-06
    • 2019-10-06
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 2016-11-18
    相关资源
    最近更新 更多