2

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

我的模型中有以下功能

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

}

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

我有点失落。

谢谢

4

3 回答 3

6

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

public function getPrivateMessages()
{

 ...

    return $messages;

}

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

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

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

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

<html><body>
    @foreach($privateMessages as $privateMessage)
        {{$privateMessage->text}}
    @endforeach
</body></html>
于 2013-06-15T06:26:02.313 回答
1

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

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

Note that MyModel should 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.

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_one and message, you'd want to use the names of whatever columns you have in the database).

For more info see these sections of the docs:

于 2013-06-15T06:44:50.030 回答
1

在你的视野内

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

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

于 2014-07-02T18:36:20.323 回答