<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Koussay</title>
    <description>The latest articles on DEV Community by Koussay (@koossaayy).</description>
    <link>https://dev.to/koossaayy</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F254748%2F80e19e1d-fe6c-47f0-a0d1-bba0efa17d97.jpg</url>
      <title>DEV Community: Koussay</title>
      <link>https://dev.to/koossaayy</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/koossaayy"/>
    <language>en</language>
    <item>
      <title>Create Custom Blade Directives in Laravel</title>
      <dc:creator>Koussay</dc:creator>
      <pubDate>Sun, 27 Aug 2023 21:27:34 +0000</pubDate>
      <link>https://dev.to/koossaayy/create-custom-blade-directives-in-laravel-bn5</link>
      <guid>https://dev.to/koossaayy/create-custom-blade-directives-in-laravel-bn5</guid>
      <description>&lt;p&gt;Laravel Blade is awesome, really awesome. One of the greatest things about it, is the ability to add custom directives. &lt;br&gt;
One use case I recently stumbled upon, is formatting money, with the currency. For sure I can create a helper function and call it. &lt;br&gt;
Example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="nb"&gt;function_exists&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'formatMoney'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;formatMoney&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;float&lt;/span&gt; &lt;span class="nv"&gt;$amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;is_string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$amount&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nv"&gt;$amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;floatval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$amount&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="nv"&gt;$settings&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;app&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;GeneralSettings&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="nv"&gt;$formatter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;NumberFormatter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;config&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'app.currency_locale'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'en_US'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="nc"&gt;NumberFormatter&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;CURRENCY&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$formatter&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;formatCurrency&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$amount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$settings&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;app_currency&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then we could use it in our views like the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...
{{ formatMoney($invoice-&amp;gt;total) }}
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And we're good to go, but in this context, let's use the power of Blade and create our own directive. &lt;br&gt;
In order to do so, open &lt;code&gt;AppServiceProvider.php&lt;/code&gt; and head to the &lt;code&gt;boot&lt;/code&gt; method. &lt;br&gt;
Inside the boot method, we will write our own custom directive.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;some&lt;/span&gt; &lt;span class="n"&gt;code&lt;/span&gt; &lt;span class="n"&gt;here&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our context we will use the code above, defined in a helper class, so we can format the money. &lt;br&gt;
The blade directive will be named &lt;code&gt;money&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'money'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php echo app('App\Utils\Helpers')-&amp;gt;formatMoney(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="s2"&gt;); ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So here is the final code, and in the view file, we can call it as follows&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@money('somevalue')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The take here is in the return value, if you want to have some formatters, or some conditions, the return must be a PHP code. Let's take another example. &lt;br&gt;
Laravel has &lt;code&gt;@auth&lt;/code&gt; &amp;amp; &lt;code&gt;@guest&lt;/code&gt; directives. &lt;br&gt;
But let's make some thing on our own. Let's say we have customers in our application, so we will make a blade directive that checks if the authenticated user is a customer or not, and show some code based on that condition.&lt;br&gt;
Usually, here is what we would do :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@if(auth()-&amp;gt;user()-&amp;gt;isCustomer)
//some code here
@endif
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But, that would be hard if we have that piece of code everywhere in our views. Imagine if we will add another condition, like checks if the customer has an active subscription for example.&lt;br&gt;
So yeah, you've got the point. &lt;br&gt;
In this situation, we could leverage blade custom directives.&lt;br&gt;
The use of the new directive would be as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@customer
//somecode here
@endcustomer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see here, we have two directives, so we will define two functions in our &lt;code&gt;AppServiceProvider.php&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'customer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php if(auth()-&amp;gt;check() &amp;amp;&amp;amp; auth()-&amp;gt;user()-&amp;gt;isCustomer): ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nc"&gt;Blade&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;directive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'endcustomer'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$expression&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"&amp;lt;?php endif; ?&amp;gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first one is to test the condition and open the colon for your code logic, and the other one, will close the the if statement. &lt;/p&gt;

