samedi 21 juillet 2018

Laravel Error Base table or view not found saving model

While trying to save a model, I am getting this error using Laravel 5.6:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db_name.image' doesn't exist (SQL: insert into `activities` (`id_user`, `name`, `updated_at`, `created_at`) values (1, activity_name, 2018-07-21 18:20:50, 2018-07-21 18:20:50))

Which is really strange because the table image is not been called in the query, I don't know what to do or what may be wrong.

Here is the code that fired the error:

$activity = new Activity;
$activity->id_user= $uc->id;
$activity->name = $request->input('name');
try {
    $activity->save();
} catch (\Exception $e) {
    dd($e);
}

The Activity model:

class Activity extends Model
{
    protected  $primaryKey = 'id';

    public function categories()
    {
        return $this->belongsToMany('App\Category', 'activities_categories', 'id_activity', 'id_category');
    }


    public function images()
    {
        return $this->hasMany('App\Image', 'id_element', 'id')
                    ->select('id', 'id_element', 'name_table', 'url', 'type')
                    ->where('name_table', 'activities')
                    ->orderBy('type');
    }
}



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire