Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pinkmoney's avatar

Illuminate\Database\QueryException : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'posts.' in 'where clause' (SQL: select `slug`, `posts`.`id` from `posts` where (`slug` = et-adipisci-culpa-voluptatem-consequatur-hic or `slug` LIKE et-adipisci-

migrations->create_posts_table.php

class CreatePostsTable extends Migration { public function up() { Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->string('slug')->unique(); $table->text('description'); $table->text('content'); $table->integer('created_by')->unsigned(); $table->timestamps();

        $table->foreign('created_by')->references('id')->on('users');
    });
}

}

migration->create_categories_table.php

class CreateCategoriesTable extends Migration { public function up() { Schema::create('categories', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->string('slug'); $table->string('icon')->nullable(); $table->timestamps(); }); } }

migration->create_category_post_table.php

class CreateCategoryPostTable extends Migration { public function up() { Schema::create('category_post', function (Blueprint $table) { $table->increments('id');

        $table->integer('post_id')->unsigned();
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');;

        $table->integer('category_id')->unsigned()->nullable();
        $table->foreign('category_id')->references('id')->on('categories')->onDelete('cascade');;

        $table->timestamps();
    });
}

}

factories-> PostFactory.php

$factory->define(App\Post::class, function (Faker $faker) { return [ 'title' => $faker->sentence(5), 'description' => $faker->sentences(2, true), 'content' => markdownContent($faker), 'created_by' => App\User::admin()->first()->id, 'status' => \Hootlex\Moderation\Status::APPROVED, 'moderated_at' => time() ];

Models->Post.php

class Post extends Model implements HasMedia { use Sluggable, HasMediaTrait, Moderatable, HashOrSlugScope;

protected $fillable = ['title', 'slug', 'description', 'content'];

/**
 * The user that belong to the post.
 * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
 */
public function owner()
{
    return $this->belongsTo(User::class, 'created_by', 'id');
}

/**
 * The categories that belong to the post.
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
public function categories()
{
    return $this->belongsToMany(Category::class);
}


/**
 * Check if the post has specific category.
 * @param $category
 *
 * @return bool
 */
public function hasCategory($category)
{
    $category_id = (is_int($category)) ? $category: $category->id;
    foreach ($this->categories as $cat) {
        if ($category_id === $cat->id){
            return true;
        }
    }
    return false;
}

/**
 * Get post status as human readable string.
 * @return string
 */
public function getHumanStatus()
{
    switch ($this->status) {
        case Status::APPROVED:
            $status = 'approved';
            break;
        case Status::REJECTED:
            $status = 'rejected';
            break;
        case Status::PENDING:
            $status = 'pending';
            break;
        case Status::POSTPONED:
            $status = 'postponed';
            break;
    }
    return $status;
}
/**
 * MUTATORS
 */
public function getHashidAttribute()
{
    return Hashids::encode($this->id);
}

/**
 * Get the original Url to an image
 * @return string|null
 */
public function getImageUrlAttribute()
{
    return $this->hasMedia() ? $this->getFirstMedia('featured')->getUrl() : null;
}

/**
 * Return the sluggable configuration array for this model.
 *
 * @return array
 */
public function sluggable()
{
    return [
        'slug' => [
            'source' => 'title'
        ]
    ];
}

} });

0 likes
1 reply
bobbybouwmann's avatar

You never create a slug in your Post factory. Try this instead

$factory->define(App\Post::class, function (Faker $faker) { 
    $title = $faker->sentence(5);
    $slug = Str::slug($title);

    return [
        'title' => $title,
        'slug' => $slug,
        'description' => $faker->sentences(2, true), 
        'content' => markdownContent($faker), 
        'created_by' => App\User::admin()->first()->id, 
        'status' => \Hootlex\Moderation\Status::APPROVED, 
        'moderated_at' => time()
    ];
}

Please or to participate in this conversation.