&lt;p&gt;In this short tutorial, we saw how we could write custom blade directives. I hope it was insightful for you to understand how to create and use them. &lt;br&gt;
If there is any question or any suggestion, feel free to reach me on &lt;a href="https://x.com/koossaayy"&gt;Twitter&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="https://koossaayy.tn/create-custom-blade-directives-in-laravel-"&gt;Original Post&lt;/a&gt; &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>php</category>
      <category>blade</category>
      <category>directive</category>
    </item>
    <item>
      <title>Laravel Livewire Multiple Selection with Virtual Select</title>
      <dc:creator>Koussay</dc:creator>
      <pubDate>Tue, 27 Jun 2023 00:40:40 +0000</pubDate>
      <link>https://dev.to/koossaayy/laravel-livewire-multiple-selection-with-virtual-select-1f87</link>
      <guid>https://dev.to/koossaayy/laravel-livewire-multiple-selection-with-virtual-select-1f87</guid>
      <description>&lt;p&gt;Hello.&lt;br&gt;
Recently in of our projects, we had to create a multiple selection dropdown. We were using Livewire, and vanilla JavaScript (no AlpineJS), so the options were either using the default multiple select, which is to be honest, ugly.&lt;br&gt;
So we have to check for something else. The next options was Select2 Js, a popular jQuery library.&lt;br&gt;
The problem with Select2 was the jQuery, we have to import jQuery just for that, and also the design wasn't that good actually.&lt;/p&gt;

&lt;p&gt;So after little research, and so many thanks for &lt;a href="https://forum.laravel-livewire.com/u/skywalker"&gt;skywalker&lt;/a&gt; , we were introduced to Virtual Select. &lt;/p&gt;

&lt;p&gt;What we like about Virtual Select is the looks and the performance of it. It was amazing really, no dependencies and it worked from the first try.&lt;br&gt;
Unfortunately, the plugin doesn't support ES6 yet. &lt;br&gt;
But if you want to contribute, here is &lt;a href="https://github.com/sa-si-dev/virtual-select/issues/168"&gt;the link&lt;/a&gt; to issue discussing the matter.&lt;/p&gt;

&lt;p&gt;In this tutorial we will cover how to import Virtual Select, and how to retrieve data in our component.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; : This tutorial will assume you have a Laravel installation in place already and Livewire is also installed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; : In this tutorial we will not cover how to get data from API, it maybe for another tutorial soon 🤞.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That being said, the first thing to do is to create the Livewire component. A common use case for this would be as follows: &lt;br&gt;
Select multiple permissions for a specific user. &lt;br&gt;
So our component would be named, for example &lt;code&gt;EditUser&lt;/code&gt;.&lt;br&gt;
So run this in your terminal :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan livewire:make EditUser 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After creating the component, the next thing to do is open the class, called &lt;code&gt;EditUser.php&lt;/code&gt;, and edit the &lt;code&gt;render&lt;/code&gt; method as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Permission&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Livewire\Component&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EditUser&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$permissions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Permission&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'livewire.edit-user'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'permissions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$permissions&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we added the list of permissions needed to assign them to the desired user.&lt;br&gt;
Another thing to do here, is to prepare a public property to hold those assigned permissions. So the class will be as follows :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;App\Models\Permission&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;Livewire\Component&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;EditUser&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Component&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nv"&gt;$selectedPermissions&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[];&lt;/span&gt;


    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$permissions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Permission&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;view&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'livewire.edit-user'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'permissions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$permissions&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And of course your component would hold other properties such as the user property and other stuff, but for the sake of this tutorial, we are making it simple. &lt;/p&gt;

&lt;p&gt;Anyways, moving on now to the frontend. The first thing you need to do is to "import" Virtual Select CSS &amp;amp; JS assets. &lt;br&gt;
You can download them, or use a CDN. &lt;br&gt;
For this example, we will use jsdelivr.com .&lt;br&gt;
So the first thing you need, is to use them in your main layout file as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/virtual-select-plugin@1.0.39/dist/virtual-select.min.js"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha256-Gsn2XyJGdUeHy0r4gaP1mJy1JkLiIWY6g6hJhV5UrIw="&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;

    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/virtual-select-plugin@1.0.39/dist/virtual-select.min.css"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha256-KqTuc/vUgQsb5EMyyxWf62qYinMUXDpWELyNx+cCUr0="&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also, you may want to use &lt;code&gt;@stack&lt;/code&gt; for the JavaScript we will use in the component later.&lt;br&gt;
