Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Took me a while to figure out, but this is what I've ended up with.<br> This simply returns some html constructed in a view from a database query based on a single value (userid) passed into a controller method via jQuery ajax.<br> Eg: The user uses Bootstrap Typeahead to select a user, then (via ajax) gets the user's details from the database and displays then.</p> <h2>The AJAX:</h2> <pre><code>var details = '&lt;?php echo URL::to('details'); ?&gt;'; $.ajax( { url: details, type: 'GET', dataType: 'html', data: {id: selected}, }).done( function(data) { $('#details').html(data); } ); </code></pre> <h2>The Controller:</h2> <pre><code>public function Details() { $id = Input::get('id'); $user = DB::table('Users')-&gt;where('UserID', $id)-&gt;get(); $data = array('user' =&gt; $user); return View::make('details', $data); } </code></pre> <h2>The View:</h2> <pre><code>@foreach($user as $person) &lt;label class="key"&gt;User ID&lt;/label&gt; &lt;label class="data"&gt;{{ $person-&gt;UserID }}&lt;/label&gt; &lt;label class="key"&gt;Person&lt;/label&gt; &lt;label class="data"&gt;{{ $person-&gt;FullName, $person-&gt;Email, $person-&gt;WebSite }}&lt;/label&gt; @endforeach </code></pre> <h2>Notes:</h2> <ul> <li>All my views are blade templates.</li> <li>My details view cannot inherit from another template (no extends('master')), this stops the view from working (no idea why).</li> <li>I'll only ever be returning 1 record, I use @foreach to access the array and turn each element into an object (there may be a better way of doing this).</li> </ul>
 

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