调用模型 [App/Model] 上的未定义关系 [relation]
Call to undefined relationship [relation] on model [App/Model]
环境:
- Laravel版本:5.8.29
- PHP 版本
$ php --version
: PHP 7.2.24 (cli)
- 数据库驱动程序和版本
$ mysql --version
:mysql Ver 8.0.23-0ubuntu0.20.04.1
- Doctrine/Inflector: 1.4
composer.json
"require": {
"php": ">=7.2.24",
"fideloper/proxy": "~4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "~1.0",
"doctrine/inflector": "1.4.0"
},
问题陈述:
以下关系在本地主机上工作但在服务器上抛出错误。这似乎是 Doctrine/Inflector 包的问题。
Note: Its working on Localhost but not on server setup.
$ php artisan tinker
>>> $borrower = Borrower::with('application')->find(2);
PHP Deprecated: The "Doctrine/Common/Inflector/Inflector::pluralize" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API. in /path/vendor/doctrine/inflector/lib/Doctrine/Common/Inflector/Inflector.php on line 264
Illuminate/Database/Eloquent/RelationNotFoundException with message 'Call to undefined relationship [application] on model [App/Borrower].'
型号
App\Borrower.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Borrower extends Authenticatable
{
protected $table = 'borrowers';
public function application()
{
return $this->hasMany('App\Models\Application', 'borrower_id', 'borrower_id');
}
App\Models\Application.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Application extends Model
{
protected $table = 'applications';
public function borrower()
{
return $this->belongsTo('App\Borrower', 'borrower_id', 'borrower_id');
}
Table
borrowers
| borrower_id | fname | email |
|-------------|--------|------------------------|
| 1 | John | john@peakyblinder.com |
| 2 | Thomas | tommy@peakyblinder.com |
applications
| id | name | borrower_id |
|----|------------|-------------|
| 1 | Birmingham | 1 |
| 2 | BBC | 1 |
问题是 doctrine inflector 方法,也许你需要升级它,否则其他解决方案是将你的 Laravel 版本至少升级到 7.x。
尝试在命令行中升级您的软件包版本:
composer update
然后安装新的 doctrine/inflector:
composer require doctrine/inflector
环境:
- Laravel版本:5.8.29
- PHP 版本
$ php --version
: PHP 7.2.24 (cli) - 数据库驱动程序和版本
$ mysql --version
:mysql Ver 8.0.23-0ubuntu0.20.04.1 - Doctrine/Inflector: 1.4
composer.json
"require": {
"php": ">=7.2.24",
"fideloper/proxy": "~4.0",
"laravel/framework": "5.8.*",
"laravel/tinker": "~1.0",
"doctrine/inflector": "1.4.0"
},
问题陈述:
以下关系在本地主机上工作但在服务器上抛出错误。这似乎是 Doctrine/Inflector 包的问题。
Note: Its working on Localhost but not on server setup.
$ php artisan tinker
>>> $borrower = Borrower::with('application')->find(2);
PHP Deprecated: The "Doctrine/Common/Inflector/Inflector::pluralize" method is deprecated and will be dropped in doctrine/inflector 2.0. Please update to the new Inflector API. in /path/vendor/doctrine/inflector/lib/Doctrine/Common/Inflector/Inflector.php on line 264
Illuminate/Database/Eloquent/RelationNotFoundException with message 'Call to undefined relationship [application] on model [App/Borrower].'
型号
App\Borrower.php
<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Borrower extends Authenticatable
{
protected $table = 'borrowers';
public function application()
{
return $this->hasMany('App\Models\Application', 'borrower_id', 'borrower_id');
}
App\Models\Application.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Application extends Model
{
protected $table = 'applications';
public function borrower()
{
return $this->belongsTo('App\Borrower', 'borrower_id', 'borrower_id');
}
Table
borrowers
| borrower_id | fname | email |
|-------------|--------|------------------------|
| 1 | John | john@peakyblinder.com |
| 2 | Thomas | tommy@peakyblinder.com |
applications
| id | name | borrower_id |
|----|------------|-------------|
| 1 | Birmingham | 1 |
| 2 | BBC | 1 |
问题是 doctrine inflector 方法,也许你需要升级它,否则其他解决方案是将你的 Laravel 版本至少升级到 7.x。
尝试在命令行中升级您的软件包版本:
composer update
然后安装新的 doctrine/inflector:
composer require doctrine/inflector