JP Foster
JP Foster

Reputation: 1845

laravel eloquent changed my query table name... ? Huh?

So I'm getting a stranger error.

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)

The thing is, 'progresses' is not my table. It's true. But I don't have 'progresses' in my code, from what I'm aware of.

Called from the index method: Check the get progress variable

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.";


}

Right now my Progress Model is empty but spelled properly.

    <?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Progress extends Model
{
    //
}

Then we have our migration... That is fine.

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

I'm really uncertain where else this could be affected by. how is this happening?! haha.

Thanks

Upvotes: 2

Views: 814

Answers (2)

Matricore
Matricore

Reputation: 593

On the other (more systematically) way,

If you don't want to use every time this one below on your model:

protected $table = 'xxx';

you could do this systematically changes below:

find line 1957 on /vendor/laravel/framework/src/illuminate/database/eloquent/model.php file

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

and change it to:

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

With this way you are free to use your table names..

Upvotes: 0

Maltronic
Maltronic

Reputation: 1802

Eloquent by convention pluralises table names, hence progress becomes progresses. You can either change the migration class to reflect this or override the convention by specifying a $table name as such:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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

    // ...the rest of the model
}

More info is available at the Laravel docs.

Upvotes: 1

Related Questions