<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class SettingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('settings', function (Blueprint $table) {
//$table->increments('id'); This will come in hand, trust me
$table->string('site_name',255)->unique();
//If you really insist on having site_name as primary key, then use the line below
// $table->string('site_name',255)->primary();
$table->string('value',255);
//$table->timestamps(); Do you need?
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('settings');
}
}
创造一个有说服力的模型
php artisan make:model Setting
充满你雄辩的模型
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Setting extends Model
{
protected $fillable = ['site_name','value'];
//protected $table = ['settings'] Only if you really need it
//Usually, if you take Laravel's approach, Models are singular version of the tables, that are in plural, but you can work it as you like
}
1条答案
按热度按时间7kqas0il1#
第一件事。
你的table需要一张table
primary key
. 那会是一个好主意吗varchar
根据你的例子?mysql的按名称搜索要比比较索引(整数)慢得多。它在理论上的作用(谷歌可以更好地解释)是将文本转换成一个索引并验证它,而索引只是访问它。事实上,我支持拥有一个id
列作为主键。如果您担心重复,那么可以在列上设置uniquesite_name
.创建迁移以创建表
填写迁移代码
创造一个有说服力的模型
充满你雄辩的模型