Simply, in your CategoryPhoto Model:
class CategoryPhoto extends Model
{
protected $table = 'category_photo';
//code
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi,
I have a site set up to sell my photographic prints. The photos are in categories and are available in a number of sizes, so I have a photos, categories and sizes table, joined by a category_photo and photo_size table.
Up to now I have been adding new photos to the site using PHPMyAdmin, but manually updating three tables is a bit of a pain, so I am building a little backend to make it quicker. I can add a new photo to the photos table easily enough, but when it comes to populating the lookup tables for category and size I am running into a problem with the table names.
Here is my code for populating the category_photo table:
foreach($request->category as $category)
{
$cat_photo = new Category_Photo;
$cat_photo->photo_id = $photo->id;
$cat_photo->category_id = $category;
$cat_photo->save();
}
However, when I run this, I get the error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.category__photos' doesn't exist (SQL: insert into `category__photos` (`photo_id`, `category_id`) values (50, 1))
It is looking for a table called category__photos rather than category_photo. How can I rectify this? Or is there a better way to achieve what I'm trying to do?
I think you should anyway use the attach method for this situation: https://laravel.com/docs/5.2/eloquent-relationships#inserting-many-to-many-relationships
Please or to participate in this conversation.