Laravel 使用查询显示表记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16723984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:48:27  来源:igfitidea点击:

Laravel display table record using query

phplaravelblade

提问by Yogesh Sawant

This may be a silly question but I'm stuck..

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

I basically want to run a simple select query in laravel and show result from a single row in database result.

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

I'm new to php and laravel so trying to use model in this to get hang on MVC.

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

Following is what I have done. Route

以下是我所做的。路线

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

Controller - groups.php

控制器-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);
    }
}

Model - groups.php

模型-groups.php

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

View - groupprofile.blade.php

查看 - 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

Can someone please guide me how should I do this? I'm not able to understand how to display data from passed result set in view using blade.

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

Or is my approach wrong to do this? I'm more comfortable in writing queries so not using Eloquent or fluent query builder.

或者我这样做的方法是错误的?我更擅长编写查询,因此不使用 Eloquent 或 fluent 查询构建器。

回答by Sajan Parikh

Look at using Eloquent in Larvel, it's the ORM. The docsare very good about this.

看看在 Larvel 中使用 Eloquent,就是 ORM。该文档是关于这个非常好。

Your model should be Group.php

你的模型应该是 Group.php

<?php class Group extends Eloquent {}

That's it! Since we are extending Eloquent, we can now in our view pull a single row like this.

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

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

Of course, you'll probably want to store that in a variable in your controller and pass the object to your view.

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

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);
    }
}

Then in your view, you can access the properties (MySQL columns) of that row like this.

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

$groupInfo->name