douh9817 2015-12-09 18:38
浏览 78
已采纳

在laravel中获得一对多的记录

What am I trying?

Get a particular category and it's associated Sub Categories


Category Model

class Category_Model extends Model
{
    protected $table = "tblcategory";
    protected $primaryKey = "CategoryID";
    public $timestamps = false;

    public function Subcategories()
    {
        return $this->hasMany('\App\Models\Skill\SubCategory_Model');
    }
}

Sub Category Model

class SubCategory_Model extends Model
{
    protected $table = "tblsubcategory";
    protected $primaryKey = "SubCategoryID";
    public $timestamps = false;
}

Action Method

public function SubCategories($category)
{
    $Categories = \App\Models\Skill\Category_Model
                  ::where("Category", "=", $category)
                  ->Subcategories;
    dd($Categories);
}

When I run the code. I get below error

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tblsubcategory.category__model_id' in 'where clause' (SQL: select * from tblsubcategory where tblsubcategory.category__model_id = 1 and tblsubcategory.category__model_id is not null)

  • 写回答

1条回答 默认 最新

  • dongwende1984 2015-12-09 18:53
    关注

    From the comments, it is most likely that your subcategory table doesn't have a category_model_id but likely a category_id. By default, Laravel tries to extrapolate the name of the foreign column from the model name (in this case Category_Model, which explains the category_model_id. Changing the class to:

    class Category_Model extends Model
    {
        protected $table = "tblcategory";
        protected $primaryKey = "CategoryID";
        public $timestamps = false;
    
        public function Subcategories()
        {
            return $this->hasMany('\App\Models\Skill\SubCategory_Model', 'category_id')->get(); // Or whatever the column is actually called
        }
    }
    

    should solve the issue.

    To return both the category object and its subcategories, change the action code to:

    public function SubCategories($category)
    {
        $Categories = \App\Models\Skill\Category_Model
                      ::where("Category", "=", $category)
                      ->with("Subcategories")
                      ->first();
        dd($Categories);
    } 
    

    The $Categories should also now contain a Subcategories object, accessed via $Categories->Subcategories, which should return a collection of Subcategory objects. If you wanted to see each one, you would loop through with a foreach

    foreach($Categories->Subcategories AS $Subcategory){
      echo $Subcategory->name;
      // etc etc.
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?