填充查找表
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)
{
$catphoto = new Category_Photo(['photo_id' => $photo->id, 'category_id' => $category]);
$photo->categories()->save($catphoto);
}
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?
我设置了一个网站来销售我的照片。 照片属于类别,有多种尺寸可供选择,所以我有照片,类别和尺寸表,由category_photo和photo_size表连接。 p>
到目前为止我一直在 使用PHPMyAdmin向网站添加新照片,但手动更新三个表有点痛苦,所以我正在构建一个小后端以使其更快。 我可以很容易地将新照片添加到照片表中,但是当填写查找表的类别和大小时,我遇到了表名的问题。 p>
这是 我填写category_photo表的代码: p>
foreach($ request-> category as $ category)
{
$ catphoto = new Category_Photo(['photo_id'= > $ photo-> id,'category_id'=> $ category]);
$ photo-> categories() - > save($ catphoto);
}
code> pre>
但是,当我运行它时,我收到错误: p>
SQLSTATE [42S02]:找不到基表或视图:1146表' db.category__photos'不存在(SQL:插入`category__photos`(`photo_id`,`category_id`)值(50,1))
code> pre>
它 正在寻找一个名为category__photos而不是category_photo的表。 我怎么能纠正这个? 或者有更好的方法来实现我想要做的事情吗? p>
div>
In your Category_Photo
model class add protected $table = 'category_photo';
Because by default laravel use plural version of model for table.