首页 > 解决方案 > Laravel SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列'testimonial_by'(SQL:插入`testimonials`

问题描述

[已解决]我在尝试修复以下错误时遇到问题。

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'testimonial_by' in 'field list' (SQL: insert into testimonials( testimonial_by, testimonial_text, updated_at, created_at) values (John Doe, Lorem Ipsum is really lit!, 2019-10-02 20:37 :53, 2019-10-02 20:37:53))

我在下面添加了与“推荐”相关的代码。

应用程序/Testimonial.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Testimonial extends Model
{
    public $guarded = [];

    public function allTestimonials()
    {
        return self::all();
    }
}

TestimonialController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Testimonial;

class TestimonialController extends Controller
{
    public function index()
    {
        $testimonials = Testimonial::all();
        return view('dashboard.testimonials.index')->withTestimonials($testimonials);
    }

    public function create()
    {
        return view('dashboard.testimonials.create');
    }

    public function store(Request $request)
    {
        $request->validate(['testimonial_text'=>'required']);
        $testimonial = Testimonial::create($request->all());
        if($testimonial)
        {
            $this->success('Testimonial added successfully');
        }
        else
        {
            $this->error();
        }
        return redirect()->back();
    }

    public function edit(Testimonial $testimonial)
    {
        return view('dashboard.testimonials.edit')->withTestimonial($testimonial);
    }

    public function update(Testimonial $testimonial,Request $request)
    {
        if($testimonial->update($request->all()))
        {
            $this->success('Testimonial Updated Successfully');
        }
        else
        {
            $this->error();
        }
        return redirect()->route('dashboard.testimonials.index');
    }

    public function destroy(Testimonial $testimonial)
    {
        if($testimonial->delete())
        {
            $this->success('Testimonial Deleted Successfully');
        }
        else
        {
            $this->error();
        }
        return redirect()->back();
    }
}

移民

<?php

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

class CreateTestimonialsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('testimonials', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->timestamps();
        });
    }

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

我似乎无法找到我要去哪里错了。感谢您的时间和支持。

标签: phpmysqllaraveleloquent

解决方案


您需要将这些列添加到迁移中:

Schema::create('testimonials', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->string('testimonial_by'); // or $table->integer('testimonial_by'); if a user ID 
    $table->string('testimonial_text');
    $table->timestamps();
});

推荐阅读