Laravel 4 - 将数据库结果放入视图中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17120718/
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:58:17  来源:igfitidea点击:

Laravel 4 - Getting database results into a view

laravellaravel-4

提问by BigJobbies

Im having a bit of trouble learning to get my data into my view, and i was hoping someone could help me.

我在学习让我的数据进入我的视野时遇到了一些麻烦,我希望有人能帮助我。

I have the following function in my model

我的模型中有以下功能

public function getPrivateMessages()
{

    $userId = Auth::user()->id;

    $messages = DB::table('pm_conversations')
                    ->where(function($query) use ($userId) {
                        $query->where('user_one', $userId)
                            ->where('user_one_archived', 0);
                    })
                    ->orWhere(function($query) use ($userId) {
                        $query->where('user_two', $userId)
                            ->where('user_two_archived', 0)
                    })
                    ->get();

}

How would i pass it to my controller, then into my view?

我如何将它传递给我的控制器,然后进入我的视图?

Im a bit lost.

我有点失落。

Thanks

谢谢

回答by Antonio Carlos Ribeiro

Assuming that this is your Conversation model, you need to return those messages you queried:

假设这是您的 Conversation 模型,您需要返回您查询的那些消息:

public function getPrivateMessages()
{

 ...

    return $messages;

}

Use it in your controller to pass to your View:

在您的控制器中使用它传递给您的视图:

class HomeController extends Controller
{
    public function index()
    {
        $conversation = Conversation::find(1);

        return View::make('index')->with('privateMessages', $conversation->getPrivateMessages());
    }
}

And in your view show whatever you need to:

并在您看来显示您需要的任何内容:

<html><body>
    @foreach($privateMessages as $privateMessage)
        {{$privateMessage->text}}
    @endforeach
</body></html>

回答by mckendricks

In your controller, you would call this in one of your actions:

在您的控制器中,您可以在其中一项操作中调用它:

$pms = MyModel->getPrivateMessages();
return View::make('layout')
    ->with('pms', $pms);

Note that MyModelshould be replaced with the actual name of your model. The ->with('pms',$pms)bit says, pass the contents of the variable $pms to the 'layout' view and assign it to a variable named 'pms' in that view. Feel free to customize the name of the view to match whatever view you want to use and pick different variable names if you are so inclined.

请注意,MyModel应替换为您模型的实际名称。该->with('pms',$pms)位表示,将变量 $pms 的内容传递给 'layout' 视图,并将其分配给该视图中名为 'pms' 的变量。随意自定义视图的名称以匹配您想要使用的任何视图,如果您愿意,可以选择不同的变量名称。

Then, in your view you would use it like this:

然后,在您看来,您会像这样使用它:

@foreach($pms as $pm)
    <p>From: {{ $pm->user_one}}</p>
    <p>{{ $pm->message }}</p>
@endforeach

Here, we're just looping over each of the private messages and outputting a few fields (user_oneand message, you'd want to use the names of whatever columns you have in the database).

在这里,我们只是遍历每条私人消息并输出一些字段(user_one并且message,您希望使用数据库中任何列的名称)。

For more info see these sections of the docs:

有关更多信息,请参阅文档的这些部分:

回答by Pranjal

inside your view

在你的视野中

<?php $var=DB::table('tablename')->get(); ?>
@foreach($var as $variable)
   {{ $variable->tablefield }}
@endforeach

here we are accessing the table named 'tablename' from our database (abbrievated as DB) and then accessing all the columns of the table through get method. Then we are storing them in a random variable (say var so the that we can loop it easily). And then we are simply looping through to print our column data(as in the case above)

在这里,我们从我们的数据库(缩写为 DB)中访问名为 'tablename' 的表,然后通过 get 方法访问该表的所有列。然后我们将它们存储在一个随机变量中(比如 var 以便我们可以轻松地循环它)。然后我们只是循环打印我们的列数据(如上例所示)