【问题标题】:Laravel SQLSTATE[42S22]: Column not found: 1054 Unknown column 'testimonial_by' in 'field list' (SQL: insert into `testimonials`Laravel SQLSTATE [42S22]:未找到列:1054 '字段列表'中的未知列'testimonial_by'(SQL:插入`testimonials`
【发布时间】:2019-10-02 21:15:20
【问题描述】:

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

SQLSTATE[42S22]:找不到列:1054 '字段列表'中的未知列'testimonial_by'(SQL:插入testimonialstestimonial_bytestimonial_textupdated_atcreated_at)值( John Doe,Lorem Ipsum 真的亮了!, 2019-10-02 20:37:53, 2019-10-02 20:37:53))

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

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

我似乎找不到哪里出错了。感谢您的时间和支持。

【问题讨论】:

  • 我没有看到任何使用testimonial_by 的代码。该错误发生在哪里?
  • 查看您的迁移,您似乎也没有列 testimonial_text$testimonial = Testimonial::create($request-&gt;all()); 您是否有一个输入名为 testimonial_bytestimonial_text 的表单?您需要将这些添加到您的迁移中
  • Testimonial::create($request-&gt;all()); 这条线很吓人
  • @CaddyDZ:为什么?能详细点吗?
  • 一个用户可以改变前端的 HTMlL 并发布更多 OP 将传递给 eloquent create 方法的数据

标签: php mysql laravel eloquent


【解决方案1】:

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

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

【讨论】:

  • 添加对外键的注释引用
  • 我看到我的表中也没有列。我必须使用php artisan make:migration ........ 制作列还是只去phpmyadmin 制作列?
  • 如果您已经运行过一次迁移,则需要在再次运行之前撤消它。我看到您已经手动添加了它,但将来:运行php artisan migrate:rollback,然后再次运行php artisan migrate。如果您已经创建并运行了其他迁移,则必须创建一个新的迁移来添加列。这是文档中添加列laravel.com/docs/6.x/migrations#creating-columns 的链接
  • 注意:migrate:rollback 将删除您的表格和所有数据。如果您需要保留其他列中的数据,则需要添加第二次迁移。
【解决方案2】:

您的表单中似乎有带有名称(testimonial_by 和 testimonial_text)的字段。但他们在迁移中不存在。您可以打开数据库并查看列吗? 如果他们不存在 - 添加到迁移中

$table->string('testimonial_by'); //or integet
$table->string('testimonial_text');

PS。您将来可能需要在模型protected $fillable = [array with column names for filling];

【讨论】:

  • 嗨,我将它们添加到迁移中。我刚刚发现我的桌子上不存在这些列。我应该使用迁移来制作它们吗?或者只是让他们成为phpmyadmin?
  • migrations 表中删除特定的迁移条目并再次运行php artisan:migrate 以反映新的更改..
【解决方案3】:

将这些行添加到您的迁移中..

Schema::create('testimonials', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('testimonial_by',200); 
$table->text('testimonial_text');
$table->timestamps();
});

在您的模型中将这些字段添加为可填充

class Testimonial extends Model
{
public $guarded = [];
public $timestamps = true;
protected $fillable = ['testimonial_text','testimonial_by'];
public function allTestimonials()
{
    return self::all();
}
}

仅使用必填字段创建表条目..

 $testimonial = Testimonial::create(
    'testimonial_by' => $request->input('testimonial_by'), //put current user name 
    'testimonial_text' => $request->input('testimonial_text')
  );

migrations 表中删除迁移文件条目并运行php artisan:migrate 命令。它将创建具有更新列的testimonials 表。

【讨论】:

    【解决方案4】:

    感谢大家的帮助。我发现我哪里出错了。

    我在迁移中添加了以下喜欢

            $table->string('testimonial_by');
            $table->string('testimonial_text');
    

    然后我尝试运行php artisan migrate,但我仍然遇到同样的错误。

    所以,我自己创建了这些专栏,然后简单地运行了这个项目,它成功了。

    【讨论】:

      猜你喜欢
      • 2015-01-19
      • 1970-01-01
      • 2017-03-29
      • 1970-01-01
      • 2018-02-21
      • 2015-12-08
      • 2018-11-02
      • 2019-10-02
      相关资源
      最近更新 更多