BigJobbies
BigJobbies

Reputation: 4033

Laravel 4 - Getting database results into a view

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

Upvotes: 2

Views: 6057

Answers (3)

Pranjal
Pranjal

Reputation: 508

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)

Upvotes: 1

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87719

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

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>

Upvotes: 6

mckendricks
mckendricks

Reputation: 377

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:

Upvotes: 1

Related Questions