理解雄辩的关系

时间:2016-08-05 06:27:36

标签: eloquent relational-database laravel-5.2 has-many

请我真的想了解Eloquent的关系,因为我在这个应用程序中遇到了很多麻烦我在建立目录样式部分,我想要实现的是显示每个样式的两个图像 catalog @页面上的样式,即两个图像上传(前)和(后)图像,但默认情况下它会显示前视图,截图将帮助您更好地理解

截图

catalogue styles list

第一张图片显示了带有默认前视图的样式列表,但是当查看更多时,它应该在幻灯片中显示字体和背面图像。

catalogue style viewmore

此图片显示点击特定样式以查看更多内容,前后图像会滑动。

问题

我希望能够在查看更多时显示正面和背面图像,但它显示的是不同身份的不同图像。

现有代码

$catalogueStyles = DB::table('catalogueimages')
->select('catalogueimages.id',
    'catalogueimages.user_id',
    'catalogueimages.catalogue_style_id',
    'catalogueimages.catalogue_style_view',
    'catalogueimages.new_name',
    'catalogueimages.image_path',
    'cataloguestyles.id',
    'cataloguestyles.user_id',
    'cataloguestyles.catalogue_style_name',
    'cataloguestyles.catalogue_style_description',
    'cataloguestyles.catalogue_style_tags',
    'cataloguestyles.catalogue_style_rating',
    'cataloguestyles.catalogue_style_publisher_name',
    'cataloguestyles.catalogue_style_publisher_url')
  ->join('cataloguestyles','cataloguestyles.id','=','catalogueimages.catalogue_style_id')
  ->where('catalogue_style_view',0)
  ->get();

我想将 cataloguestyles 表与 catalogueimages 表联系起来,以便将它们组合起来并从数据库中提取数据,我已经尝试过这个以进行测试

Cataloguestyle模型

<?php

namespace App\Wasamar;

use Illuminate\Database\Eloquent\Model;

class Cataloguestyle extends Model
{
    /**
             * The database table used by the model.
             *
             * @var string
     */

    protected $table = 'cataloguestyles';

    function catalogueimage(){

            return $this>hasMany('App\Wasamar\Catalogueimage','catalogue_style_id');
    }
}

目录图像模型

<?php

namespace App\Wasamar;

use Illuminate\Database\Eloquent\Model;

class Catalogueimage extends Model
{
/**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'catalogueimages';


    function cataloguestyle(){

        return $this->belongsTo('App\Wasamar\Cataloguestyle','id');
    }
}

当我在目录样式上尝试hasMany函数并尝试获取值时,我收到此错误

routes.php第246行中的

FatalErrorException:在null上调用成员函数catalogueimage()

0 个答案:

没有答案
相关问题