I have 2 tables: products and images
Products table:
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');
Image table
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');
I have 2 models, a products model and the images model, I am trying to select the images for the products, but I am getting 'exists false'
Products Model
<?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');
}
}
Images model
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Images extends Model
{
protected $table = 'images';
}
The controller I am using to get all the products 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);
}
And the route in my web.php
Route::get('item/{product}', 'ProductsController@getProduct');
But when I diedump to see if the image is being selected I get the following under the image mapping:
#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 [▶]
}
How come it is not selecting the images for the product 16, but it is selecting the data from the products table?
from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2klmD08
via IFTTT
Aucun commentaire:
Enregistrer un commentaire