After those modifications, our main layout file would be look like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;...
    @livewireStyles
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/virtual-select-plugin@1.0.39/dist/virtual-select.min.js"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha256-Gsn2XyJGdUeHy0r4gaP1mJy1JkLiIWY6g6hJhV5UrIw="&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://cdn.jsdelivr.net/npm/virtual-select-plugin@1.0.39/dist/virtual-select.min.css"&lt;/span&gt; &lt;span class="na"&gt;integrity=&lt;/span&gt;&lt;span class="s"&gt;"sha256-KqTuc/vUgQsb5EMyyxWf62qYinMUXDpWELyNx+cCUr0="&lt;/span&gt; &lt;span class="na"&gt;crossorigin=&lt;/span&gt;&lt;span class="s"&gt;"anonymous"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

&lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;




    @livewireScripts
    @stack('js')
    ...
&lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now moving on to our component. &lt;br&gt;
The first thing to do is to initiate an empty &lt;code&gt;div&lt;/code&gt; for the plugin to work. So open &lt;code&gt;edit-user.blade.php&lt;/code&gt; and add the empty div with an &lt;code&gt;id&lt;/code&gt; so we can reference it later. &lt;br&gt;
At first, our component will look like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"permissions"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After initializing the DOM element, the next thing to do is to initiate the plugin. In order to do so, we will "push" the stacked &lt;code&gt;js&lt;/code&gt; we defined earlier. It will look like this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"permissions"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    @push('js')
        &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;

        &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    @endpush
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now let's get to work. We need to define our options, to do so, we can create a new JavaScript variable to hold the permissions coming from the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            const permissions = [
                @foreach ($permissions as $permission)
                    {
                        label: "{{ $permission-&amp;gt;name }}",
                        value: "{{ $permission-&amp;gt;id }}"
                    },
                @endforeach
            ];

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This piece of code, will be able to transform the collection of permissions into a JavaScript array. &lt;br&gt;
After that, what we need to do is to initiate the plugin itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"permissions"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    @push('js')
        &lt;span class="nt"&gt;&amp;lt;script&amp;gt;&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;permissions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
                &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;foreach&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;$permissions&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;$permission&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                    &lt;span class="p"&gt;{&lt;/span&gt;
                        &lt;span class="nl"&gt;label&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{{ $permission-&amp;gt;name }}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;{{ $permission-&amp;gt;id }}&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
                    &lt;span class="p"&gt;},&lt;/span&gt;
                &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;endforeach&lt;/span&gt;
            &lt;span class="p"&gt;];&lt;/span&gt;
            &lt;span class="nx"&gt;VirtualSelect&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;init&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
                &lt;span class="na"&gt;ele&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#permissions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;multiple&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;permissions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    @endpush
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, we called the &lt;code&gt;init&lt;/code&gt; method with the options :&lt;br&gt;
&lt;/p&gt;
&lt;li&gt;

&lt;code&gt;ele&lt;/code&gt; : The element of the DOM that will render the dropdown
&lt;/li&gt;
&lt;br&gt;
&lt;li&gt;

&lt;code&gt;multiple&lt;/code&gt; : whether to indicate it's a multiple selection or not
&lt;/li&gt;
&lt;br&gt;
&lt;li&gt;

&lt;code&gt;options&lt;/code&gt; : the data the dropdown will populated with, which we defined earlier.
&lt;/li&gt;

&lt;p&gt;At this moment, if we visit the page, our component will be like this: &lt;br&gt;
&lt;iframe src="https://player.vimeo.com/video/839911117" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;At this stage, everything is working perfectly, but the problem is how to tell Livewire about those selections. At this point, we will use Livewire beauty ❤️&lt;/p&gt;

&lt;p&gt;In order to pass the data to Livewire, we will use the JavaScript event listener, whenever the selection is changed, we will send them to our component. &lt;br&gt;
So in our view file, what we need to add is the following code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;selectedPermissions&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#permissions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="nx"&gt;selectedPermissions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;change&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;selectedPermissions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;selectedPermissions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="p"&gt;});&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we did is we first get the DOM element defined earlier, then we attach to it an event listener, whenever the selection changes, we want to get the selection and send it to our component.&lt;br&gt;
And then at the component level, the selected data will be available at &lt;code&gt;$selectedPermissions&lt;/code&gt; property, so you can assign them to the user. &lt;/p&gt;

