samedi 18 avril 2015

How to return two SQL tables in one result- Laravel

I want to return two SQL tables to single result array. I tried to use array_merge(), but it seems that I am missing an important point.


Here is a route '/' function



Route::get('/', function()
{
$items = get_items();
return View::make('posts')->withItems($items);
});

function get_items()
{

$sql = "select postlist.* from postlist order by postlist.id desc ";
$items = DB::select($sql);
$sql1 = "select count (commentlist.id) as 'comments',commentlist.id,commentlist.name,commentlist.comment from postlist,commentlist where postlist.id=commentlist.post_id";
$items1 = DB::select($sql1);

$items = array_merge($items, $items1);

return $items;

}


Blade.php file where I retrieve records.



@if ($items)

<table class="bordered">
<thead>
<tr><th>Icon</th><th>Title</th><th>Post</th><th>Name</th><th>Update Post </th><th>Delete Post </th><th>View Comments</th></tr>
<tbody>
@foreach ($items as $item)
<tr>
<td><img class='photo' src='image/images.png' alt='photo' style="height:25px; width:35px;" ></td>
<td>{{{ $item->title }}} </td>
<td> {{{ $item->post }}} </td>
<td>{{{ $item->name }}}</td>
<td><a href="{{{ url("update_item/$item->id") }}}">Update</a></td>
<td><a href="{{{ url("delete_item_action/$item->id") }}}">Delete</a></td>
<td><a href="{{{ url("comments_item/$item->id") }}}">Comments</a></td>

</tr>

@endforeach
</tbody>
</table>
@else
<p>No items found.</p>
@endif
@stop

Aucun commentaire:

Enregistrer un commentaire