<?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>follow unfollow Archives - Codeplaners</title>
	<atom:link href="https://codeplaners.com/tag/follow-unfollow/feed/" rel="self" type="application/rss+xml" />
	<link>https://codeplaners.com/tag/follow-unfollow/</link>
	<description>Code Solution</description>
	<lastBuildDate>Wed, 12 May 2021 07:04:27 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.7</generator>

<image>
	<url>https://codeplaners.com/wp-content/uploads/2020/09/cropped-favicon-social-32x32.png</url>
	<title>follow unfollow Archives - Codeplaners</title>
	<link>https://codeplaners.com/tag/follow-unfollow/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Laravel 8 Follow Unfollow Example</title>
		<link>https://codeplaners.com/laravel-8-follow-unfollow-example/</link>
					<comments>https://codeplaners.com/laravel-8-follow-unfollow-example/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 12 May 2021 06:13:42 +0000</pubDate>
				<category><![CDATA[Laravel]]></category>
		<category><![CDATA[Laravel 8]]></category>
		<category><![CDATA[follow unfollow]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[Jquery]]></category>
		<guid isPermaLink="false">https://codeplaners.com/?p=669</guid>

					<description><![CDATA[<p>Today, i we will show you Laravel 8 Follow Unfollow Example. This article will give you simple example of Laravel 8 Follow Unfollow Example. you will learn Laravel 8 Follow Unfollow Example. step by step explain Laravel 8 Follow Unfollow Example. For making it i will use &#8220;overture/laravel-follow&#8221; composer package to create follow unfollow system &#8230; <a href="https://codeplaners.com/laravel-8-follow-unfollow-example/" class="more-link">Continue reading<span class="screen-reader-text"> "Laravel 8 Follow Unfollow Example"</span></a></p>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-8-follow-unfollow-example/">Laravel 8 Follow Unfollow Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Today, i we will show you Laravel 8 Follow Unfollow Example. This article will give you simple example of Laravel 8 Follow Unfollow Example. you will learn Laravel 8 Follow Unfollow Example. step by step explain Laravel 8 Follow Unfollow Example.</p>
<p>For making it i will use &#8220;overture/laravel-follow&#8221; composer package to create follow unfollow system in Laravel.<br />
<img decoding="async" src="https://codeplaners.com/wp-content/uploads/2021/05/followtemplate.png" alt="Laravel 8 Follow Unfollow" ><br />
<img decoding="async" src="https://codeplaners.com/wp-content/uploads/2021/05/follow.png" alt="Laravel 8 Follow Unfollow" ></p>
<h3>Step 1 :- Install Laravel 8</h3>
<pre class="brush: php; title: ; notranslate">
composer create-project --prefer-dist laravel/laravel follow-unfollow
</pre>
<p><strong>Connect Database .env</strong></p>
<pre class="brush: php; title: ; notranslate">
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=databse
DB_USERNAME=root
DB_PASSWORD=
</pre>
<h3>Step 2 :- Install overtrue/laravel-follow Package</h3>
<pre class="brush: php; title: ; notranslate">
composer require overtrue/laravel-follow -vvv
</pre>
<p>Now open <strong>config/app.php</strong> file and add service provider and aliase.</p>
<p><strong>config/app.php</strong></p>
<pre class="brush: php; title: ; notranslate">
'providers' =&gt; &#x5B;
	Overtrue\LaravelFollow\FollowServiceProvider::class,
],
</pre>
<p>To publish the migrations file run bellow command.</p>
<pre class="brush: php; title: ; notranslate">
php artisan vendor:publish --provider='Overtrue\LaravelFollow\FollowServiceProvider' --tag=&quot;migrations&quot;
</pre>
<p>As optional if you want to modify the default configuration, you can publish the configuration file.</p>
<pre class="brush: php; title: ; notranslate">
php artisan vendor:publish --provider=&quot;Overtrue\LaravelFollow\FollowServiceProvider&quot; --tag=&quot;config&quot;
</pre>
<p>Then just migrate it by using following command</p>
<pre class="brush: php; title: ; notranslate">
php artisan migrate
</pre>
<h3>Step 3 :- Create Authentication </h3>
<pre class="brush: php; title: ; notranslate">
composer require laravel/ui

php artisan ui vue --auth
</pre>
<h3>Step 4: Create Dummy Users</h3>
<pre class="brush: php; title: ; notranslate">
php artisan tinker
\App\Models\User::factory(10)-&gt;create();
</pre>
<h3>Step 5: Update User Model</h3>
<p><strong>App/Models/User.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Models;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Overtrue\LaravelFollow\Followable;


class User extends Authenticatable
{
    use Notifiable, Followable; 

    protected $fillable = &#x5B;
        'name', 'email', 'password',
    ];

    protected $hidden = &#x5B;
        'password', 'remember_token',
    ];
}

</pre>
<h3>Step 6: Add Routes</h3>
<p><strong>routes/web.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the &quot;web&quot; middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();
Route::get('home', &#x5B;App\Http\Controllers\HomeController::class, 'index'])-&gt;name('home');
Route::get('users', &#x5B;App\Http\Controllers\HomeController::class, 'users'])-&gt;name('users');
Route::get('user/{id}', &#x5B;App\Http\Controllers\HomeController::class, 'user'])-&gt;name('user.view');
Route::post('follow', &#x5B;App\Http\Controllers\HomeController::class, 'follwUserRequest'])-&gt;name('follow');


</pre>
<h3>Step 7: Create Controller Method</h3>
<p><strong>app/Http/HomeController.php</strong></p>
<pre class="brush: php; title: ; notranslate">
&lt;?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

class HomeController extends Controller
{
    public function __construct()
    {
        $this-&gt;middleware('auth');
    }

    public function index()
    {
        return view('home');
    }

    public function users()
    {
        $users = User::get();
        return view('users', compact('users'));
    }

    public function user($id)
    {
        $user = User::find($id);
        return view('usersView', compact('user'));
    }

    public function follwUserRequest(Request $request){


        $user = User::find($request-&gt;user_id);
        $response = auth()-&gt;user()-&gt;toggleFollow($user);


        return response()-&gt;json(&#x5B;'success'=&gt;$response]);
    }
}

</pre>
<h3>Step 8: Create Blade files and JS File</h3>
<p>Now in this file we will need to create userList.blade.php, users.blade.php and usersView.blade.php files and custom.js file. So let&#8217;s create both files.</p>
<p><strong>resources/views/users.blade.php</strong></p>
<pre class="brush: php; title: ; notranslate">
@extends('layouts.app')
@section('content')

&lt;script src=&quot;{{ asset('js/custom.js') }}&quot; defer&gt;&lt;/script&gt;

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row justify-content-center&quot;&gt;
        &lt;div class=&quot;col-md-12&quot;&gt;
            &lt;div class=&quot;card&quot;&gt;
                &lt;div class=&quot;card-header&quot;&gt;List of Users- CodePlaners.com&lt;/div&gt;
                &lt;div class=&quot;card-body&quot;&gt;
                    &lt;div class=&quot;row pl-5&quot;&gt;
                        @include('userList', &#x5B;'users'=&gt;$users])
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
@endsection 
</pre>
<p><strong>resources/views/usersView.blade.php</strong></p>
<pre class="brush: php; title: ; notranslate">
@extends('layouts.app')

@section('content')
&lt;script src=&quot;{{ asset('js/custom.js') }}&quot; defer&gt;&lt;/script&gt;

&lt;div class=&quot;container&quot;&gt;
    &lt;div class=&quot;row justify-content-center&quot;&gt;
        &lt;div class=&quot;col-md-12&quot;&gt;
            &lt;div class=&quot;card&quot;&gt;
                &lt;div class=&quot;card-header&quot;&gt;
                    {{ $user-&gt;name }}
                    &lt;br/&gt;
                    &lt;small&gt;
                        &lt;strong&gt;Website:&lt;/strong&gt; CodePlaners.com, 
                        &lt;strong&gt;Email: &lt;/strong&gt;{{ $user-&gt;email }}
                    &lt;/small&gt;
                &lt;/div&gt;

                &lt;div class=&quot;card-body&quot;&gt;
                    &lt;nav&gt;
                      &lt;div class=&quot;nav nav-tabs&quot; id=&quot;nav-tab&quot; role=&quot;tablist&quot;&gt;
                        &lt;a class=&quot;nav-item nav-link active&quot; id=&quot;nav-home-tab&quot; data-toggle=&quot;tab&quot; href=&quot;#followers&quot; role=&quot;tab&quot; aria-controls=&quot;nav-home&quot; aria-selected=&quot;true&quot;&gt;Followers &lt;span class=&quot;badge badge-primary&quot;&gt;{{ $user-&gt;followers()-&gt;get()-&gt;count() }}&lt;/span&gt;&lt;/a&gt;
                        &lt;a class=&quot;nav-item nav-link&quot; id=&quot;nav-profile-tab&quot; data-toggle=&quot;tab&quot; href=&quot;#following&quot; role=&quot;tab&quot; aria-controls=&quot;nav-profile&quot; aria-selected=&quot;false&quot;&gt;Following &lt;span class=&quot;badge badge-primary&quot;&gt;{{ $user-&gt;followings()-&gt;get()-&gt;count() }}&lt;/span&gt;&lt;/a&gt;
                      &lt;/div&gt;
                    &lt;/nav&gt;
                    &lt;div class=&quot;tab-content&quot; id=&quot;nav-tabContent&quot;&gt;
                      &lt;div class=&quot;tab-pane fade show active&quot; id=&quot;followers&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;nav-home-tab&quot;&gt;
                        &lt;div class=&quot;row pl-5&quot;&gt;
                            @include('userList', &#x5B;'users'=&gt;$user-&gt;followers()-&gt;get()])
                        &lt;/div&gt;
                      &lt;/div&gt;
                      &lt;div class=&quot;tab-pane fade&quot; id=&quot;following&quot; role=&quot;tabpanel&quot; aria-labelledby=&quot;nav-profile-tab&quot;&gt;
                        &lt;div class=&quot;row pl-5&quot;&gt;
                            @include('userList', &#x5B;'users'=&gt;$user-&gt;followings()-&gt;get()])
                        &lt;/div&gt;
                      &lt;/div&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;
@endsection 
</pre>
<p><strong>resources/views/userList.blade.php</strong></p>
<pre class="brush: php; title: ; notranslate">
@if($users-&gt;count())
    @foreach($users as $user)
        &lt;div class=&quot;col-2 profile-box border p-1 rounded text-center bg-light mr-4 mt-3&quot;&gt;
            &lt;img src=&quot;https://codeplaners.com/wp-content/themes/codeplaners/img/favicon.png&quot; style=&quot;height: 50px; width: 50px; border-radius: 50%;&quot; class=&quot;img-responsive&quot;&gt;
            &lt;h5 class=&quot;m-0&quot;&gt;&lt;a href=&quot;{{ route('user.view', $user-&gt;id) }}&quot;&gt;&lt;strong&gt;{{ $user-&gt;name }}&lt;/strong&gt;&lt;/a&gt;&lt;/h5&gt;
            &lt;p class=&quot;mb-2&quot;&gt;
                &lt;small&gt;Following: &lt;span class=&quot;badge badge-primary&quot;&gt;{{ $user-&gt;followings()-&gt;get()-&gt;count() }}&lt;/span&gt;&lt;/small&gt;
                &lt;small&gt;Followers: &lt;span class=&quot;badge badge-primary tl-follower&quot;&gt;{{ $user-&gt;followers()-&gt;get()-&gt;count() }}&lt;/span&gt;&lt;/small&gt;
            &lt;/p&gt;
            &lt;button class=&quot;btn btn-info btn-sm action-follow&quot; data-id=&quot;{{ $user-&gt;id }}&quot;&gt;&lt;strong&gt;
            @if(auth()-&gt;user()-&gt;isFollowing($user))
                UnFollow
            @else
                Follow
            @endif
            &lt;/strong&gt;&lt;/button&gt;
        &lt;/div&gt;
    @endforeach
@endif 
</pre>
<p class="step_code"><strong>Read Also:</strong> <a href="https://codeplaners.com/laravel-8-like-dislike-example/">Laravel Like Dislike Example</a></p>
<p><strong>publis/js/custom.js</strong></p>
<pre class="brush: php; title: ; notranslate">
$(document).ready(function() {     

    $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta&#x5B;name=&quot;csrf-token&quot;]').attr('content')
        }
    });

    $('.action-follow').click(function(){    
        var user_id = $(this).data('id');
        var cObj = $(this);
        var c = $(this).parent(&quot;div&quot;).find(&quot;.tl-follower&quot;).text();

        $.ajax({
           type:'POST',
           url:'/follow',
           data:{user_id:user_id},
           success:function(data){
              console.log(data.success);
              if(jQuery.isEmptyObject(data.success.attached)){
                cObj.find(&quot;strong&quot;).text(&quot;Follow&quot;);
                cObj.parent(&quot;div&quot;).find(&quot;.tl-follower&quot;).text(parseInt(c)-1);
              }else{
                cObj.find(&quot;strong&quot;).text(&quot;UnFollow&quot;);
                cObj.parent(&quot;div&quot;).find(&quot;.tl-follower&quot;).text(parseInt(c)+1);
              }
           }
        });
    });      

}); 
</pre>
<p>Now we are ready to run our project.</p>
<pre class="brush: php; title: ; notranslate">
php artisan serve
</pre>
<p>The post <a rel="nofollow" href="https://codeplaners.com/laravel-8-follow-unfollow-example/">Laravel 8 Follow Unfollow Example</a> appeared first on <a rel="nofollow" href="https://codeplaners.com">Codeplaners</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://codeplaners.com/laravel-8-follow-unfollow-example/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
