【发布时间】:2019-10-02 21:15:20
【问题描述】:
[已解决]我在尝试修复以下错误时遇到问题。
SQLSTATE[42S22]:找不到列:1054 '字段列表'中的未知列'testimonial_by'(SQL:插入
testimonials(testimonial_by,testimonial_text,updated_at,created_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->all());您是否有一个输入名为testimonial_by和testimonial_text的表单?您需要将这些添加到您的迁移中 -
Testimonial::create($request->all());这条线很吓人 -
@CaddyDZ:为什么?能详细点吗?
-
一个用户可以改变前端的 HTMlL 并发布更多 OP 将传递给 eloquent
create方法的数据
标签: php mysql laravel eloquent