1
It's since two days that I'm trying to solve an issue that i face when i try to insert a record in the DB.
I'm building a project where an admin can create a list of places to visit for users.
I have created 3 tables and relative models as follow : places , place_categories, category_places (this the pivot table).
places
table has the following inside :
Schema::create('places', function (Blueprint $table) {
$table->id();
$table->timestamps();
$table->string('google_place_id');
$table->unsignedBigInteger('property_id');
$table->foreign('property_id')->references('id')->on('properties');
});
And the Place
model has the following :
protected $fillable = [
'google_place_id',
'property_id'
];
public function property(){
return $this->belongsTo(Property::class);
}
public function category(){
return $this->belongsToMany(PlaceCategory::class);
}
The place_categories
table has the following code :
Schema::create('place_categories', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
the PlaceCategory
model has the following :
protected $fillable = [
'name'
];
public function place(){
return $this->belongsToMany(Place::class);
}
While the pivot category_places
table has the following :
Schema::create('category__places', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('place_id');
$table->foreign('place_id')->references('id')->on('places');
$table->unsignedBigInteger('category_id');
$table->foreign('category_id')->references('id')->on('place_categories');
$table->timestamps();
});
And the Category_Place
has the following code :
protected $table = 'category_places';
protected $fillable = [
'category_id',
'place_id',
];
}
In my PlaceController
, in the store
method i wrote the following :
public function store(Request $request)
{
$googlePlaceId = $request->input('google_place_id');
$propertyId= $request->input('property_id');
$categories= $request->input('categories');
$place = Place::create([
'google_place_id' => $googlePlaceId,
'property_id'=> $propertyId,
]);
$place->category()->attach($categories);
return redirect()->route('home', ['property'=> $propertyId])->with('message', 'Place correctly created');
}
Now, when i try to insert a new place with relative categories i keep getting the following error :
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'myhosthtl.place_place_category' doesn't exist
insert into `place_place_category` (`place_category_id`, `place_id`) values (1, 3)
So he's looking for a table called place_place_category that obviously does not exist, but where do he get this table from? I have also declared the protected $table
in my Category_Place
model.
After debugging a bit the code, i found out the the error i thrown at line 63 in the store
method at this line :
$place->category()->attach($categories);
It seem like he cant find the place_categories table, but he should insert in the category_places the record for the pivot, not in the places_categories.
Is there anybody able to help me? What Am i doing wrong?
Thank you so much for your help.
0 comments:
Post a Comment
Thanks