&lt;p&gt;I hope you liked this tutorial, and you gain information about it.&lt;br&gt;
If something is not clear or you want ask about further information, you can contact me on &lt;a href="https://twitter.com/Koossaayy"&gt;Twitter&lt;/a&gt; or comment down below.&lt;br&gt;
Thank you again and have a nice day.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://koossaayy.tn/laravel-livewire-multiple-selection-with-virtual-select-"&gt;Original Post&lt;/a&gt;&lt;br&gt;
Credits : Photo by &lt;a href="https://unsplash.com/@edgr?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Edu Grande&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/0vY082Un2pk?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>livewire</category>
      <category>javascript</category>
      <category>php</category>
    </item>
    <item>
      <title>Effortless Email Archiving: Saving Sent Emails on Server with Laravel IMAP</title>
      <dc:creator>Koussay</dc:creator>
      <pubDate>Sun, 11 Jun 2023 13:49:25 +0000</pubDate>
      <link>https://dev.to/koossaayy/effortless-email-archiving-saving-sent-emails-on-server-with-laravel-imap-3he7</link>
      <guid>https://dev.to/koossaayy/effortless-email-archiving-saving-sent-emails-on-server-with-laravel-imap-3he7</guid>
      <description>&lt;p&gt;Hello, &lt;br&gt;
Sending emails with Laravel is really one of the easiest things to do. For example, sending password resets, or account activation, but sometimes, you want to send emails to your users and leave a trail for that. In order to do so, you have 2 options, either storing the email in the database, or save it on the IMAP server. &lt;br&gt;
In this tutorial we will see the second option, append the sent email to the IMAP server. So let's get started.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; : This tutorial will assume you have a Laravel installation in place already.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first thing to do is to install &lt;code&gt;laravel-imap&lt;/code&gt; package&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;composer require webklex/laravel-imap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After installing it, we will set up the IMAP credentials in our &lt;code&gt;.env&lt;/code&gt; file.&lt;br&gt;
After opening the &lt;code&gt;.env&lt;/code&gt; file, paste the following code at the end of the file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;IMAP_HOST="imaphost"
IMAP_PORT="993"
IMAP_ENCRYPTION="ssl"
IMAP_VALIDATE_CERT="true"
IMAP_USERNAME="email"
IMAP_PASSWORD="password"
IMAP_DEFAULT_ACCOUNT=default
IMAP_PROTOCOL=imap
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then change the values in according to your configuration. &lt;/p&gt;

&lt;p&gt;The next step is to generate a Mailable so we can send it, to do so, just run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;php artisan make:mail TestMail
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This command will generate a new mailable class called &lt;code&gt;TestMail.php&lt;/code&gt;. &lt;br&gt;
All you need to do now is open the file and edit it (mainly adjust the view name). &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; : You can see other tutorials about sending emails using Laravel.&lt;br&gt;
Now after everything is in place, now we will try to send the email and save it on the IMAP server. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So to send the email, all you need to do is the following&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$mail&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Mail&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;to&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'hello@gmail.com'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;TestMail&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note here, we saved the result in the &lt;code&gt;$mail&lt;/code&gt; variable, because we will use that to store the sent email. &lt;br&gt;
After the email was sent, we need to connect to our IMAP server now.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;account&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'default'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nv"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nv"&gt;$folder&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$client&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getFolderByName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Sent'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code above will try to connect to the IMAP server using the credentials we put earlier in the &lt;code&gt;.env&lt;/code&gt; file. After the connection has been made, we will retrieve the &lt;code&gt;Sent&lt;/code&gt; folder. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; : The sent folder name is usually &lt;code&gt;Sent&lt;/code&gt;, this depends on your IMAP configuration.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;After successfully getting the folder, all we need now is to append it, by using the &lt;code&gt;appendMessage&lt;/code&gt; method.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$folder&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;appendMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$mail&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getSymfonySentMessage&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'\Seen'&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"d-M-Y h:i:s O"&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's break down this method, and see: &lt;br&gt;
The first parameter, is the message in a string format. &lt;br&gt;
The &lt;code&gt;$mail&lt;/code&gt; variable will be a &lt;code&gt;SymfonySentMessage&lt;/code&gt; type, so it has a method called &lt;code&gt;getSymfonySentMessage()-&amp;gt;toString()&lt;/code&gt;, this method will return the string format of the message. &lt;br&gt;
This is an example of the result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;From: Laravel &amp;lt;hello@example.com&amp;gt;
To: hello@gmail.com
Subject: Test Mail
Message-ID: &amp;lt;1259c22f78de504c28beabdb9eda5636@example.com&amp;gt;
MIME-Version: 1.0
Date: Sun, 11 Jun 2023 13:37:36 +0000
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

