I am studying the Laravel PHP structure based on the Model, View, Controller paradigm. I'm stuck trying to incorporate AJAX into my simple starter app ... phonecall registrar. Here I usually give up. But I refuse!
So I have a Phonecall model:
class Phonecall extends Eloquent {
Here is my phonecalls table:
mysql> desc phonecalls; +------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +------------+------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | who | varchar(200) | NO | | NULL | | | what | varchar(200) | NO | | NULL | | | created_at | datetime | NO | | NULL | | | updated_at | datetime | NO | | NULL | | | initiator | varchar(200) | NO | | NULL | | | info | text | NO | | NULL | | +------------+------------------+------+-----+---------+----------------+ 7 rows in set (0.00 sec)
My view lists all the calls (by whom and by whom) currently in the database:
<!doctype html> <html> <head> <title>Title</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"> </script> </head> <body> <h1>Welcome</h1> <p>Here a list of recent phonecalls</p> <ul class="call-list"> @foreach($phonecalls as $call) <li>{{ $call->who }} - {{ $call->what }} - <a href="phonecalls/show/{{ $call->id }}">Show</a> | {{ HTML::link('phonecalls/delete/'.$call->id, 'Delete') }} | {{ HTML::link('phonecalls/update/'.$call->id, 'Update') }} </li> </ul> {{-- Placeholder for AJAX content --}} <div id="call-info"> </div> </body> </html>
Here is my simple controller:
class Phonecalls_Controller extends Base_Controller { // Use $this within a method the same way you // would use the object name outside of the class public function get_index() { $phonecalls = Phonecall::all(); return View::make('phonecalls.index')->with('phonecalls', $phonecalls); } // ************************************ // DISPLAY CALL INFO public function get_show($call_id) { $call = Phonecall::find($call_id); // WHAT GOES HERE? }
I want the user to be able to click "Show" in the view and have the call information display within ...
<div id="call-info"> </div>
in view.
UPDATE:
Here is what he did ...
My get_show () method
// ************************************ // SHOW CALL INFO public function get_show($call_id) { $call = Phonecall::find($call_id); return $call->info; }
My javascript
//************************************ // Phonecall AJAX Example //************************************ $(document).ready(function() { $('.call-list > li > a').click(function(e) { // e=event e.preventDefault(); var id = $(this).attr('id'); $.get(BASE+'/phonecalls/show/'+id, function(data) { $("#call-info").html(data); }) });