Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.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 Laravel,带连接模型类的mysql查询_Php_Laravel 4 - Fatal编程技术网

Php Laravel,带连接模型类的mysql查询

Php Laravel,带连接模型类的mysql查询,php,laravel-4,Php,Laravel 4,laravel为每个表创建自己的模型。因为它是最受欢迎的框架作品之一,所以我们选择了它。我们的大多数mysql查询都基于多个表,我们使用join并在控制器本身中编写这些查询。有人能告诉我们,如何处理模型类中多个表的查询吗 提前谢谢 您必须在模型类中定义关系,有关关系的信息,请参阅 例如,如果我想访问州名称,请在我的城市页面中: 控制器: 存储库: 城市模式: 有关更多信息,请访问以下网站: 在控制器中,创建构造函数: 然后在您的模型中执行:$this->model->function,以执行您的

laravel为每个表创建自己的模型。因为它是最受欢迎的框架作品之一,所以我们选择了它。我们的大多数mysql查询都基于多个表,我们使用join并在控制器本身中编写这些查询。有人能告诉我们,如何处理模型类中多个表的查询吗


提前谢谢

您必须在模型类中定义关系,有关关系的信息,请参阅

例如,如果我想访问州名称,请在我的城市页面中:

控制器:

存储库:

城市模式:

有关更多信息,请访问以下网站:

在控制器中,创建构造函数:

然后在您的模型中执行:$this->model->function,以执行您的模型函数


看,如果有帮助的话。

嗨,高拉夫,谢谢你的回答。。。若你们能解释一下如何在控制器中获得查询结果,那个将是非常感激的,因为函数是在模型类中声明的,并且它使用关系。。
$city = App::make('Platform\Storage\City\CityRepository')->view($city_id, false, [], ['state'=>array('id', 'name')])
public function view($id, $active=true, $fields=array(), $with=array()) {
        $query = $this->model->newQuery();
        $tableName = $this->model->getTable();

        if ($active) {
            $query->where($tableName . '.is_active', '=', 1);
        }
        if (!$fields) {
            $fields = array($tableName . '.*');
        }
        if ($with) {
            $with = $this->model->processWithSelects($with);
            $query->with($with);
        }

        try {
            return $query->with($with)->where($tableName . '.id', '=', $id)->first($fields);
        }
        catch (Exception $e) {
            throw new DBException($e->getMessage(), $e->getCode(), $e->getPrevious());
        }
    }
public function state() {
    return $this->belongsTo('State');
}
public function __construct(Model $model) {
    parent::__construct();

    $this->model = $model; // Model $model, means IOC or Dependancy Injection
    }