php - 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');
}
}
我似乎无法找到我要去哪里错了。感谢您的时间和支持。
解决方案
您需要将这些列添加到迁移中:
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();
});
推荐阅读
- dns - GCloud 错误:我已将 A 类型的 DNS dinhonesto.com.br 和 www.dinhonesto.com.br 配置为外部 ip,但只有 dinhonesto.com.br 有效
- javascript - 滑块的填充影响固定顶栏
- c# - 卡在循环c#
- java - java累积和扫描仪方法printf
- c++ - 在给定操作成本的情况下优化构造字符串的算法
- java - 可用于 Android 应用程序开发的最新 Java SDK 是什么?
- rxjs - 一旦可观察数据变量不为 NULL,如何发出
- macos - Mac OSX OpenGL 没有得到关键事件
- powershell - 删除随机注册表项
- javascript - 在 d3.js 中更新大型数据集的列类型