You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel中如何正确设置模型默认属性?CRUD数据不显示求助

Fixing Laravel Model Default Values & Data Display Issues

Hey there! Let's break down the problems you're facing step by step—there are a few small mistakes in your code that are causing both the default values to not work as expected and the frontend table to show no data.

1. Model File Issues (Naming, Spelling, and Default Value Logic)

First, Laravel follows strict conventions, and a few typos plus incorrect logic in your model are throwing things off:

  • Model Naming: Laravel models use PascalCase (首字母大写驼峰式), so your model class should be Crud (not crud) and the filename must match exactly as Crud.php.
  • Spelling Mistakes:
    • $timestramp → should be $timestamps (plural, correct spelling)
    • $primarykey → should be $primaryKey (capitalized "K")
  • Default Value for id: The id is your auto-incrementing primary key—you don't need to set a default value here. The database automatically generates this value when creating new records, so remove it from $attributes.

Corrected Model Code (Crud.php):

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Crud extends Model {
    // Disable timestamps since your migration doesn't include them
    protected $timestamps = false;
    // Define primary key (optional here, "id" is Laravel's default)
    protected $primaryKey = "id";
    // Allow mass assignment for these fields (required for Crud::create())
    protected $fillable = ['element1', 'element2'];
    
    /**
     * Default values for non-primary attributes
     */
    protected $attributes = [
        'element1' => "ABC",
        'element2' => "abc",
    ];
}

Note: The $fillable property tells Laravel which fields are allowed for mass assignment (like when using Crud::create()). Without this, you'll get a mass assignment error when creating new records.

2. View File: Incorrect Attribute Access

The main reason your frontend table shows no data is how you're accessing model attributes in your blade view. Using $item->$id is wrong—it tries to access a variable named $id instead of the model's id attribute. Remove the extra $ from attribute names.

Corrected View Code (Loop Section):

<tbody>
@foreach ($CRUDitems as $item)
<tr>
<th scope="row">{{ $item->id }}</th>
<td>{{ $item->element1 }}</td>
<td>{{ $item->element2 }}</td>
</tr>
@endforeach
</tbody>

3. Understanding Model Default Values

A quick clarification: The $attributes default values only apply when you create a new model instance (e.g., Crud::create() or new Crud()). They don't automatically add a record to your database. To see data in your table, you need to manually create a record first.

You can do this easily with Artisan Tinker:

  1. Run php artisan tinker in your terminal
  2. Type Crud::create(); and hit enter—this creates a new record using your default values
  3. Exit Tinker with exit

Now when you visit your /CRUD route, you should see the record in your table.

4. Controller & Route Check

Your controller and route look mostly fine, but update the model reference in your controller to match the corrected Crud class:

  • Add use App\Crud; at the top of CRUDController.php
  • Change crud::all(); to Crud::all();

Quick Recap of Fixes

  • Fix model naming, spelling errors, and remove the id default value
  • Correct attribute access in your blade view (remove extra $ from attribute names)
  • Add $fillable to your model for mass assignment
  • Create a test record using Tinker to see default values in action

内容的提问来源于stack exchange,提问作者Jackyrm

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 09:24:16