0

这可能是一个愚蠢的问题,但我被困住了..

我基本上想在 laravel 中运行一个简单的选择查询,并在数据库结果中显示单行的结果。

我是 php 和 laravel 的新手,所以尝试在其中使用模型来挂起 MVC。

以下是我所做的。路线

Route::get('groupprofile/(:any)',array('uses'=>'groups@profile'));

控制器 - groups.php

class Groups_Controller extends Base_Controller {
    public $restful=true;
    public function get_profile($groupName){
        $groups=new Groups();
        $groupInfo=$groups->getGroupInfo($groupName);
        return View::make('groups.profile.groupprofile')->with('groupInfo',$groupInfo);
    }
}

模型 - groups.php

class Groups{
    public function getGroupInfo($name){
        return DB::query('select * from Groups where name=?',array($name));
    }
}

查看 - groupprofile.blade.php

@layout('layouts.default')
@section('content')
<h1>This is a profile page for a group.</h1>
@foreach ($groupInfo -> results as $info)
    <br />Here I want to display all columns e.g. name, philosophy, founder name etc.
    <br />$info->Description
@endforeach
<br />Testing end

@endsection

有人可以指导我该怎么做吗?我无法理解如何使用刀片在视图中显示传递的结果集中的数据。

还是我这样做的方法是错误的?我更喜欢编写查询,所以不使用 Eloquent 或 fluent 查询构建器。

4

1 回答 1

2

看看在 Larvel 中使用 Eloquent,它就是 ORM。文档对此非常好。

你的模型应该是 Group.php

<?php class Group extends Eloquent {}

而已!由于我们正在扩展 Eloquent,我们现在可以像这样在视图中拉出一行。

Group::where('name', '=', $somevar)->first()

当然,您可能希望将其存储在控制器中的变量中并将对象传递给您的视图。

class Groups_Controller extends Base_Controller {
    public $restful=true;
    public function get_profile($groupName){
        $groupInfo = Group::where('name', '=', $groupName)->first()
        return View::make('groups.profile.groupprofile')->with('groupInfo',$groupInfo);
    }
}

然后在您的视图中,您可以像这样访问该行的属性(MySQL 列)。

$groupInfo->name
于 2013-05-23T21:36:33.683 回答