HELLO WORLD
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using this string, we will be able to append the sent message, &lt;/p&gt;

&lt;p&gt;The second parameter is the flag, in this example, we will use the &lt;code&gt;seen&lt;/code&gt; flag, so it will be seen on the IMAP. You can learn more about flags on this &lt;a href="http://www.faqs.org/rfcs/rfc2060.html"&gt;RFC 2060&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And finally, the date, we will use that to indicate when this message will be saved at IMAP. &lt;/p&gt;

&lt;p&gt;Finally, we will check the result of the &lt;code&gt;appendMessage&lt;/code&gt; method. &lt;br&gt;
The result will be something like that&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;'OK [APPENDUID 1655722943 20] Append completed (0.018 + 0.135 + 0.016 secs)';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you have this string, then everything went okay. &lt;/p&gt;

&lt;p&gt;I hope you liked this tutorial, and you gain information about it. &lt;br&gt;
If something is not clear or you want ask about further information, you can contact me on &lt;a href="https://twitter.com/koossaayy"&gt;Twitter&lt;/a&gt; or comment down below.&lt;br&gt;
&lt;a href="https://koossaayy.tn/effortless-email-archiving-saving-sent-emails-on-server-with-laravel-imap"&gt;Original Post&lt;/a&gt;&lt;br&gt;
Thank you again and have a nice day.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>email</category>
      <category>imap</category>
      <category>php</category>
    </item>
    <item>
      <title>Laravel Realtime with Ably &amp; Laravel Livewire</title>
      <dc:creator>Koussay</dc:creator>
      <pubDate>Sun, 21 May 2023 13:49:24 +0000</pubDate>
      <link>https://dev.to/koossaayy/laravel-realtime-with-ably-laravel-livewire-224k</link>
      <guid>https://dev.to/koossaayy/laravel-realtime-with-ably-laravel-livewire-224k</guid>
      <description>&lt;p&gt;Welcome, &lt;br&gt;
One of the things I love about Livewire, is the seamless integration with Laravel Echo, which makes listening to real time events a breeze. &lt;br&gt;
In this tutorial, wel will see how to integrate Livewire with Ably.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; : The reason why I chose Ably, because of their generous free tier.&lt;br&gt;
&lt;strong&gt;&lt;em&gt;NOTE:&lt;/em&gt;&lt;/strong&gt; : This guide will assume you have Laravel &amp;amp; Livewire installed and already configured.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let's get started. &lt;br&gt;
Firs thing to do is to create an account in &lt;a href="https://ably.com" rel="noopener noreferrer"&gt;Ably&lt;/a&gt;, so head there and create your account. &lt;br&gt;
Then, you have to create an application, give it a name, and we are good to go. &lt;br&gt;
The next thing to do, is to grab the API key.&lt;br&gt;
From the Ably dashboard, head to API Keys, you will find 2 keys, we are interested in the first one&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8uasl1uvmsme04mwj9go.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8uasl1uvmsme04mwj9go.png" alt="API Keys in Ably dashboard"&gt;&lt;/a&gt;&lt;br&gt;
Click on Show, then click on Copy, and let's go to our project.&lt;br&gt;
In your favorite text editor, open the &lt;code&gt;.env&lt;/code&gt; file. &lt;br&gt;
At the bottom of the file, add this entry : &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

