So, I have been struggling with this problem. in a hasMany relationship, Laravel is expecting me to make the foreign key in the attachment table which is wrong for my DB structure, I should have it in the category table instead.
When I do
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$categories = Category::with('attachments')->get();
return $this->success(ApiResponseMessage::SUCCESS, $categories);
}
I get the following error:
Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found:
1054 Unknown column 'attachments.category_id' in 'where clause'
(SQL: select * from `attachments` where `attachments`.`category_id` = 1
and `attachments`.`category_id` is not null) in file
F:\xampp\htdocs\svysh\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 671
Category
class Category extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'slug'
];
public function attachments()
{
return $this->hasMany('App\Attachment');
}
}
public function up()
{
Schema::create('attachments', function (Blueprint $table) {
$table->id();
$table->string('url');
$table->string('width');
$table->string('height');
$table->timestamps();
});
}
Attachment
class Attachment extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'url', 'width', 'height', 'attachment_id'
];
public function category()
{
return $this->belongsTo('App\Category');
}
}
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('slug');
$table->integer('attachment_id')->nullable()->references('id')->on('attachment');
$table->timestamps();
});
}