Laravel属于和有很多不返回数据

时间:2018-12-30 03:58:23

标签: php laravel laravel-5.7

我是laravel / eloquent的新手,到目前为止非常喜欢它们,但是我在获取belongsTo和让很多人工作方面遇到困难。 我已经阅读了无数其他人遇到过的问题,但是还没有找到可以为我解决问题的解决方案。

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

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();
});

这是我的Page模型:

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()和category()函数换成类似这些的函数,则效果很好:

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();

2 个答案:

答案 0 :(得分:0)

尝试一下。

    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();
});

答案 1 :(得分:0)

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

$page->categories称为属性给了我数据

作为{strong>函数调用$page->categories()返回一个可以继续链接其他命令的对象。

万一它对任何人都有帮助,我可以通过回顾非常有用的laracasts视频找到解决方案。具体来说,我刚刚发表的关于雄辩性关系的插曲清楚地说明了这一点: https://laracasts.com/series/laravel-from-scratch-2018/episodes/16