富贵资源网 Design By www.hznty.com

下面通过本文给大家介绍laravel5.3 vue 实现收藏夹功能,具体代码如下所述:

{
 "private": true,
 "scripts": {
 "prod": "gulp --production",
 "dev": "gulp watch"
 },
 "devDependencies": {
 "bootstrap-sass": "^3.3.7",
 "gulp": "^3.9.1",
 "jquery": "^3.1.0",
 "laravel-elixir": "^6.0.0-14",
 "laravel-elixir-vue-2": "^0.2.0",
 "laravel-elixir-webpack-official": "^1.0.2",
 "lodash": "^4.16.2",
 "vue": "^2.0.1",
 "vue-resource": "^1.0.3"
 }
}

"htmlcode">

const elixir = require('laravel-elixir');"htmlcode">
const app = new Vue({
 el: '#app'
});

1.1 安装npm 模块

(如果之前没有执行此操作) "text-align: center">laravel5.3 vue 实现收藏夹功能实例详解 

1.2 创建模型及迁移

我们需要一个User模型(laravel附带),一个Post模型和一个Favorite模型以及它们各自的迁移文件。 因为我们之前创建过了 Post 的模型,所以我们只需要创建一个 Favorite 模型即可。 "htmlcode">

php artisan make:model App\Models\Favorite -m

laravel5.3 vue 实现收藏夹功能实例详解 

"htmlcode">

php artisan make:migration add_userId_to_posts_table --table=posts

修改 database/migrations/2018_01_18_145843_add_userId_to_posts_table.php

public function up()
 {
 Schema::table('posts', function (Blueprint $table) {
  $table->integer('user_id')->unsigned()->after('id');
 });
 }
database/migrations/2018_01_18_142146_create_favorites_table.php "color: #ff0000">2. 完成搜藏夹功能

修改 routes/web.php

2.1 创建路由器


Auth::routes();
Route::post('favorite/{post}', 'ArticleController@favoritePost');
Route::post('unfavorite/{post}', 'ArticleController@unFavoritePost');
Route::get('my_favorites', 'UsersController@myFavorites')->middleware('auth');

2.2 文章和用户之间多对多关系

由于用户可以将许多文章标记为收藏夹,并且一片文章可以被许多用户标记为收藏夹,所以用户与最收藏的文章之间的关系将是多对多的关系。要定义这种关系,请打开 User 模型并添加一个 favorites() "htmlcode">

public function favorites()
 {
 return $this->belongsToMany(Post::class, 'favorites', 'user_id', 'post_id')->withTimeStamps();
 }

"htmlcode">

<"htmlcode">
import axios from 'axios';
window.axios = axios;

2.6 创建收藏夹组件

// resources/assets/js/components/Favorite.vue
<template>
 <span>
 <a href="#" rel="external nofollow" rel="external nofollow" v-if="isFavorited" @click.prevent="unFavorite(post)">
  <i class="fa fa-heart"></i>
 </a>
 <a href="#" rel="external nofollow" rel="external nofollow" v-else @click.prevent="favorite(post)">
  <i class="fa fa-heart-o"></i>
 </a>
 </span>
</template>
<script>
 export default {
 props: ['post', 'favorited'],
"htmlcode">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />

"htmlcode">

// 加在logout-form之后
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
 {{ csrf_field() }}
</form>"{{ url('my_favorites') }}" rel="external nofollow" >我的收藏夹</a>

"htmlcode">

// resources/views/home/article/index.blade.php"panel-footer">
 <favorite
  :post={{ $list->id }}
  :favorited={{ $list->favorited() "htmlcode">
public function favorited()
 {
 return (bool) Favorite::where('user_id', Auth::id())
    ->where('post_id', $this->id)
    ->first();
 }"htmlcode">
Vue.component('favorite', require('./components/Favorite.vue'));

编译

npm run dev

"" src="/UploadFiles/2021-04-02/2018012111254213.png">

效果图 "text-align: center">laravel5.3 vue 实现收藏夹功能实例详解 

3. 完成 我的收藏夹

3.1 创建用户控制器"htmlcode">

php artisan make:controller UsersController

"htmlcode">

app/Http/Controllers/UsersController.php "htmlcode">
// resources/views/users/my_favorites.blade.php
"container">
 <div class="row">
 <div class="col-md-8 col-md-offset-2">
  <div class="page-header">
  <h3>My Favorites</h3>
  </div>
  @forelse ($myFavorites as $myFavorite)
  <div class="panel panel-default">
   <div class="panel-heading">
   <a href="/article/{{ $myFavorite->id }}" rel="external nofollow" >
    {{ $myFavorite->title }}
   </a>
   </div>
"panel-body" style="max-height:300px;overflow:hidden;">
   <img src="/UploadFiles/2021-04-02/{!! ($myFavorite->cover)[0] !!}">
Route::get('/', 'ArticleController@index');

"" src="/UploadFiles/2021-04-02/2018012111254215.gif">

最后效果图

总结

以上所述是小编给大家介绍的laravel5.3 vue 实现收藏夹功能,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!

富贵资源网 Design By www.hznty.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
富贵资源网 Design By www.hznty.com