How to Iterate and Render Database Data in Phoenix LiveView
From ElixirBlocks
This page is in progress
Before You Begin
Installing Phoenix
This article assumes that you have installed the Phoenix web framework and all its dependencies correctly without errors. If you have not installed the Phoenix web framework please view the documentation here
Example
This code references a PostgreSQL table named TestBeds.
Before
defmodule AppWeb.PageLive do
use AppWeb, :live_view
def mount(_params, _session, socket) do
{:ok, socket}
end
def render(assigns) do
~H"""
Hello World! YAY
"""
end
end
After
defmodule AppWeb.PageLive do
use AppWeb, :live_view
alias App.TestBeds #This is how you pull the database table into your app
def mount(_params, _session, socket) do
{:ok, assign(socket, testbeds: TestBeds.list_testbeds())}
end
def render(assigns) do
~H"""
<%= for testbed <- @testbeds do %>
<div><%= testbed.name %></div>
<% end %>
"""
end
end