【问题标题】:Laravel belongsTo and hasMany not returning dataLaravel belongsTo 和 hasMany 不返回数据
【发布时间】:2018-12-30 12:15:27
【问题描述】:

我是 laravel/eloquent 的新手,到目前为止非常喜欢它们,但我无法让 belongsTo 和 hasMany 工作。 我已经阅读了其他人遇到的无数问题,但没有找到适合我的解决方案。

我有一个“页面”模型和一个“类别”模型。以下是表格的定义:

Schema::create('pages', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->timestamps();
});

Schema::create('categories', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('page_id');
    $table->foreign('page_id')->references('id')->on('pages');
    $table->string('name');
    $table->timestamps();
});

这是我的页面模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Page extends Model
{
    public function categories()
    {
        return $this->hasMany('App\Category');
    }
}

这是我的类别模型:

namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public function page() 
    {
        return $this->belongsTo('App\Page');
    }
}

以下是一些修补的结果:

> php artisan cache:clear
Application cache cleared!
> php artisan tinker
>>> \App\Category::find(1)
=> App\Category {#2916
     id: "1",
     page_id: "1",
     name: "First Category",
     created_at: "2018-12-30 03:18:07.347",
     updated_at: "2018-12-30 03:18:07.347",
   }
>>> \App\Category::find(1)->page()
=> Illuminate\Database\Eloquent\Relations\BelongsTo {#2918}
>>> \App\Page::find(1)
=> App\Page {#2902
     id: "1",
     name: "First Page",
     created_at: "2018-12-30 03:18:07.220",
     updated_at: "2018-12-30 03:18:07.220",
   }
>>> \App\Page::find(1)->categories()
=> Illuminate\Database\Eloquent\Relations\HasMany {#2922}
>>>

我得到我的对象就好了,但是当我尝试拉关系时,我得到的只是看起来像空的对象:

=> Illuminate\Database\Eloquent\Relations\BelongsTo {#2918}

=> Illuminate\Database\Eloquent\Relations\HasMany {#2922}

如果我将我的 page() 和 categories() 函数换成这样的函数,一切都会很好:

public function page() 
{
    return \App\Page::find($this->page_id);
}

public function categories()
{
    return \App\Category::where('page_id', $this->id)->get();
}

以下是这些结果:

> php artisan cache:clear
Application cache cleared!
> php artisan tinker
Psy Shell v0.9.9 (PHP 7.3.0 — cli) by Justin Hileman
>>> \App\Page::find(1)
=> App\Page {#2916
     id: "1",
     name: "First Page",
     created_at: "2018-12-30 03:18:07.220",
     updated_at: "2018-12-30 03:18:07.220",
   }
>>> \App\Page::find(1)->categories()
=> Illuminate\Database\Eloquent\Collection {#2918
     all: [
       App\Category {#2921
         id: "1",
         page_id: "1",
         name: "First Category",
         created_at: "2018-12-30 03:18:07.347",
         updated_at: "2018-12-30 03:18:07.347",
       },
       App\Category {#2922
         id: "2",
         page_id: "1",
         name: "Second Category",
         created_at: "2018-12-30 03:18:07.480",
         updated_at: "2018-12-30 03:18:07.480",
       },
       App\Category {#2923
         id: "3",
         page_id: "1",
         name: "Third Category",
         created_at: "2018-12-30 03:18:07.623",
         updated_at: "2018-12-30 03:18:07.623",
       },
     ],
   }
>>> \App\Category::find(1)
=> App\Category {#2904
     id: "1",
     page_id: "1",
     name: "First Category",
     created_at: "2018-12-30 03:18:07.347",
     updated_at: "2018-12-30 03:18:07.347",
   }
>>> \App\Category::find(1)->page()
=> App\Page {#2921
     id: "1",
     name: "First Page",
     created_at: "2018-12-30 03:18:07.220",
     updated_at: "2018-12-30 03:18:07.220",
   }
>>>

编辑:

我发现如果我添加->get(),我会在修补时看到数据,正如我所期望的那样:

public function page() 
{
    return $this->belongsTo('App\Page')->get();
}

但是,我所见过的例子都没有显示有人这样做。我只是误解了这应该如何工作吗?我希望在修补时只做 $page->categories();会给我类别而不需要做 $page->categories()->get();

【问题讨论】:

    标签: php laravel laravel-5.7


    【解决方案1】:

    试试这个。

        Schema::create('categories', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('page_id')->unsigned();
        $table->foreign('page_id')->references('id')->on('pages');
        $table->string('name');
        $table->timestamps();
    });
    

    【讨论】:

    • 不幸的是,这没有帮助。还是)感谢你的建议!为清楚起见,您唯一更改的是将 ->unsigned() 添加到 page_id 吗?
    【解决方案2】:

    啊,像往常一样,我发现这是一个简单的解决方法。

    $page->categories 称为属性会为我提供数据

    $page->categories() 作为函数 调用会返回一个对象,我可以继续将更多命令链接到该对象。

    如果它对任何人有帮助,我通过重温非常有用的 laracasts 视频找到了解决方案。具体来说,我刚刚发布的关于雄辩关系的插曲得到了明确的解释: https://laracasts.com/series/laravel-from-scratch-2018/episodes/16

    【讨论】:

      猜你喜欢
      • 2018-04-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多