我有这两种型号的
class Image extends Model
{
public function product(){
return $this->belongsTo(Product::class);
}
}
class Product extends Model
{
public function images()
{
return $this->hasMany(Image::class, 'product_id', 'id');
}
}
我有这两个表通过product_id 链接
This is my product table
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('price');
$table->text('description');
$table->timestamps();
});
}
This is my image table
public function up()
{
Schema::create('images', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('product_id');
$table->string('sm');
$table->string('md');
$table->timestamps();
$table->foreign('product_id')->references('id')->on('products')
->onDelete('cascade');
});
}
当我使用手工修补程序来检查它的工作时,我得到了一个空的结果
$a=新应用程序\产品$a->图像$a::查找(1(
但是如果我从phpmyadmin 运行sql
SELECT * FROM products, images where products.id = images.id AND products.id=1
我得到结果任何帮助感谢
>>> $a->find(1)
=> AppProduct {#3057
id: 1,
title: "1",
price: "1",
description: "1",
pic: "1",
created_at: null,
updated_at: null,
}
>>> $a->images
=> IlluminateDatabaseEloquentCollection {#3050
all: [],
}
这是一个非常简单的
首先执行此命令。composer dump-autoload
然后在你的php artisan tinker;
命令中,你写下这个
$product = AppProduct::find(1);
$product->images;
希望这对你有帮助。