dousongxuan7507 2018-02-22 10:55
浏览 92
已采纳

如何在Laravel 5.6中使用现有表?

I am extremely new to Laravel (like 2 days in learning it) and I've worked my through most of the Intro to Laravel laracast.

I thought today i'd give a my own project a go.

I want to be able to use an existing table in Laravel. For instance, I have a bible table called 'kjv'. It has the following columns:

CREATE TABLE `kjv` (
  `id` int(11) NOT NULL,
  `book` varchar(140) COLLATE utf8mb4_unicode_ci NOT NULL,
  `chapter` smallint(6) DEFAULT NULL,
  `verse` int(11) DEFAULT NULL,
  `contents` mediumtext COLLATE utf8mb4_unicode_ci,
  `in-red` mediumtext COLLATE utf8mb4_unicode_ci,
  `all-red` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

And i've imported that into my laravel database.

The thing is, I dont want to create a new migration, as that would overwrite the data.

I want to be able to use the data in my project, so how would I go about this? I'm guessing I need a model, but I have no idea what i'd write in there. Do I need a controller also?

I'd like to be able to start with a 'get verse' function in the model, so lets say, something along the lines of:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

use App\KJV;

class KJV extends Model
{
    //

    public function verse($id) {

        $verse = KJV::find($id);

        return view("verse");

    }

}

Of course, I'm really guessing here. Any help would be greatly appreciated.

  • 写回答

2条回答 默认 最新

  • drydaenth257216154 2018-02-22 10:56
    关注

    Specify the table name in the KJV model:

    protected $table = 'kjv';
    

    Then this code will work:

    KJV::find($id);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)