ABLY_KEY=UDAfQA.yFhVpQ:*************************


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The API key you copied should go to ABLY_KEY.&lt;br&gt;
After setting up the key, now let's install the Ably Broadcaster.&lt;br&gt;
In your terminal run this command : &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

composer require ably/laravel-broadcaster


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After installing it, in your &lt;code&gt;.env&lt;/code&gt; file, set &lt;code&gt;BROADCAST_DRIVER&lt;/code&gt; as &lt;code&gt;ably&lt;/code&gt;. &lt;br&gt;
Then, go to &lt;code&gt;config/app.php&lt;/code&gt; and uncomment &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

        App\Providers\BroadcastServiceProvider::class,


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, let's install Ably Laravel Echo. &lt;br&gt;
In your terminal, run this command &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

npm install @ably/laravel-echo ably


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After the installation, put the following code, that will allow us to make connection to Ably SDK. &lt;br&gt;
You can put this code in the &lt;code&gt;bootstrap.js&lt;/code&gt; file, or in any other JavaScript file &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

import Echo from '@ably/laravel-echo';
import * as Ably from 'ably';

window.Ably = Ably;
window.Echo = new Echo({
    broadcaster: 'ably',
});
//this bit of code is optional, it's used to check the conntection to ably
window.Echo.connector.ably.connection.on(stateChange =&amp;gt; {
    if (stateChange.current === 'connected') {
        console.log('connected to ably server');
    }
});



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After setting up everything, now let's go create our Livewire components.&lt;br&gt;
We will create a Sender component, a receiver component, and an event.&lt;br&gt;
We will start with the event. In your terminal, run this command&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 php artisan make:event TriggerEvent


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The output of the command will be a class located under App/Events. Let's open the class, and edit it so it will be as follows: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

class TriggerEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     */
    public function __construct(
        public string $message
    ) {}

    /**
     * Get the channels the event should broadcast on.
     *
     * @return array&amp;lt;int, \Illuminate\Broadcasting\Channel&amp;gt;
     */
    public function broadcastOn(): array
    {
        return [
            new Channel('public-events'),
        ];
    }
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;TriggerEvent&lt;/code&gt; class must implement &lt;code&gt;ShouldBroadcast&lt;/code&gt; interface. Then in the constructor we passed a message, so we can get it later in the receiver component.&lt;br&gt;
Then in the &lt;code&gt;broadcastOn&lt;/code&gt; function, we returned an array of channels to broadcast on.&lt;br&gt;
In our example, we returned an instance of &lt;code&gt;Channel&lt;/code&gt;, which is a public channel, with the name of &lt;code&gt;public-events&lt;/code&gt;.&lt;br&gt;
The name is used to listen to it whenever an event is triggered.&lt;br&gt;
Now let's get to our components.&lt;br&gt;
The sender component will trigger the event, and the receiver will listen to it and update the UI. &lt;br&gt;
So in your terminal, run those commands to create the components.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 php artisan livewire:make Sender


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

 php artisan livewire:make Receiver


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our focus for now is the sender component. In the view file we will have an input text and button to trigger the event. &lt;br&gt;
In the &lt;code&gt;sender.blade.php&lt;/code&gt;, put the following code : &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;div&amp;gt;
    This is the sender component, the notification will be triggered from here.
    &amp;lt;div&amp;gt;
        &amp;lt;input type="text" wire:model='message'&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;button wire:click='trigger'&amp;gt;Trigger&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the above code, we have a model binded input to a property called &lt;code&gt;message&lt;/code&gt; and the button is binded to a method called trigger. &lt;br&gt;
Now in the &lt;code&gt;Sender.php&lt;/code&gt; class, our code will be as follows : &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

...
    public $message;

    public function render()
    {
        return view('livewire.sender');
    }

    public function trigger()
    {
        TriggerEvent::dispatch($this-&amp;gt;message);
    }
...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, let's head to the Receiver component. &lt;br&gt;
In the view file, we will have the following : &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&amp;lt;div&amp;gt;
    Breaking News :
    &amp;lt;h1&amp;gt;{{ $message }}&amp;lt;/h1&amp;gt;
