Laravel - 雄辩的关系不起作用一对多的关系

穆斯图法

我有两个具有一对多关系的模型。我想在刀片中显示具有关系的数据。

产品表

Table name = Products
PrimaryKey = pro_id
ForeignKey = cat_id

类别表

Table name = categories
PrimaryKey = cat_id

产品型号代码

namespace App;

use Illuminate\Database\Eloquent\Model;

class productsModel extends Model
{
    //code...
    protected $table      = 'products';
    protected $primaryKey = 'pro_id';

    // Every Products Belongs To One Category

    public function category()
    {
        # code...
        return $this->belongsTo('APP\abcModel','cat_id');
    }
}

类别 型号代码

namespace App;

use Illuminate\Database\Eloquent\Model;

class categoryModel extends Model
{
    //code...
    protected $table      = 'categories';
    protected $primaryKey = 'cat_id';

    // One Category Has Many Products

    public function products()
    {
        # code...
        return $this->hasMany('App\productsModel','cat_id','pro_id');
    }
}

控制器代码

命名空间 App\Http\Controllers;

use Illuminate\Http\Request;
use App\productsModel;
class productsController extends Controller
{
    //code...
    public function products($category_id='')
    {
        # code...
        $data["products"] = productsModel::where
                            ('cat_id',$category_id)
                            ->get();        
        $data["categories"] = productsModel::where
                            ('cat_id',$category_id)->first()->category;
        echo "<pre>";
        print_r($data);
        echo "</pre>";
    }
}

错误: Symfony\Component\Debug\Exception\FatalThrowableError 类 'APP\categoryModel' 未找到

帕特里克·阿拉尔特

似乎有时您有App,有时APP,而 PHP 对类名不区分大小写,您可能会使用在文件名方面区分大小写的操作系统(Linux?)。

我建议只App在任何地方都有,您的错误消息清楚地表明:APP.

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章