【发布时间】:2018-10-17 13:49:43
【问题描述】:
我对 Laravel 很陌生,现在我正在尝试将以前的应用程序的一部分从一个小型的自写框架移动到 Laravel。通讯录是多语言的,所以表结构有点复杂。
这是我的源代码:
- AddressBookController.php
namespace App\Http\Controllers;
use App\AddressBook as AB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
class AddressBookController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$entries = AB::all();
return view('addressBook')->with([
'class' => __CLASS__,
'function' => __FUNCTION__,
'line' => __LINE__,
'entries' => $entries,
]);
}
}
-
型号AddressBook.php
namespace App; use Illuminate\Database\Eloquent\Model; class AddressBook extends Model { protected $table = 'address'; protected $primaryKey = 'address_id'; protected $keyType = 'int'; public $incrementing = true; public $timestamps = false; protected $searchable = [ 'columns' => [ 'address.address_surname' => 10, 'address.address_company' => 5, 'address.address_vatid' => 2, ], ]; public function country() { return $this->hasOne('country', 'country_id', 'country_id'); } public function addresstype() { return $this->hasOne('addresstype', 'addresstype_id', 'addresstype_id'); } } -
模型国家.php
namespace App; use Illuminate\Database\Eloquent\Model; class Country extends Model { protected $table = 'country'; protected $primaryKey = 'country_id'; protected $keyType = 'int'; public $incrementing = true; public $timestamps = false; public function translation() { return $this->hasOne('translations', 'translations_id', 'translations_id'); } public function addressbook() { return $this->belongsTo('address', 'country_id', 'country_id'); } } -
型号地址类型
namespace App; use Illuminate\Database\Eloquent\Model; class AddressType extends Model { protected $table = 'addresstype'; protected $primaryKey = 'addresstype_id'; protected $keyType = 'int'; public $incrementing = true; public $timestamps = false; public function translation() { return $this->hasOne('translations', 'translations_id', 'translations_id'); } public function addressbook() { return $this->belongsTo('address', 'addresstype_id', 'addresstype_id'); } } -
模型翻译.php
namespace App; use Illuminate\Database\Eloquent\Model; class Translation extends Model { protected $table = 'translations'; protected $primaryKey = 'translations_id'; protected $keyType = 'int'; public $incrementing = true; public $timestamps = false; public function country() { return $this->belongsTo('country', 'translations_id', 'translations_id'); } public function addresstype() { return $this->belongsTo('addresstype', 'translations_id', 'translations_id'); } }
请求“$entries = AB::all();”一般工作,但我得到了 id,也许我在这里完全错了,但我认为来自外键的数据将被相应的模型替换(如果配置正确)。所以我的问题是:
一个。我在配置过程中是否犯了错误,如果是,错误究竟出在哪里?
或
湾。我用对象替换 id 的假设是完全错误的吗?
提前致谢! 史蒂夫
【问题讨论】:
标签: laravel model belongs-to has-one