laravel中Eloquent模型是一种面向对象的对数据库的封装,里面定义了<code>hasOne\hasMany</code>与其对应的<code>belongsTo</code>,还有最后就是多对多关系的belongsToMany。表面的意思很简单,一个一个来说。
hasMany
这个最好理解,就是一个<code>用户</code>对应一篇<code>文章</code>。
<code>hasOne</code>是一种获取方式,获取对应的一个内容。
class User extend Model{
publi funtion article(){
return $this->hasOne('Article');
}
}
然后在控制器controller里可以这样使用。
class Acontroller extend Controller{
public funtion index(){
$article=User::find(1)->article();
}
}
这里就不细说了,别的用发很类似,具体可以看官方文档,这样可以明白。这里主要通过一个例子来解释一个,如果使用里面的外键功能,hasOne\hasMany\BelongsTo都有定义外键的功能。首先hasOne\hasMany\BelongsTo都有默认定义外键的功能。下面通过一个例子说明。
Client.php
nameSpace App;
use Illuminate\Database\Eloquent\Model;
class Client extend Model{
protected $fillable=['name','company_name','vat','email','address','zipcode','city','primary_number','secondary_number','industry_id','company_type','fk_user_id'];
public funtion userAssignee(){
return $this->belongsTo('App\User','fk_user_id','id');
}
public funtion alltask(){
return $this->hasMany('App\Tasks','fk_client_id','id')->orderBy('status','asc')->orderBy('created_at', 'desc');
}
public funtion allleads(){
return $this->hasMany('App\Leads','fk_client_id','id')->orderBy('status', 'asc')->orderBy('created_at', 'desc');
}
}
Leads.php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Carbon;
class Leads extends Model{
protected $fillable = [ 'title', 'note', 'status', 'fk_user_id_assign', 'fk_user_id_created', 'fk_client_id', 'contact_date', ];
protected $dates = ['contact_date'];
protected $hidden = ['remember_token'];
public function clientAssignee(){
return $this->belongsTo('App\Client', 'fk_client_id');
}
public function notes(){
return $this->hasMany('App\Note', 'fk_lead_id', 'id');
}
}
首先先看Client.php里面的allleads方法,里面用到了<code>$this->hasMany('App\Leads','fk_client_id','id')</code>,这里的内容指定是调用App\Leads中的fk_client_id外键,如果不指定就会调用默认的<code>Client_id</code>这个外键,所以调用默认外键的规律就是:本类名+id,但是这个外键在被调用的这个类(App\Leads)上。如果指明外键,那就不用默认外键了,但是这个这个外键依然在被调用的类上。下面看看Leads的数据迁移部分就知道了。
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateLeadsTable extends Migration{
public function up()
{
Schema::create('leads', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('note');
$table->integer('status');
$table->integer('fk_user_id_assign')->unsigned();
$table->foreign('fk_user_id_assign')->references('id')->on('users');
$table->integer('fk_client_id')->unsigned();
$table->foreign('fk_client_id')->references('id')->on('clients');
$table->integer('fk_user_id_created')->unsigned();
$table->foreign('fk_user_id_created')->references('id')->on('users');
$table->datetime('contact_date');
$table->timestamps();
});
}
}
在Lead的表创建过程中,明确指定了Lead的外键fk_client_id关联的是clients类的id属性,这样就串起来了吧。如果使用自定义的外键,那就在被调用(leads)的时候,告诉调用者(clients),具体是哪个外键(fk_client_id)。
belongsTo
belongsTo外键的使用刚好和hasMany反过来,看Client.php的userAssignee方法,里面用到了belongsTo方法,使用的外键是fk_user_id,但这个外键是调用者的外键,即Client的内部定义的属性,可以在fillable内看到,这样就明确了,belongsTo里定义的外键是调用者(Client)内部定义外键,链接着被调用者(User)。下面是Client的迁移,可以看到是在调用者内部定义的外键。
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateClientsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('clients', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->integer('primary_number');
$table->integer('secondary_number');
$table->string('address');
$table->integer('zipcode');
$table->string('city');
$table->string('company_name');
$table->integer('vat');
$table->string('industry');
$table->string('company_type');
$table->integer('fk_user_id')->unsigned();
$table->foreign('fk_user_id')->references('id')->on('users');
$table->integer('industry_id')->unsigned();
$table->foreign('industry_id')->references('id')->on('industries');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::drop('clients');
DB::statement('SET FOREIGN_KEY_CHECKS = 1');
}
}