2015-11-12 44 views
2

所以我得到一個陌生人的錯誤。laravel雄辯改變了我的查詢表名...?咦?

QueryException in Connection.php line 651: SQLSTATE[42S02]: 
Base table or view not found: 1146 La table 
'aba.progresses' n'existe pas 
(SQL: select * from `progresses` where 
`user_id` = 1 and `task_type` = Match Pictures limit 1) 

事情是,'進展'不是我的表。這是真的。但是,從我所知道的情況來看,我的代碼中沒有「進步」。

從索引方法調用:檢查取得進展變量

public function index() 
{ 
    $tasktype = 'Match Pictures'; 

    $session = \App\Sessions::where('user_id', '=', \Auth::user()->id) 
      ->where('task_type', '=', $tasktype) 
      ->where('status', '=', 'started')->first(); 

    //if no session has been started. 
    //then start the session and redirect 
    if (empty($session)) 
    { 
     //get Progress info 
     $getProgress = \App\Progress::where('user_id', '=', \Auth::user()->id) 
      ->where('task_type', '=', $tasktype)->first(); 

     if (empty($getProgress)) 
     { 
      $newProgress = new \App\Progress; 
      $newProgress->user_id = \Auth::user()->id; 
      $newProgress->task_type = $tasktype; 
      $newProgress->stimuli_set = 5; 
      $newProgress->save(); 


      $addSession = new \App\Session; 
      $addSession->user_id = \Auth::user()->id; 
      $addSession->task_type = $tasktype; 
      $addSession->stimuli_set = $newProgress->stimuli_set; 
      $addSession->number_of_trials = $newProgress->stimuli_set; 
      $addSession->save(); 

      //Gather Data 
      $data['status'] = 'first'; 
      $data['message'] = "This is your first time playing Match Pictures!"; 


      //send to view with task start button and introduction 
      return view('task.matchpictures.intro'); 
     } 
     else 
     { 
      //add Session 
      $addSession = new \App\Session; 
      $addSession->user_id = \Auth::user()->id; 
      $addSession->task_type = $tasktype; 
      $addSession->stimuli_set = $getProgress->stimuli_set; 
      $addSession->number_of_trials = $getProgress->stimuli_set; 
      $addSession->save(); 

      $data['status'] = "not first"; 
      $data['message'] = "This is your first time playing Match Pictures!"; 

      //send to view with task start button and introduction 
      return view('task.matchpictures.intro'); 

     } 

    } 
    else 
    { 
     //Make Sure that the Session has Finished. 


     $trialCount = \App\Trials::where('session_id', '=', $session->session_id) 
       ->count(); 

     if ($trialCount >= $session->number_of_trials) 
     { 
      //end session by update session row (status = done) 
      // then send to page for summary 

      $session->status = 'done'; 
      $session->save(); 

      //send view to a Task Summary Page 
      return view('task.matchpictures.summary'); 
     } 
     else 
     { 
      //A session has started but is not done. Proceed: 
      redirect('/task/matching/active'); 
     } 


    } 


    return "There was an error. Go back to the main page."; 


} 

現在我的進步模式是空的,但拼寫正確。

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Progress extends Model 
{ 
    // 
} 

然後,我們有我們的遷移...這很好。

<?php 

use Illuminate\Database\Schema\Blueprint; 
use Illuminate\Database\Migrations\Migration; 

class CreateProgressTable extends Migration 
{ 
    /** 
    * Run the migrations. 
    * 
    * @return void 
    */ 
    public function up() 
    { 
     Schema::create('progress', function (Blueprint $table) { 
      $table->unsignedInteger('user_id'); 
      $table->string('task_type'); 
      $table->integer('stimuli_set')->default('5'); 
      $table->foreign('user_id')->references('id')->on('users'); 
     }); 
    } 

    /** 
    * Reverse the migrations. 
    * 
    * @return void 
    */ 
    public function down() 
    { 
     Schema::drop('progress'); 
    } 
} 

我真的不確定在哪裏可能會受到影響。這是怎麼發生的?!哈哈。

謝謝

+0

看起來雄辯被翻譯您的類名成多。在ORM中並不罕見。 – Kenney

回答

1

按照約定多次表述名稱,因此進展變得進行。您可以通過指定$table名這樣的更改遷移類來反映這個或覆蓋約定:

<?php 

namespace App; 

use Illuminate\Database\Eloquent\Model; 

class Progress extends Model 
{ 
    /** 
    * @var string 
    */ 
    protected $table = 'progress'; 

    // ...the rest of the model 
} 

更多信息可在Laravel docs

+1

完美。感謝您的解釋。我明白爲什麼現在發生了。 –

0

在其他(更系統)的方式,

如果你不想在這之下的每次使用您的手機型號:

protected $table = 'xxx'; 

你可以做到這一點下面的系統變化:

在/vendor/laravel/framework/src/illuminate/database/eloquent/model.php文件

行查找1957年

return str_replace('\\', '', Str::snake(Str::plural(class_basename($this)))); 

並將其更改爲:

return str_replace('\\', '', Str::snake(class_basename($this))); 

用這種方式,您可以自由使用你的表名..