0

chatcontroller.php function returns variable to view:    
public function getChat()
    {
        $message = chat_messages::all();                    
        return View::make('home',compact($message));
    }   
this is my route:
Route::get('/getChat', array('as' => 'getChat','uses' => 'ChatController@getChat'));                                                                      
this is my home.blade.php:                                                           

@extends('layouts.main')
@section('content')
    <div class="container">
        <h2>Welcome to Home Page!</h2>
        <p> <a href="{{ URL::to('/logout') }}" > Logout </a></p>
        <h1>Hello <span id="username">{{ Auth::user()->username }} </span>!</h1>
        <div id="chat_window">
                
        </div>
        <input type="text" name="chat" class="typer" id="text" autofocus="true" onblur="notTyping()">
       
    </div>
<ul>
@foreach($message as $msg)
<li>
{{ $msg['sender_username']," says: ",$msg['message'],"<br/>" }}
</li>
@endforeach
</ul>
<script src="{{ asset('js/jquery-2.1.3.min.js') }}"></script>
<script src="{{ asset('js/chat.js') }}"></script>

@stop                                     

I am trying to send result returned by select query to view from controller.

when I do this from homecontroller.php then it works fine. if I try to pass from controller which I have defined it gives error message as:Undefined variable.

I have used the extends \BaseController do i need to do anything else to access my controller variable from view.

please suggest some tutorial if possible for same.

4
  • Probably your route is not accessing your defined controller. You must change it to use the new controller instead of the HomeController Commented Apr 22, 2015 at 6:57
  • Can you provide some code? Commented Apr 22, 2015 at 7:23
  • The problem should be in your routes. Show us some code, plz. Commented Apr 22, 2015 at 10:05
  • chatcontroller.php function returns variable to view: Commented Apr 24, 2015 at 5:36

3 Answers 3

1

Verify the route to be sure it uses the new controller:

Route::get('user/profile', array('uses' => 'MyDefinedController@showProfile'));
Sign up to request clarification or add additional context in comments.

Comments

1

First of all check your routes, as Matei Mihai says. There are two different ways to pass data into your view;

$items = Item::all();

// Option 1
return View::make('item.index', compact('items'));

// Option 2
return View::make('item.index')->with('items', $items); // same code as below

// Option 3
View::share('item.index', compact('items'));
return View::make('item.index);

Comments

0

You can also do this:

$this->data['items'] = Item::all();

return View::make('item.index', $this->data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.