&amp;lt;/div&amp;gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A dead simple UI, with a &lt;code&gt;message&lt;/code&gt; property. &lt;br&gt;
But the magic will happen, in the php class. &lt;br&gt;
So open the &lt;code&gt;Receiver.php&lt;/code&gt; class, and let's edit it as follows: &lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

...
    protected $listeners = ['echo:public-events,TriggerEvent' =&amp;gt; 'responseToEvent'];

    public $message;

    public function render()
    {
        return view('livewire.receiver');
    }

    public function responseToEvent($event)
    {
        $this-&amp;gt;message = $event['message'];
    }
...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The first thing to add is the &lt;code&gt;$listeners&lt;/code&gt; property, which is the array responsible for defining our listeners. &lt;br&gt;
In that array, &lt;code&gt;echo:public-events,TriggerEvent&lt;/code&gt; where &lt;code&gt;public-events&lt;/code&gt; is the name of the channel we defined earlier, and &lt;code&gt;TriggerEvent&lt;/code&gt; is the name of the event, also we have &lt;code&gt;responseToEvent&lt;/code&gt; which is the function will be fired whenever &lt;code&gt;TriggerEvent&lt;/code&gt; is triggered.&lt;br&gt;
Then we have a property called message which will be updated in the &lt;code&gt;responseToEvent&lt;/code&gt; method. &lt;/p&gt;

&lt;p&gt;And that's it, if everything went good, we will have the following result &lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://player.vimeo.com/video/828761869" width="710" height="399"&gt;
&lt;/iframe&gt;
&lt;br&gt;
If you have any questions, feel free to comment here, or DM me on &lt;a href="https://twitter.com/Koossaayy" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;br&gt;
&lt;a href="https://koossaayy.tn/laravel-realtime-with-ably-laravel-livewire-" rel="noopener noreferrer"&gt;Original Post&lt;/a&gt;&lt;br&gt;
Thank you for following the tutorial, and I will see you in other ones.&lt;/p&gt;

</description>
      <category>realtime</category>
      <category>laravel</category>
      <category>livewire</category>
      <category>ably</category>
    </item>
    <item>
      <title>How to test a Laravel package locally
</title>
      <dc:creator>Koussay</dc:creator>
      <pubDate>Sun, 12 Dec 2021 00:32:55 +0000</pubDate>
      <link>https://dev.to/koossaayy/how-to-test-a-laravel-package-locally-1556</link>
      <guid>https://dev.to/koossaayy/how-to-test-a-laravel-package-locally-1556</guid>
      <description>&lt;p&gt;Howdy!&lt;/p&gt;

&lt;p&gt;Lately I’ve been working on submitting a PR to a package, Laravel package to be precise.&lt;/p&gt;

&lt;p&gt;I forked &amp;amp; cloned the project to my local machine. I made my adjustments, and then I want to test it, because it has a visual interface.&lt;/p&gt;

&lt;p&gt;But I had a problem linking it an existing Laravel app to see those adjustments. So reading Composer docs, I found “Repositories”, and you can use it to link a local composer package, How ?&lt;/p&gt;

&lt;p&gt;First of all, after cloning or creating your own package, you can link it in your main application by editing &lt;code&gt;composer.json&lt;/code&gt;, adding this :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="nl"&gt;"repositories"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"path"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"url"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"../../awesome/package"&lt;/span&gt;&lt;span class="w"&gt;

        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note: the url must be the full path to the package.&lt;br&gt;
After that, add the package to the require section&lt;/p&gt;

&lt;p&gt;And finally&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Composer now will link the package to the main app, and any change you make in the package will be reflected immediately into your app.&lt;br&gt;
Thank you for reading.&lt;/p&gt;

</description>
      <category>php</category>
      <category>composer</category>
      <category>laravel</category>
      <category>package</category>
    </item>
    <item>
      <title>Laravel Mapbox, easily integrate Mapbox inside your Laravel app</title>
      <dc:creator>Koussay</dc:creator>
      <pubDate>Mon, 29 Nov 2021 22:54:59 +0000</pubDate>
      <link>https://dev.to/koossaayy/laravel-mapbox-easily-integrate-mapbox-inside-your-laravel-app-3iea</link>
      <guid>https://dev.to/koossaayy/laravel-mapbox-easily-integrate-mapbox-inside-your-laravel-app-3iea</guid>
      <description>&lt;p&gt;I have just released Laravel Mapbox, a Laravel package that allows you to add Mapbox to your views easily, with highly flexibility. &lt;br&gt;
