在 Mysql 中创建设置 table 并在 laravel 中使用 eloquent 获取
Creating a settings table in Mysql and fetching with eloquent in laravel
我想创建一个包含 2 列(设置、值)的设置 table,设置将包含 SITE_NAME 之类的内容,而值将是 "Facebook" 或 "Youtube" 或类似的东西。这将包含站点名称、徽标 url 等。我将如何创建它,最重要的是我将如何使用 Laravel eloquent 获取信息而没有 id 字段。
要事第一。
您的 table 需要 primary key
。根据您的示例,那将是一个 varchar
字段吗? MySql 的名称搜索比将 an 与索引(整数)进行比较 WAY SLOWER。它在理论上所做的(google 可以更好地解释它)在某种程度上将文本转换为索引并对其进行验证,而对于索引,它只是访问它。我看不出有什么不妥——事实上,我支持——将 id
列作为主键的想法。如果您担心重复,那么您可以在列 site_name
.
上设置唯一
创建迁移以创建 table
php artisan make:migration settings_table
填写您的迁移代码
<?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');
}
}
创建一个Eloquent模型
php artisan make:model Setting
填写您的 Eloquent 模型
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
}
我想创建一个包含 2 列(设置、值)的设置 table,设置将包含 SITE_NAME 之类的内容,而值将是 "Facebook" 或 "Youtube" 或类似的东西。这将包含站点名称、徽标 url 等。我将如何创建它,最重要的是我将如何使用 Laravel eloquent 获取信息而没有 id 字段。
要事第一。
您的 table 需要 primary key
。根据您的示例,那将是一个 varchar
字段吗? MySql 的名称搜索比将 an 与索引(整数)进行比较 WAY SLOWER。它在理论上所做的(google 可以更好地解释它)在某种程度上将文本转换为索引并对其进行验证,而对于索引,它只是访问它。我看不出有什么不妥——事实上,我支持——将 id
列作为主键的想法。如果您担心重复,那么您可以在列 site_name
.
创建迁移以创建 table
php artisan make:migration settings_table
填写您的迁移代码
<?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'); } }
创建一个Eloquent模型
php artisan make:model Setting
填写您的 Eloquent 模型
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 }