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

Laravel自定义End模型数据库连接与表为空问题求助

问题:单页内容数据库数据无法展示,$end模型实例为空

我创建了一个存储单页内容的数据库,已执行迁移并插入数据,但展示数据时页面无内容。用dd($end)调试发现数据库连接和表均为空,而dd($blog)能正常显示MySQL连接及对应表数据。


Web路由

Route::get('/end-to-end', [EndController::class, 'view']);

控制器代码

<?php

namespace App\Http\Controllers;

use App\Models\End;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class EndController extends Controller
{
    public function view(End $end)
    {
        return view('static_edit.end-to-end', ['end' => $end]);
    }


    public function update(Request $request, End $end)
    {
        $formFields = $request->validate([
            'header' => 'required',
            'sub_header' => 'required',
            'header_para' => 'required',
            'header_img' => 'required',
            'header_card' => 'required',
            'card_svg1' => 'required',
            'card_svg2' => 'required',
            'card_svg3' => 'required',
            'card_h1' => 'required',
            'card_h2' => 'required',
            'card_h3' => 'required',
            'card_para1' => 'required',
            'card_para2' => 'required',
            'card_para3' => 'required',
            'steps_header' => 'required',
            'steps_h1' => 'required',
            'steps_h2' => 'required',
            'steps_h3' => 'required',
            'steps_h4' => 'required',
            'steps_h5' => 'required',
            'steps_h6' => 'required',
            'steps_h7' => 'required',
            'steps_h8' => 'required',
            'steps_para1' => 'required',
            'steps_para2' => 'required',
            'steps_para3' => 'required',
            'steps_para4' => 'required',
            'steps_para5' => 'required',
            'steps_para6' => 'required',
            'steps_para7' => 'required',
            'steps_para8' => 'required',
            'steps_img1' => 'required',
            'steps_img2' => 'required',
            'steps_img3' => 'required',
            'steps_img4' => 'required',
            'steps_img5' => 'required',
            'steps_img6' => 'required',
            'steps_img7' => 'required',
            'steps_img8' => 'required'
        ]);
        $end->update($formFields);
    }
}

模型代码

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class End extends Model
{
    use HasFactory;
    public $casts = [
        'header',
        'sub_header',
        'header_para',
        'header_img',
        'header_card',
        'card_svg1',
        'card_svg2',
        'card_svg3',
        'card_h1',
        'card_h2',
        'card_h3',
        'card_para1',
        'card_para2',
        'card_para3',
        'steps_header',
        'steps_h1',
        'steps_h2',
        'steps_h3',
        'steps_h4',
        'steps_h5',
        'steps_h6',
        'steps_h7',
        'steps_h8',
        'steps_para1',
        'steps_para2',
        'steps_para3',
        'steps_para4',
        'steps_para5',
        'steps_para6',
        'steps_para7',
        'steps_para8',
        'steps_img1',
        'steps_img2',
        'steps_img3',
        'steps_img4',
        'steps_img5',
        'steps_img6',
        'steps_img7',
        'steps_img8',
    ];
}

迁移代码

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('ends', function (Blueprint $table) {
            $table->id();
            $table->string('header');
            $table->string('sub_header');
            $table->text('header_para');
            $table->binary('header_img');
            $table->string('header_card');
            $table->binary('card_svg1');
            $table->binary('card_svg2');
            $table->binary('card_svg3');
            $table->string('card_h1');
            $table->string('card_h2');
            $table->string('card_h3');
            $table->text('card_para1');
            $table->text('card_para2');
            $table->text('card_para3');
            $table->string('steps_header');
            $table->string('steps_h1');
            $table->string('steps_h2');
            $table->string('steps_h3');
            $table->string('steps_h4');
            $table->string('steps_h5');
            $table->string('steps_h6');
            $table->string('steps_h7');
            $table->string('steps_h8');
            $table->text('steps_para1');
            $table->text('steps_para2');
            $table->text('steps_para3');
            $table->text('steps_para4');
            $table->text('steps_para5');
            $table->text('steps_para6');
            $table->text('steps_para7');
            $table->text('steps_para8');
            $table->binary('steps_img1');
            $table->binary('steps_img2');
            $table->binary('steps_img3');
            $table->binary('steps_img4');
            $table->binary('steps_img5');
            $table->binary('steps_img6');
            $table->binary('steps_img7');
            $table->binary('steps_img8');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('ends');
    }
};

问题原因及解决方法

1. 隐式路由模型绑定未生效

你的路由/end-to-end没有传递模型ID参数,Laravel无法自动查找对应的End模型实例,导致注入的$end是空对象。由于这是单页内容,数据库中应该只有一条记录,无需路由参数,直接在控制器中查询唯一记录即可:

修改控制器view方法:

public function view()
{
    // 查询唯一的单页内容记录
    $end = End::firstOrFail();
    return view('static_edit.end-to-end', ['end' => $end]);
}

2. 模型$casts属性格式错误

模型中的$casts必须是键值对格式(键为字段名,值为转换类型),当前写法会导致Laravel无法正确解析,影响数据读取:

修改模型$casts:

public $casts = [
    'header' => 'string',
    'sub_header' => 'string',
    'header_para' => 'string',
    'header_img' => 'string', // binary字段建议转string存储,可根据实际需求调整类型
    'header_card' => 'string',
    'card_svg1' => 'string',
    'card_svg2' => 'string',
    'card_svg3' => 'string',
    'card_h1' => 'string',
    'card_h2' => 'string',
    'card_h3' => 'string',
    'card_para1' => 'string',
    'card_para2' => 'string',
    'card_para3' => 'string',
    'steps_header' => 'string',
    'steps_h1' => 'string',
    'steps_h2' => 'string',
    'steps_h3' => 'string',
    'steps_h4' => 'string',
    'steps_h5' => 'string',
    'steps_h6' => 'string',
    'steps_h7' => 'string',
    'steps_h8' => 'string',
    'steps_para1' => 'string',
    'steps_para2' => 'string',
    'steps_para3' => 'string',
    'steps_para4' => 'string',
    'steps_para5' => 'string',
    'steps_para6' => 'string',
    'steps_para7' => 'string',
    'steps_para8' => 'string',
    'steps_img1' => 'string',
    'steps_img2' => 'string',
    'steps_img3' => 'string',
    'steps_img4' => 'string',
    'steps_img5' => 'string',
    'steps_img6' => 'string',
    'steps_img7' => 'string',
    'steps_img8' => 'string',
];

3. update方法路由与逻辑调整

单页内容更新无需传递ID,直接查询唯一记录进行更新:

修改控制器update方法:

public function update(Request $request)
{
    $formFields = $request->validate([
        // 保留原有验证规则
    ]);
    End::firstOrFail()->update($formFields);
    return back()->with('success', '内容更新成功');
}

对应路由调整为不带参数的PUT请求:

Route::put('/end-to-end', [EndController::class, 'update']);

内容的提问来源于stack exchange,提问作者Jasper Kenneth Samson

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 19:24:52