Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 拉雷维尔有很多。我这样做对吗?_Php_Laravel - Fatal编程技术网

Php 拉雷维尔有很多。我这样做对吗?

Php 拉雷维尔有很多。我这样做对吗?,php,laravel,Php,Laravel,我是拉威尔的新手,我正在筹建一家商店。我已经为我的产品形象建立了一种关系,它可以根据我的需要工作,但我只是想检查我是否正确/有效地完成了它。如果任何有过Laravel经验的人都能关注以下内容并提出改进建议或给我一些反馈,我们将不胜感激 我的产品有很多产品图片。以下是我的product和productimage模型: class ProductImage extends Eloquent{ public static $table = 'core_productimages';

我是拉威尔的新手,我正在筹建一家商店。我已经为我的产品形象建立了一种关系,它可以根据我的需要工作,但我只是想检查我是否正确/有效地完成了它。如果任何有过Laravel经验的人都能关注以下内容并提出改进建议或给我一些反馈,我们将不胜感激

我的产品有很多产品图片。以下是我的product和productimage模型:

class ProductImage extends Eloquent{

    public static $table = 'core_productimages';

    public function product(){
        return $this->belongs_to('Product');
    }
}
产品:

class Products extends Eloquent {

    public static $table = 'core_Products';
    ........
    public function ProductImages(){
        return $this->has_many('ProductImage', 'product_id');
    }
}
产品形象:

class ProductImage extends Eloquent{

    public static $table = 'core_productimages';

    public function product(){
        return $this->belongs_to('Product');
    }
}
我的产品控制器(这是我认为我可以更有效地做事的地方。当然我不需要传递产品id来检索图像。模型是否应该已经意识到这一点?)

最后,我的观点是:

foreach ($productImages as $image){
    echo $image->strImageURI . "<br/>";
}
foreach($productImages作为$image){
echo$image->strimageri.“
”; }
在您的产品模型中,您可以调用您的方法
images()
,这样在控制器中,您就可以在产品上调用
->images()
。此外,您的模型应该是单数的,并且不需要像假设的那样在您的has\u many查询中使用
'product\u id'
。它看起来像这样:

产品型号:

class Product extends Eloquent {

    public static $table = 'core_Products';

    // ...

    public function images() {

        return $this->has_many('ProductImage');
    }
}
ProductImage模型:

class ProductImage extends Eloquent {

    public static $table = 'core_productimages';

    public function product() {

        return $this->belongs_to('Product');
    }
}
产品控制员:

class Store_Product_Controller extends Base_Controller {

    public function action_index($serfURL = "", $intSectionID, $pSerfURL = "", $intProductID) {

        // ...

        $view->product = Product::find($intProductID);

        return $view;       
    }

}
产品视图:

foreach ($product->images as $image) {

    echo $image->strImageURI . "<br/>";
}
foreach($product->images as$image){
echo$image->strimageri.“
”; }
我知道这不是一个答案,但你最好把这个问题定位在Laravel社区——www.Laravel.io;-)我没有看到任何错误,只是认为您不需要
->get()
,因为
有很多
已经返回了图像数组。CMIIW除非是打字错误,否则您的模型应该是单一的,即
产品
我将使用刀片模板解决方案的强大功能查看该视图,但您所拥有的似乎很好。我最后要说的是,看看如何使用RESTful控制器,这对我来说更有意义。这是我的$0.02:)发布您的数据库模型也会很有用。还有