Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing AJAX in Laravel to interact with database
    text
    copied!<p>I'm learning the Laravel PHP framework, based on the Model, View, Controller paradigm. I'm stuck on trying to incorporate AJAX into my simple starter application...a phonecall logger. This is where I normally give up. But I refuse!</p> <p>So I have my Phonecall Model:</p> <pre><code>class Phonecall extends Eloquent { // Creates an instance of the database object } </code></pre> <p>Here's my phonecalls table:</p> <pre><code>mysql&gt; 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) </code></pre> <p>My View lists all calls (by who and what) currently in the database:</p> <pre><code>&lt;!doctype html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;Title&lt;/title&gt; &lt;script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"&gt; &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Welcome&lt;/h1&gt; &lt;p&gt;Here's a list of recent phonecalls&lt;/p&gt; &lt;ul class="call-list"&gt; @foreach($phonecalls as $call) &lt;li&gt;{{ $call-&gt;who }} - {{ $call-&gt;what }} - &lt;a href="phonecalls/show/{{ $call-&gt;id }}"&gt;Show&lt;/a&gt; | {{ HTML::link('phonecalls/delete/'.$call-&gt;id, 'Delete') }} | {{ HTML::link('phonecalls/update/'.$call-&gt;id, 'Update') }} &lt;/li&gt; &lt;/ul&gt; {{-- Placeholder for AJAX content --}} &lt;div id="call-info"&gt; &lt;/div&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Here's my simple Controller:</p> <pre><code> 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')-&gt;with('phonecalls', $phonecalls); } // ************************************ // DISPLAY CALL INFO public function get_show($call_id) { $call = Phonecall::find($call_id); // WHAT GOES HERE? } </code></pre> <p>I want the user to be able to click on "Show" in the view and have the call info display within the...</p> <pre><code>&lt;div id="call-info"&gt; &lt;/div&gt; </code></pre> <p>tags in the View. </p> <hr> <h2>UPDATE:</h2> <p>Here's what did it...</p> <p><strong>My get_show() method</strong></p> <pre><code>// ************************************ // SHOW CALL INFO public function get_show($call_id) { $call = Phonecall::find($call_id); return $call-&gt;info; } </code></pre> <p><strong>My Javascript</strong></p> <pre><code>//************************************ // Phonecall AJAX Example //************************************ $(document).ready(function() { $('.call-list &gt; li &gt; a').click(function(e) { // e=event e.preventDefault(); var id = $(this).attr('id'); $.get(BASE+'/phonecalls/show/'+id, function(data) { $("#call-info").html(data); }) }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload