laravel5.3开发知乎-第10章-关注用户 laravel5.3开发知乎-第10章-关注用户

2023-07-13

一、创建关注表

生成关注表

php artisan make:migration create_followers_table --create=followers

修改生成的 create_followers_table.php

public function up()
{
   Schema::create('followers', function (Blueprint $table) {
       $table->increments('id');
       $table->integer('follower_id')->unsigned()->index();
       $table->integer('followed_id')->unsigned()->index();
       $table->timestamps();
   });
}

执行数据迁移

php artisan migrate

修改 app/User.php

public function followers()
{
   return $this->belongsToMany(self::class, 'followers', 'follower_id', 'followed_id')->withTimestamps();
}

public function followersUser()
{
   return $this->belongsToMany(self::class, 'followers', 'followed_id', 'follower_id')->withTimestamps();
}

public function followThisUser($user)
{
   return $this->followers()->toggle($user);
}

二、注册路由

新建 app/Repositories/UserRepository.php

namespace App\Repositories;

use App\User;

class UserRepository
{
   public function byId($id)
   {
       return User::find($id);
   }
}

修改 routes/api.php

Route::post('/user/follower', 'FollowersController@index')->middleware('auth:api');
Route::post('/user/follow', 'FollowersController@follow')->middleware('auth:api');

创建控制器

php artisan make:controller FollowersController

修改 app/Http/Controllers/FollowersController.php

namespace App\Http\Controllers;

use App\Repositories\UserRepository;
use Illuminate\Http\Request;
use Auth;

class FollowersController extends Controller
{
   protected $userRepository;

   public function __construct(UserRepository $userRepository)
   {
       $this->userRepository = $userRepository;
   }

   public function index(Request $request)
   {
       $user = $this->userRepository->byId($request->get('user'));
       $followers = $user->followersUser()->pluck('follower_id')->toArray();
       if(in_array(user('api')->id, $followers)){
           $followed = true;
       }else{
           $followed = false;
       }
       return response()->json(['followed'=>$followed]);
   }

   public function follow()
   {
       $userToFollow = $this->userRepository->byId(request('user'));
       $followed = user('api')->followThisUser($userToFollow->id);
       if(count($followed['attached']) > 0){
           $userToFollow->increment('followers_count');
           $followed = true;
       }else{
           $userToFollow->decrement('followers_count');
           $followed = false;
       }
       return response()->json(['followed'=>$followed]);
   }
}

三、vue 组件

新建 resources/assets/js/components/UserFollowButton.vue

https://file.lulublog.cn/images/3/2023/07/SEz6a6fuF1J9IF9ahzaI1Aj92cMpH0.jpg

注册组件:修改 resources/assets/js/app.js

Vue.component('user-follow-button', require('./components/UserFollowButton.vue'));

执行 gulp

gulp

修改 resources/views/questions/show.blade.php

https://file.lulublog.cn/images/3/2023/07/LJTYDKlD0py7lPTrIz0dTRTLPLlOdz.png

四、站内通知

生成 notification 表

php artisan notifications:table

执行数据迁移

php artisan migrate

生成 notification

php artisan make:notification NewUserFollowNotification

修改生成的文件:

public function via($notifiable)
{
   return ['database'];
}

public function toDatabase($notifiable)
{
   return [
       'name' => user('api')->name,
   ];
}

修改 app/Http/Controllers/FollowersController.php

use App\Notifications\NewUserFollowNotification;

public function follow()
{
   $userToFollow = $this->userRepository->byId(request('user'));
   $followed = user('api')->followThisUser($userToFollow->id);
   if(count($followed['attached']) > 0){
       $userToFollow->increment('followers_count');
       $followed = true;
       $userToFollow->notify(new NewUserFollowNotification());
   }else{
       $userToFollow->decrement('followers_count');
       $followed = false;
   }
   return response()->json(['followed'=>$followed]);
}

创建控制器

php artisan make:controller NotificationsController

修改生成的控制器:app/Http/Controllers/NotificationsController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;

class NotificationsController extends Controller
{
   public function index()
   {
       $user = Auth::user();
       return view('notifications/index', compact('user'));
   }
}

注册路由:修改 routes/web.php

Route::get('notifications', 'NotificationsController@index');

新建 resources/views/notifications/index.blade.php

https://file.lulublog.cn/images/3/2023/07/STm1sBBBoFNZ82YQSMSfMMfFOMOhfm.jpg

新建 resources/views/notifications/new_user_follow_notification.blade.php

https://file.lulublog.cn/images/3/2023/07/Q9W1Bf3zHbjjjJ9f43o0TfbHdtdtTk.jpg

五、邮件通知

新建 app/Channels/qqMailChannel.php

namespace App\Channels;

use Illuminate\Notifications\Notification;

class qqMailChannel
{
   public function send($notifiable, Notification $notification)
   {
       $message = $notification->toQqMail($notifiable);
   }
}

新建 resources/views/email/follow.blade.php

{{ config('app.name') }}上,{{ $name }} 关注了你: {{ $url }}

修改 routes/web.php

Route::get('/', ['as'=>'home','uses'=>'QuestionsController@index']);

创建邮件模板

php artisan make:mail SomebodyFocusYou

修改 app/Mail/SomebodyFocusYou.php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class SomebodyFocusYou extends Mailable
{
   use Queueable, SerializesModels;

   private $data;

   public function __construct($data)
   {
       $this->data = $data;
   }

   public function build()
   {
       return $this->view('email.follow', $this->data);
   }
}

修改 app/Notifications/NewUserFollowNotification.php

use App\Channels\qqMailChannel;
use Auth;
use Mail;

public function via($notifiable)
{
   return ['database', qqMailChannel::class];
}

public function toQqMail($notifiable)
{
   $data = [
       'url' => route('home'),
       'name' => user('api')->name,
   ];
   Mail::to($notifiable->email)->send(new \App\Mail\SomebodyFocusYou($data));
}

public function toQqMailOld($notifiable)
{
   $data = [
       'url' => route('home'),
       'name' => user('api')->name,
   ];
   Mail::send('email.follow', $data, function ($message) use($notifiable){
       $subject = config('app.name').'上有人关注了你';
       $message->to($notifiable->email)->subject($subject);
   });
}

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开微信扫一扫,即可进行扫码打赏哦

阅读 245