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条)

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测