Elequoent关系不是选择相关的行

时间:2017-02-12 20:02:03

标签: php laravel-5

我有2个表:productsimages

产品表:

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

我有2个型号,一个产品型号和图像模型,我正在尝试为产品选择图像,但我得到'存在假'

产品型号

<?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';
}

我用来获取所有产品图像的控制器:

public function getProduct(Product $product)
{
    // get the product addons
    $addons = $product->where('product_addon', $product->id)->get();

    dd($product->images());

    return View('pages.product.view-item')->withProduct($product)->withAddons($addons);
}

我的web.php中的路线

Route::get('item/{product}', 'ProductsController@getProduct');

但是当我看到图像被选中时,我会在图像映射下获得以下内容:

  #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的图像,但它是从产品表中选择数据?

1 个答案:

答案 0 :(得分:2)

尝试使用$product->images代替$product->images()

当您将关系称为函数时,它会返回可用于添加其他查询约束的关系查询对象,但它看起来并不像您想要的那样。此时,您仍需要执行查询对象以获取数据。

当您将关系称为关闭模型的属性时,eloquent将自动执行sql并获取数据。

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

$product->images

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