在laravel中,我在向db添加数据时遇到以下错误

时间:2018-03-13 09:52:15

标签: php sql laravel

我正在尝试使用以下代码以dB为单位添加类别,但它显示的错误如“SQLSTATE [42S02]:未找到基表或视图:1146表'mydb.categories'不存在(SQL:插入category_nameupdated_atcreated_at<?php namespace App\Http\Controllers\Admin; use Illuminate\Http\Request; use App\Http\Controllers\Controller; use App\Category; use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Redirect; use Illuminate\Support\Facades\Auth; class AdminController extends Controller { public function show() { return view('admin/admin'); // echo "here"; } public function addCategory() { $category=new Category; $category->category_name = Input::get('category'); $category->save(); dd("2"); //return view('admin'); } } )值(时尚,2018-03-13 09:44:38,2018-03-13 09:44: 38))“

我的代码如下

管理控制器

<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <style>
        .bod{
            background-color:#f8f8f8;
            border-shadow: 5px 10px 20px white inset;
            border-width:5px;
            margin-top:-12px;
        }
        .h1{
            font-family:"Book Antiqua";
            font-size: 90px;
            color: red;
            text-align: center;
        }
        div {
            border-radius: 5px;
            /*background-color: #f2f2f2;*/
            /*background-color: #7CA8C1  ;*/
            padding: 20px;
            position: relative;
            /*border:2px solid white;*/
            height: 200px;
            width: 50%;
        }
    </style>
</head>
<body class="bod">
<h1 class="h1">Admin</h1>
<div align="left">
    <form action="{{ route('addCategory') }}" method="get">
        {{ csrf_field() }}
        <label for="category" style="font-family: 'Book Antiqua';font-size: 48px">Category</label> <br/>
        <input type="text" name="category" id="category" placeholder="Enter the Category" style="width: 25%;height:35px; border:2px solid floralwhite">
        <input type="submit" name="add" id="add" value="ADD">
    </form>
</div>
</body>
</html>

Admin.blade.php

Route::get('/addCategory','Admin\AdminController@addCategory')->name('addCategory');

路由

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    //

    protected $fillable = [
        'category_name',
    ];

}

类别模型

-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Mar 13, 2018 at 10:12 AM
-- Server version: 5.7.19
-- PHP Version: 5.6.31

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `mydb`
--

-- --------------------------------------------------------

--
-- Table structure for table `category`
--

DROP TABLE IF EXISTS `category`;
CREATE TABLE IF NOT EXISTS `category` (
  `category_id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `category_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`category_id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

db Strcture

Some

2 个答案:

答案 0 :(得分:1)

将此添加到您的模型

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
    //
    protected $table = "category";
    protected $fillable = [
        'category_name',
    ];
}

您的表名是category,laravel认为您的类别应该使用db migration来创建数据库表,这将帮助您远离这些错误。

答案 1 :(得分:-1)

您必须在模型中提及表名。

protected $table = "Categories";