本文主和大家介绍laravel5.3 vue 实现收藏夹功能,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下,希望能帮助到大家。
下面通过本文给大家介绍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" }}
登录后复制
1.0.2 修改 gulpfile.js
将原来的 require(‘laravel-elixir-vue’); 修改为 require(‘laravel-elixir-vue-2’);
const elixir = require('laravel-elixir');require('laravel-elixir-vue-2');/* |-------------------------------------------------------------------------- | Elixir Asset Management |-------------------------------------------------------------------------- | | Elixir provides a clean, fluent API for defining some basic Gulp tasks | for your Laravel application. By default, we are compiling the Sass | file for our application, as well as publishing vendor resources. | */elixir(mix => { mix.sass('app.scss') .webpack('app.js');});
登录后复制
1.0.3 修改 resource/assets/js/app.js
立即学习“前端免费学习笔记(深入)”;
将原来的 el: ‘body’ 改为 el: ‘#app’
const app = new Vue({ el: '#app'});
登录后复制
1.1 安装npm 模块
(如果之前没有执行此操作)
npm install
1.2 创建模型及迁移
我们需要一个User模型(laravel附带),一个Post模型和一个Favorite模型以及它们各自的迁移文件。 因为我们之前创建过了 Post 的模型,所以我们只需要创建一个 Favorite 模型即可。
php artisan make:model AppModelsFavorite -m
登录后复制
这会创建一个 Favorite
模型以及迁移文件。
1.3 修改 posts 迁移表及 favorites 的 up 方法
给 posts 表在 id 字段后面新增一个 user_id 字段
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 public function up() { Schema::create('favorites', function (Blueprint $table) { $table->increments('id'); $table->integer('user_id')->unsigned(); $table->integer('post_id')->unsigned(); $table->timestamps(); }); }
登录后复制
该 favorites 表包含两列:
user_id 被收藏文章的用户ID。
post_id 被收藏的帖子的ID。
然后进行表迁移
php artisan migrate
1.4 用户认证
因为我们之前就已经创建过了,所以这里就不需要重复创建了。
如果你没有创建过用户认证模块,则需要执行 php artisan make:auth
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() app/User.php
注意 post 模型的命名空间是 AppModelsPost 所以注意要头部引入 use AppModelsPost;
public function favorites() { return $this->belongsToMany(Post::class, 'favorites', 'user_id', 'post_id')->withTimeStamps(); }
登录后复制
第二个参数是数据透视表(收藏夹)的名称。第三个参数是要定义关系(User)的模型的外键名称(user_id),而第四个参数是要加入的模型(Post)的外键名称(post_id)。 注意到我们链接withTimeStamps()到belongsToMany()。这将允许插入或更新行时,数据透视表上的时间戳(create_at和updated_at)列将受到影响。
2.3 创建文章控制器
因为我们之前创建过了,这里也不需要创建了。
如果你没有创建过,请执行 php artisan make:controller ArticleController
2.4 在文章控制器添加 favoritePost 和 unFavoritePost 两个方法
注意要头部要引入 use IlluminateSupportFacadesAuth;
favorites()->attach($post->id); return back(); } public function unFavoritePost(Post $post) { Auth::user()->favorites()->detach($post->id); return back(); }}
登录后复制
2.5 集成 axios 模块
•安装axios
npm install axios –save
•引入axios模块 resource/assets/js/bootstrap.js 在最后加入
import axios from 'axios';window.axios = axios;
登录后复制
2.6 创建收藏夹组件
// resources/assets/js/components/Favorite.vue export default { props: ['post', 'favorited'], data: function() { return { isFavorited: '', } }, mounted() { this.isFavorited = this.isFavorite ? true : false; }, computed: { isFavorite() { return this.favorited; }, }, methods: { favorite(post) { axios.post('/favorite/'+post) .then(response => this.isFavorited = true) .catch(response => console.log(response.data)); }, unFavorite(post) { axios.post('/unfavorite/'+post) .then(response => this.isFavorited = false) .catch(response => console.log(response.data)); } } }
登录后复制
2.7 视图中引入组件
在视图组件使用之前,我们先引入字体文件 resource/views/layouts/app.blade.php 头部引入字体文件
登录后复制
并在 app.blade.php 添加 我的收藏夹 链接
// 加在logout-form之后 {{ csrf_field() }}我的收藏夹
登录后复制
使用组件
// resources/views/home/article/index.blade.phpif (Auth::check())
登录后复制
endif
然后我们要创建 favorited() 打开 app/Models/Post.php 增加 favorited() 方法
注意要在头部引用命名空间 use AppModelsFavorite; use IlluminateSupportFacadesAuth;
public function favorited() { return (bool) Favorite::where('user_id', Auth::id()) ->where('post_id', $this->id) ->first(); }
登录后复制
2.8 使用组件
引入 Favorite.vue 组件 resources/assets/js/app.js
Vue.component('favorite', require('./components/Favorite.vue'));
登录后复制
编译
npm run dev
登录后复制
效果图
3. 完成 我的收藏夹
3.1 创建用户控制器
php artisan make:controller UsersController
登录后复制
修改
app/Http/Controllers/UsersController.php favorites; return view('users.my_favorites', compact('myFavorites')); }}
登录后复制
添加视图文件
// resources/views/users/my_favorites.blade.phpextends('layouts.app')@section('content')
My Favorites
@forelse ($myFavorites as $myFavorite)
id }}" rel="external nofollow" > {{ $myFavorite->title }}
@if (Auth::check()) @endif @emptyYou have no favorite posts.
@endforelse @endsection
登录后复制
然后重新向一下根目录 routes/web.php 添加一条路由
Route::get('/', 'ArticleController@index');
登录后复制
最后效果图
相关推荐:
js Firefox 加入收藏夹功能代码 兼容Firefox 和 IE_javascript技巧
JavaScript加入收藏夹功能(兼容IE、firefox、chrome)_javascript技巧
原生JS实现加入收藏夹的代码_javascript技巧
以上就是laravel5.3 vue 实现收藏夹功能的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2789177.html