Skip to content
Advertisement

Laravel: Why are results from the database being displayed in the view differently than what Dump&Die says is retrieved?

I have a table called parts. The primary key is part_id, it is not to be incremented, it is a VARCHAR (Laravel string). All part id’s are 2 numbers, hyphen, 4 numbers (e.g. 12-3456)

When the results from a query are displayed in the web browser they are not entirely the same as what Dump&Die <?php dd($parts); ?> says is being retrieved…

Here is what I have figured out from adding test parts to the database:

  • It seems zeros before a single digit are not displayed. (e.g. 04 is displayed as 4, 44 is displayed as 44)
  • The character - is not displayed and nor are any other characters that follow this. (e.g. 04-0001 is displayed as 4, 44-4444 is displayed as 44)
  • An entry with just letters displays as a 0. (e.g. hello is displayed as 0, g is displayed as 0)
  • A string of only numbers is displayed as is expected. (e.g. 1234 is displayed as 1234, 123456789 is displayed as 123456789)

Any ideas why this would be? I’m completely stumped…

Code and screenshots below for clarity.

Screenshot from browser with dd() function called.

This screenshot shows the result displayed in the browser 1 and the result from the dd() function 01-2222.

The Laravel view in question:

@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

My migration for this table:

<?php

use IlluminateSupportFacadesSchema;
use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

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');
    }
}

The Controller:

<?php

namespace AppHttpControllers;

use AppPart;
use IlluminateHttpRequest;

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');
    }


}

The Model:

<?php

namespace App;

use IlluminateDatabaseEloquentModel;

class Part extends Model
{
    protected $primaryKey = 'part_id';
    protected $guarded = [];

    
    public function path() {

        return "/parts/{$this->part_id}";
    }
}

If any more information is needed to help debug this please ask me to provide it. I’m sure I’ve missed something silly but I keep looking over the code and can’t see it…

Advertisement

Answer

Because you are using the part_id as your primary key. This is a poor choice, and unneccessary for the sake of one column on the database.

You need to set public $incrementing=false; on the model to tell Eloquent not to cast the column as an unsigned integer.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement