【问题标题】:Laravel ManyToMany relation with singular table namesLaravel 多对多关系与单数表名
【发布时间】:2021-04-08 08:19:14
【问题描述】:

我想在 samplessupposed_origins 之间创建 ManyToMany 关系。
在我的公司中,我们的约定是使用单一的表名,所以正如https://laravel.com/docs/8.x/eloquent#table-names 中提到的,我定义了:
迁移

  Schema::create('v3_sample', function (Blueprint $table) {
    $table->increments('id');
    $table->string('number_canonical', 6)->unique(); // business key
    $table->string('name')->nullable();
  };

  Schema::create('v3_supposed_origin', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('location_id')->unsigned(); // from other database
    $table->string('description');
  });

  Schema::create('v3_sample_supposed_origin', function (Blueprint $table) {
    $table->increments('id');

    $table->integer('sample_id')->unsigned();
    $table->foreign('sample_id')->references('id')->on('v3_sample')->onDelete('cascade');

    $table->integer('supposed_origin_id')->unsigned();
    $table->foreign('supposed_origin_id')->references('id')->on('v3_supposed_origin')->onDelete('cascade');
  });


模型

class Sample extends Model
{
    protected $table = 'v3_sample';

    public function supposed_origins() {
        dd($this);
        return $this->belongsToMany('App\SupposedOrigin');
    }
}

class SupposedOrigin extends Model
{
    protected $table = 'v3_supposed_origin';

    public function samples() {
        return $this->belongsToMany('App\Sample');
    }   
}

控制器

class SampleController extends Controller
{
    // […]

    public function edit(Request $request, $number_canonical)
    {
        $sample_record = DB::table('v3_sample')->where('number_canonical', $number_canonical)->first();
        $supposed_origins = $sample_record->supposed_origins; 
        dd($supposed_origins); // returns ErrorException Undefined property: stdClass::$supposed_origins 

SampleController 返回ErrorException Undefined property: stdClass::$supposed_origins

由于我有单数表名,the rule 定义为具有单数表名 (sample_supposed_origin) 到复数表名(samples 和假定_origins 而不是我的 sample 假定_origin)的数据透视表!

问题:

  • 单数命名是找不到关系的原因吗?
  • 有没有办法正确指定它?添加数据透视表模型?

附加信息:Laravel Framework 7.28.4、PHP 8.0.3、mariadb Ver 15.1 Distrib 10.1.47-MariaDB

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    您的问题是您使用的是查询生成器,而不是 Eloquent 查询生成器。

    DB::table('v3_sample')->where('number_canonical', $number_canonical)->first() 将返回一个表示数据库结果的对象,但它只是一个标准的 PHP 对象。

    你想要:

    Sample::query()->where('number_canonical', $number_canonical)->first()

    这将返回一个Sample 模型,您将可以访问所需的关系。

    在你的情况下$sample_record->supposed_origins

    【讨论】:

      【解决方案2】:

      正如@KurtFriars 在他的回答中提到的,我将控制器更改为:

      public function edit(Request $request, $number_canonical)
          {
              $sample_record = Sample::query()->where('number_canonical', $number_canonical)->first();
              $supposed_origins = $sample_record->supposed_origins;
      

      之前的错误消失了,但我还有另一个错误:

      SQLSTATE[42S02]: Base table or view not found: 1146 Table dbname.sample_supposed_origin' doesn't exist`
      

      要拥有我的supposed_origins,还需要像这样改进我的Sample 模型:

          public function supposed_origins() {
              return $this->belongsToMany(
                  'App\SupposedOrigin',
                  'v3_sample_supposed_origin',
                  'sample_id',
                  'supposed_origin_id'
              );
          }
      

      【讨论】:

        猜你喜欢
        • 2018-03-16
        • 2019-10-16
        • 2019-01-29
        • 2018-09-23
        • 2016-04-26
        • 1970-01-01
        • 2017-06-21
        • 2019-11-08
        • 1970-01-01
        相关资源
        最近更新 更多