Php Elequoent关系未选择相关行

Php Elequoent关系未选择相关行,php,laravel-5,Php,Laravel 5,我有两个表:产品和图像 产品表: CREATE TABLE `products` ( `id` int(10) UNSIGNED NOT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `deleted_at` timestamp NULL DEFAULT NULL, `name` varchar(191) COLLATE utf8mb4_uni

我有两个表:
产品
图像

产品表:

CREATE TABLE `products` (
  `id` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `deleted_at` timestamp NULL DEFAULT NULL,
  `name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL,
  `short_description` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` text COLLATE utf8mb4_unicode_ci NOT NULL,
  `price_standard` int(11) NOT NULL,
  `price_life` int(11) NOT NULL,
  `price_multi` int(11) NOT NULL,
  `product_addon` int(11) NOT NULL,
  `banner_style` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT 'blue'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `products`
--

INSERT INTO `products` (`id`, `created_at`, `updated_at`, `deleted_at`, `name`, `short_description`, `description`, `price_standard`, `price_life`, `price_multi`, `product_addon`, `banner_style`) VALUES
(16, '2017-02-12 19:25:03', '2017-02-12 19:25:03', NULL, 'tet', 'test', 'test', 100, 200, 330, 0, 'blue');
图像表

CREATE TABLE `images` (
  `id` int(10) UNSIGNED NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  `product_id` int(11) NOT NULL,
  `image_name` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `images`
--

INSERT INTO `images` (`id`, `created_at`, `updated_at`, `product_id`, `image_name`) VALUES
(1, '2017-02-12 19:25:03', '2017-02-12 19:25:03', 16, '58a0b68fd1d1b.jpg');
我有两个模型,一个是产品模型,另一个是图像模型,我试图为产品选择图像,但我得到的是“exists false”

产品型号

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Product extends Model
{
    public function features()
    {
        return $this->hasMany('App\ProductFeatures');
    }

    public function images()
    {
        return$this->hasMany('App\Images', 'product_id');
    }
}
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Images extends Model
{
    protected $table = 'images';
}
以及myweb.php中的路径

Route::get('item/{product}', 'ProductsController@getProduct');
但是,当我在diedump中查看是否选择了图像时,我在图像映射下得到以下信息:

  #related: Images {#219 ▼
    #table: "images"
    #connection: null
    #primaryKey: "id"
    #keyType: "int"
    +incrementing: true
    #with: []
    #perPage: 15
    +exists: false
    +wasRecentlyCreated: false
    #attributes: []
    #original: []
    #casts: []
    #dates: []
    #dateFormat: null
    #appends: []
    #events: []
    #observables: []
    #relations: []
    #touches: []
    +timestamps: true
    #hidden: []
    #visible: []
    #fillable: []
    #guarded: array:1 [▶]
  }

为什么不是为产品16选择图像,而是从产品表中选择数据?

尝试
$product->images
而不是
$product->images()

当您将关系作为函数调用时,它将返回关系查询对象,您可以使用该对象添加额外的查询约束,但这似乎不是您想要的。但此时仍然需要执行查询对象来获取数据

当您将关系作为属性从模型中调用时,eloquent将自动执行sql并获取数据

所以这两条线应该产生相同的结果

$product->images

$product->images()->get()

我想出来了。我是这样启动的:$product->images()->,而不是$product->image->,后者获得项目。。。因此,如果有人想在回答中解释原因,以防将来有人在同一件事上挣扎。我认为这是最好的答案。(给出一个好的答案,不是几行)
$product->images

$product->images()->get()