laravel数据库sql

dy2hfwbg  于 2021-06-15  发布在  Mysql
关注(0)|答案(2)|浏览(323)

我有个sql问题。我不明白我错在哪里。我已经创建了一组类似的文件,这是可行的。在这个小组里,它告诉我 label_winner 不存在。但我产生了一个 winner_label 因为它反转而迁移?

Illuminate \ Database \ QueryException (42S02)

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'canoa.label_winner' doesn't exist (SQL: insert into `label_winner` (`created_at`, `label_id`, `updated_at`, `winner_id`) values (2018-12-18 23:41:09, 3, 2018-12-18 23:41:09, 3))

迁移优胜者标签

public function up()
    {
        Schema::create('winner_label', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('winner_id');
            $table->integer('label_id');
            $table->timestamps();
        });
    }

迁移规则\u优胜者

public function up()
    {
        Schema::create('discipline_winner', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('winner_id');
            $table->integer('discipline_id');
            $table->timestamps();
        });
    }

移民赢家

public function up()
    {
        Schema::create('winners', function (Blueprint $table) {
          $table->increments('id');
          $table->integer('user_id')->unsigned();
          $table->string('title');
          $table->string('slug')->unique();
          $table->string('image')->default('default.png');
          $table->text('body');
          $table->integer('view_count')->default(0);
          $table->boolean('status')->default(false);
          $table->boolean('is_approved')->default(false);
          $table->foreign('user_id')
            ->references('id')->on('users')
            ->onDelete('cascade');
          $table->timestamps();
      });

Winner控制器

public function store(Request $request)
    {
      $this->validate($request,[
          'title' => 'required|unique:winners',
          'image' => 'image',
          'disciplines' => 'required',
          //'labels' => 'required',
          'body' => 'required',
      ]);
      $image = $request->file('image');
      $slug = str_slug($request->title);
      if(isset($image))
      {
//            make unipue name for image
          $currentDate = Carbon::now()->toDateString();
          $imageName  = $slug.'-'.$currentDate.'-'.uniqid().'.'.$image->getClientOriginalExtension();
          if(!Storage::disk('public')->exists('winner'))
          {
              Storage::disk('public')->makeDirectory('winner');
          }
          $winnerImage = Image::make($image)->resize(1600,1066)->stream();
          Storage::disk('public')->put('winner/'.$imageName,$winnerImage);
      } else {
          $imageName = "default.png";
      }
      $winner = new Winner();
      $winner->user_id = Auth::id();
      $winner->title = $request->title;
      $winner->slug = $slug;
      $winner->image = $imageName;
      $winner->body = $request->body;
      if(isset($request->status))
      {
          $winner->status = true;
      }else {
          $winner->status = false;
      }
      $winner->is_approved = true;
      $winner->save();
      $winner->disciplines()->attach($request->disciplines);
      $winner->labels()->attach($request->labels);

      Toastr::success('Post Successfully Saved :)','Success');
      return redirect()->route('admin.winner.index');
    }
uqcuzwp8

uqcuzwp81#

问题是在winner\u标签表的模型中。运行查询时,laravel从模型中获取表的名称。您应该使用正确的语法调用模型。另一种方法是在引用模型中设置表的名称。

protected $table = 'winner_label';
cqoc49vn

cqoc49vn2#

我发现这个错误是由此产生的

<div class="form-group">
  <label>Select Labels</label>
  <select class="form-control select2" name="labels[]" id="label" data-live-search="true" multiple="multiple">
    @foreach($labels as $label)
    <option value="{{ $label->id }}">{{ $label->name }}</option>
    @endforeach
  </select>
</div>

相关问题