It requires Laravel 8 and PHP 8.0 and up. &lt;/p&gt;

&lt;p&gt;Before installing the package, create a &lt;a href="https://mapbox.com"&gt;Mapbox account&lt;/a&gt; to get your token.&lt;/p&gt;
&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;To install the package, all you have to do is run this command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require koossaayy/laravel-mapbox 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After the installation, add this key to the &lt;code&gt;.env&lt;/code&gt; file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;MAPBOX_TOKEN={your mapbox token here}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And lastly publish the config file using this command :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;php artisan vendor:publish &lt;span class="nt"&gt;--tag&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"mapbox-config"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also don't forget to add CSS and JS files of Mapbox&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;'https://api.mapbox.com/mapbox-gl-js/v2.6.0/mapbox-gl.css'&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;'stylesheet'&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;'https://api.mapbox.com/mapbox-gl-js/v2.6.0/mapbox-gl.js'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's It you're good to go&lt;/p&gt;

&lt;h3&gt;
  
  
  Usage
&lt;/h3&gt;

&lt;p&gt;To show a map on a page, all you have to do is use :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;x-mapbox&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"map"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The package comes with a lot with other options, like markers, popups, interactivity, and a lot. &lt;br&gt;
You may check them &lt;a href="https://github.com/koossaayy/laravel-mapbox#usage"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you like the package, give it a star, and everyone is more than welcome to contribute ♥ &lt;/p&gt;

</description>
      <category>laravel</category>
      <category>news</category>
      <category>php</category>
      <category>mapbox</category>
    </item>
    <item>
      <title>Redirect User based on condition in Laravel Fortify</title>
      <dc:creator>Koussay</dc:creator>
      <pubDate>Thu, 26 Nov 2020 22:37:24 +0000</pubDate>
      <link>https://dev.to/koossaayy/redirect-user-based-on-condition-in-laravel-fortify-2d81</link>
      <guid>https://dev.to/koossaayy/redirect-user-based-on-condition-in-laravel-fortify-2d81</guid>
      <description>&lt;p&gt;3aslema (means Hello in Tunisian)&lt;br&gt;
Recently I had a Laravel project, where I had to redirect users based on their role, to their dashboards. &lt;br&gt;
I'm using Laravel 8, so I thought about using Laravel Fortify. &lt;br&gt;
So after installing and configure it, create a new PHP file, name it, let's say "LoginResponse" &lt;br&gt;
This class will implement the "LoginResponseContract" and it will override the "toResponse" method, and in that method we will implement the logic. &lt;br&gt;
So the class will be like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use Illuminate\Support\Facades\Auth;
use Laravel\Fortify\Contracts\LoginResponse as LoginResponseContract;

class LoginResponse implements LoginResponseContract
{

    /**
     * toResponse
     *
     * @param mixed $request
     * 
     * @return RedirectResponse
     */
    public function toResponse($request): RedirectResponse
    {
        if(Auth::user()-&amp;gt;role === "Admin")
            return redirect()-&amp;gt;route('admin');
        return redirect()-&amp;gt;route('user');

    }

}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we will check the role of the authenticated user, if it's admin, we will redirect it the admin route, otherwise, we will redirect it the default route. &lt;/p&gt;

&lt;p&gt;After creating the class, all you need to do now, is to bind the newly creating class to Fortify.&lt;/p&gt;

&lt;p&gt;All you need to do is open &lt;code&gt;FortifyServiceProvider.php&lt;/code&gt;, and in the &lt;code&gt;boot&lt;/code&gt; method, add this code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;        &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;singleton&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nc"&gt;Laravel\Fortify\Contracts\LoginResponse&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;LoginResponse&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;class&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that's it ♥&lt;/p&gt;

&lt;p&gt;P.S : I'm not a Laravel Expert, I'm just writing real-world examples I encounter, so Please correct me if I'm wrong.&lt;/p&gt;

</description>
      <category>laravel</category>
      <category>fortify</category>
      <category>php</category>
    </item>
  </channel>
</rss>
