Laravel database sql

109 Views Asked by At

I have a SQL problem. I do not understand where I'm wrong. I've created a group of similar files and that works. In this group it tells me that label_winner does not exist. But I have generated a winner_label migration because it inverts?

Error

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))

Migration winner_label

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

Migration discipline_winner

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

Migration winner

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

WinnerController

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

There are 2 best solutions below

0
On

I found that the error is generated by this

<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>

0
On

The problem is in Model of Winner_label table. When you run a query, laravel get name of the table from model. You should call the model with correctly syntax. The alternative is to set a name of table in model of reference.

protected $table = 'winner_label';