<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Calender &#8211; Web Development &amp; Digital Marketing Agency </title>
	<atom:link href="https://darkgrey-chimpanzee-552275.hostingersite.com/blog/tag/calender/feed/" rel="self" type="application/rss+xml" />
	<link>https://darkgrey-chimpanzee-552275.hostingersite.com</link>
	<description>KeyTech </description>
	<lastBuildDate>Sun, 06 Aug 2023 10:47:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://darkgrey-chimpanzee-552275.hostingersite.com/wp-content/uploads/2023/07/cropped-logo-32x32.png</url>
	<title>Calender &#8211; Web Development &amp; Digital Marketing Agency </title>
	<link>https://darkgrey-chimpanzee-552275.hostingersite.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Build a Dynamic Calendar with Livewire: A Step-by-Step Guide</title>
		<link>https://darkgrey-chimpanzee-552275.hostingersite.com/blog/dynamic-calendar-with-livewire/</link>
		
		<dc:creator><![CDATA[KeyTech Developer]]></dc:creator>
		<pubDate>Fri, 04 Aug 2023 18:17:41 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel Livewire]]></category>
		<category><![CDATA[Calender]]></category>
		<category><![CDATA[Livewire]]></category>
		<category><![CDATA[PHP]]></category>
		<guid isPermaLink="false">https://darkgrey-chimpanzee-552275.hostingersite.com/blog/build-a-dynamic-calendar-with-livewire-a-step-by-step-guide/</guid>

					<description><![CDATA[Livewire is a tool that helps developers build websites and web applications. It&#8217;s like a set of building blocks that makes it easier and faster...]]></description>
										<content:encoded><![CDATA[<div>
<h2>Livewire is a tool that helps developers build websites and web applications. It&#8217;s like a set of building blocks that makes it easier and faster for developers to create websites that look and feel modern and dynamic.</h2>
<p>The key benefit of Livewire is that it allows developers to build these dynamic websites without having to write as much complicated code. This means that developers can spend less time worrying about the technical details and more time focusing on the user experience and design of the website.</p>
<p>Today, Let&#8217;s learn how can we build a calendar using Livewire.</p>
<p>To build a live calendar using Livewire in Laravel, you can follow these steps:</p>
<p>Step 1: Install Livewire First, you need to install Livewire in your Laravel application if you haven&#8217;t already done so. You can do this using Composer by running the following command in your terminal:</p>
<pre><code class="lang-bash">composer require livewire/livewire
</code></pre>
<p>Step 2: Create a new Livewire component Next, create a new Livewire component that will handle the calendar functionality. You can do this by running the following command in your terminal:</p>
<pre><code class="lang-bash">php artisan make:livewire Calendar
</code></pre>
<p>This will create a new Livewire component called <code>Calendar</code> in the <code>app/Http/Livewire</code> directory.</p>
<p>Step 3: Define properties and methods Define some properties in the <code>Calendar</code> component to store the current month and year, as well as the events that are scheduled for the selected date. Add the following code to the <code>Calendar</code> component:</p>
<pre><code class="lang-bash">class Calendar extends Component
{
    public <span class="hljs-variable">$month</span>;
    public <span class="hljs-variable">$year</span>;
    public <span class="hljs-variable">$events</span> = [];

    public <span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">mount</span></span>()
    {
        <span class="hljs-variable">$this</span>-&gt;month = date(<span class="hljs-string">'m'</span>);
        <span class="hljs-variable">$this</span>-&gt;year = date(<span class="hljs-string">'Y'</span>);
    }

    public <span class="hljs-keyword">function</span> getEventsForDate(<span class="hljs-variable">$date</span>)
    {
        // Get the events <span class="hljs-keyword">for</span> the selected date and store them <span class="hljs-keyword">in</span> the <span class="hljs-variable">$events</span> property
    }

    public <span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">render</span></span>()
    {
        // Render the calendar view with the current month and year
    }
}
</code></pre>
<p>In the <code>mount()</code> method, we set the <code>$month</code> and <code>$year</code> properties to the current month and year.</p>
<p>The <code>getEventsForDate()</code> method will be used to fetch the events for the selected date and store them in the <code>$events</code> property.</p>
<p>The <code>render()</code> method will be used to render the calendar view with the current month and year.</p>
<p>Step 4: Create the view Next, create the <code>calendar.blade.php</code> view that will display the calendar and the events for the selected date. Add the following code to the <code>calendar.blade.php</code> file:</p>
<pre><code class="lang-bash">&lt;div&gt;
    &lt;h2&gt;{{ date(<span class="hljs-string">'F Y'</span>, strtotime(<span class="hljs-variable">$year</span> . <span class="hljs-string">'-'</span> . <span class="hljs-variable">$month</span> . <span class="hljs-string">'-01'</span>)) }}&lt;/h2&gt;

    &lt;table&gt;
        &lt;thead&gt;
            &lt;tr&gt;
                &lt;th&gt;Sun&lt;/th&gt;
                &lt;th&gt;Mon&lt;/th&gt;
                &lt;th&gt;Tue&lt;/th&gt;
                &lt;th&gt;Wed&lt;/th&gt;
                &lt;th&gt;Thu&lt;/th&gt;
                &lt;th&gt;Fri&lt;/th&gt;
                &lt;th&gt;Sat&lt;/th&gt;
            &lt;/tr&gt;
        &lt;/thead&gt;
        &lt;tbody&gt;
            @foreach (<span class="hljs-variable">$calendar</span> as <span class="hljs-variable">$week</span>)
                &lt;tr&gt;
                    @foreach (<span class="hljs-variable">$week</span> as <span class="hljs-variable">$day</span>)
                        &lt;td wire:click=<span class="hljs-string">"getEventsForDate('{{ <span class="hljs-variable">$day</span>['date'] }}')"</span>&gt;{{ <span class="hljs-variable">$day</span>[<span class="hljs-string">'day'</span>] }}&lt;/td&gt;
                    @endforeach
                &lt;/tr&gt;
            @endforeach
        &lt;/tbody&gt;
    &lt;/table&gt;

    &lt;div&gt;
        @<span class="hljs-keyword">if</span> (count(<span class="hljs-variable">$events</span>) &gt; 0)
            &lt;h3&gt;Events <span class="hljs-keyword">for</span> {{ date(<span class="hljs-string">'F j, Y'</span>, strtotime(<span class="hljs-variable">$selectedDate</span>)) }}&lt;/h3&gt;

            &lt;ul&gt;
                @foreach (<span class="hljs-variable">$events</span> as <span class="hljs-variable">$event</span>)
                    &lt;li&gt;{{ <span class="hljs-variable">$event</span>-&gt;title }}&lt;/li&gt;
                @endforeach
            &lt;/ul&gt;
        @endif
    &lt;/div&gt;
&lt;/div&gt;
</code></pre>
<p>This code displays the calendar for the selected month and year using a <code>table</code> element. The <code>foreach</code> loops are used to loop through the weeks and days of the calendar.</p>
<p>The <code>wire:click</code> directive is used to listen for clicks on each day of the calendar. When a day is clicked, it calls the <code>getEventsForDate()</code> method with the selected date as a parameter.</p>
<p>The <code>if</code> statement checks if there are any events for the selected date. If there are, it displays the events in an unordered list.</p>
<p>Step 5: Define the calendar data Define the data that will be used to generate the calendar. Add the following code to the <code>Calendar</code> component:</p>
<pre><code class="lang-bash">class Calendar extends Component
{
    public <span class="hljs-variable">$month</span>;
    public <span class="hljs-variable">$year</span>;
    public <span class="hljs-variable">$events</span> = [];

    public <span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">mount</span></span>()
    {
        <span class="hljs-variable">$this</span>-&gt;month = date(<span class="hljs-string">'m'</span>);
        <span class="hljs-variable">$this</span>-&gt;year = date(<span class="hljs-string">'Y'</span>);
    }

    public <span class="hljs-keyword">function</span> getEventsForDate(<span class="hljs-variable">$date</span>)
    {
        // Get the events <span class="hljs-keyword">for</span> the selected date and store them <span class="hljs-keyword">in</span> the <span class="hljs-variable">$events</span> property
    }

    public <span class="hljs-keyword">function</span> <span class="hljs-function"><span class="hljs-title">render</span></span>()
    {
        <span class="hljs-variable">$calendar</span> = [];

        <span class="hljs-variable">$weeksInMonth</span> = Carbon::createFromDate(<span class="hljs-variable">$this</span>-&gt;year, <span class="hljs-variable">$this</span>-&gt;month)-&gt;weeksInMonth;

        <span class="hljs-keyword">for</span> (<span class="hljs-variable">$week</span> = 1; <span class="hljs-variable">$week</span> &lt;= <span class="hljs-variable">$weeksInMonth</span>; <span class="hljs-variable">$week</span>++) {
            <span class="hljs-variable">$days</span> = [];

            <span class="hljs-keyword">for</span> (<span class="hljs-variable">$day</span> = 1; <span class="hljs-variable">$day</span> &lt;= 7; <span class="hljs-variable">$day</span>++) {
                <span class="hljs-variable">$date</span> = Carbon::createFromDate(<span class="hljs-variable">$this</span>-&gt;year, <span class="hljs-variable">$this</span>-&gt;month, 1)-&gt;addWeeks(<span class="hljs-variable">$week</span> - 1)-&gt;addDays(<span class="hljs-variable">$day</span> - 1);

                <span class="hljs-variable">$days</span>[] = [
                    <span class="hljs-string">'day'</span> =&gt; <span class="hljs-variable">$date</span>-&gt;format(<span class="hljs-string">'j'</span>),
                    <span class="hljs-string">'date'</span> =&gt; <span class="hljs-variable">$date</span>-&gt;format(<span class="hljs-string">'Y-m-d'</span>),
                ];
            }

            <span class="hljs-variable">$calendar</span>[] = <span class="hljs-variable">$days</span>;
        }

        <span class="hljs-built_in">return</span> view(<span class="hljs-string">'livewire.calendar'</span>, [
            <span class="hljs-string">'calendar'</span> =&gt; <span class="hljs-variable">$calendar</span>,
            <span class="hljs-string">'selectedDate'</span> =&gt; <span class="hljs-variable">$this</span>-&gt;selectedDate,
            <span class="hljs-string">'events'</span> =&gt; <span class="hljs-variable">$this</span>-&gt;events,
        ]);
    }
}
</code></pre>
<p>In this code, we use the Carbon library to generate the calendar data. We start by getting the number of weeks in the current month using the <code>weeksInMonth</code> property.</p>
<p>We then loop through each week and each day of the week to generate an array of days for each week. We use the <code>Carbon</code> library to create a <code>Carbon</code> instance for each day, which allows us to easily format the day and date.</p>
<p>Finally, we return the <code>calendar</code> data, along with the <code>selectedDate</code> and <code>events</code> properties, to the view.</p>
<p>Step 6: Fetch the events for the selected date In the <code>getEventsForDate()</code> method, we need to fetch the events for the selected date and store them in the <code>$events</code> property. Here&#8217;s an example code:</p>
<pre><code class="lang-bash">public <span class="hljs-keyword">function</span> getEventsForDate(<span class="hljs-variable">$date</span>)
{
    <span class="hljs-variable">$this</span>-&gt;selectedDate = <span class="hljs-variable">$date</span>;
    <span class="hljs-variable">$this</span>-&gt;events = Event::whereDate(<span class="hljs-string">'start_time'</span>, <span class="hljs-variable">$date</span>)-&gt;get();
}
</code></pre>
<p>In this code, we set the <code>$selectedDate</code> property to the selected date and fetch the events for that date using the <code>whereDate()</code> method of the <code>Event</code> model. We then store the events in the <code>$events</code> property.</p>
<p>Step 7: Add styles Optionally, you can add some styles to the calendar to make it look better. Here&#8217;s an example CSS code that you can use:</p>
<pre><code class="lang-bash">.calendar-container {
    max-width: 600px;
    margin: 0 auto;
    padding: 20px;
    border: 1px solid <span class="hljs-comment">#ccc;</span>
}

.calendar-container h2 {
    font-size: 24px;
    text-align: center;
}

.calendar-container table {
    width: 100%;
    border-collapse: collapse;
    margin: 20px 0;
}

.calendar-container table th {
    padding: 10px;
    text-align: center;
    border: 1px solid <span class="hljs-comment">#ccc;</span>
}

.calendar-container table td
{ 
 padding: 10px; text-align: center; 
 border: 1px solid <span class="hljs-comment">#ccc; </span>
} 
.calendar-container table td.today { 
background-color: <span class="hljs-comment">#fff6d5; </span>
}

.calendar-container table td:not(.disabled):hover { cursor: pointer; background-color: <span class="hljs-comment">#f1f1f1; </span>
}

.calendar-container table td.disabled { color: <span class="hljs-comment">#ccc;</span>
}
</code></pre>
<p>In this code, we define some basic styles for the calendar container, the header, the table, and the table cells. We also define a style for the current date cell and a hover effect for the clickable cells.</p>
<p>Step 8: Display the events Finally, we need to display the events for the selected date in the calendar. We can do this by adding the following code to the <code>render()</code> method:</p>
<pre><code class="lang-bash"><span class="hljs-built_in">return</span> view(<span class="hljs-string">'livewire.calendar'</span>, [ <span class="hljs-string">'calendar'</span> =&gt; <span class="hljs-variable">$calendar</span>, <span class="hljs-string">'selectedDate'</span> =&gt; <span class="hljs-variable">$this</span>-&gt;selectedDate, <span class="hljs-string">'events'</span> =&gt; <span class="hljs-variable">$this</span>-&gt;events, ]);
</code></pre>
<p>And adding the following code to the <code>index.blade.php</code> view:</p>
<pre><code class="lang-bash">
@<span class="hljs-keyword">if</span> (count(<span class="hljs-variable">$events</span>) &gt; 0) &lt;ul&gt; @foreach (<span class="hljs-variable">$events</span> as <span class="hljs-variable">$event</span>) &lt;li&gt;{{ <span class="hljs-variable">$event</span>-&gt;name }}&lt;/li&gt; @endforeach &lt;/ul&gt; @endif
</code></pre>
<p>In this code, we check if there are any events for the selected date, and if there are, we display them in an unordered list.</p>
<p>That&#8217;s it! You now have a working live calendar in Laravel using Livewire. You can customize this code further to add more features, such as the ability to add and edit events.</p>
</div>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
