资讯专栏INFORMATION COLUMN

laravel5.3 vue 实现收藏夹功能

adie / 3083人阅读

摘要:要定义这种关系,请打开模型并添加一个注意模型的命名空间是所以注意要头部引入第二个参数是数据透视表收藏夹的名称。这将允许插入或更新行时,数据透视表上的时间戳和列将受到影响。

laravel5.3 vue 实现收藏夹功能

本篇是接着laravel中使用WangEditor及多图上传(下篇) 
所以我们这里不演示怎么新建项目了。
1. laravel项目安装


下载之前的项目,完成安装。

1.0 写在之前的(before)

为了避免后面踩到vue版本的坑,请务必阅读此部分
1.0.1 修改package.json
{
  "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迁移表及favoritesup方法

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 在文章控制器添加favoritePostunFavoritePost两个方法
注意要头部要引入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
​

2.7 视图中引入组件

在视图组件使用之前,我们先引入字体文件
resource/views/layouts/app.blade.php
头部引入字体文件

    


并在app.blade.php
添加我的收藏夹链接

// 加在logout-form之后
我的收藏夹


使用组件

// resources/views/home/article/index.blade.php
​
if (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.php
​
extends("layouts.app")
​
@section("content")
@forelse ($myFavorites as $myFavorite)
@if (Auth::check()) @endif
@empty

You have no favorite posts.

@endforelse
@endsection


然后重新向一下根目录
routes/web.php 添加一条路由

Route::get("/", "ArticleController@index");


最后效果图

参考资料 
Implement a Favoriting Feature Using Laravel and Vue.js
laravel 5.4 vue 收藏文章
github地址 https://github.com/pandoraxm/laravel-vue-favorites
原文链接 https://www.bear777.com/blog/laravel5-3-vue

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/92596.html

相关文章

  • laravel5.3 vue 实现藏夹功能

    摘要:要定义这种关系,请打开模型并添加一个注意模型的命名空间是所以注意要头部引入第二个参数是数据透视表收藏夹的名称。这将允许插入或更新行时,数据透视表上的时间戳和列将受到影响。 laravel5.3 vue 实现收藏夹功能 ​ 本篇是接着laravel中使用WangEditor及多图上传(下篇) 所以我们这里不演示怎么新建项目了。​ 1. laravel项目安装 ​下载之前的项目,完成安装。...

    mumumu 评论0 收藏0
  • Laravel实用小功能

    摘要:实用小功能控制访问次数的新特性,通过中间件设置根据控制访问次数原理通过回传三个响应头,,实现控制访问次数。返回的是集合。 Laravel实用小功能 1.控制访问次数 laravel5.2的新特性,通过中间件设置throttle根据IP控制访问次数 原理:通过回传三个响应头X-RateLimit-Limit,X-RateLimit-Remaining,Retry-After实现控制访问次...

    AaronYuan 评论0 收藏0
  • Laravel实用小功能

    摘要:实用小功能控制访问次数的新特性,通过中间件设置根据控制访问次数原理通过回传三个响应头,,实现控制访问次数。返回的是集合。 Laravel实用小功能 1.控制访问次数 laravel5.2的新特性,通过中间件设置throttle根据IP控制访问次数 原理:通过回传三个响应头X-RateLimit-Limit,X-RateLimit-Remaining,Retry-After实现控制访问次...

    Vultr 评论0 收藏0
  • Laravel实用小功能

    摘要:实用小功能控制访问次数的新特性,通过中间件设置根据控制访问次数原理通过回传三个响应头,,实现控制访问次数。返回的是集合。 Laravel实用小功能 1.控制访问次数 laravel5.2的新特性,通过中间件设置throttle根据IP控制访问次数 原理:通过回传三个响应头X-RateLimit-Limit,X-RateLimit-Remaining,Retry-After实现控制访问次...

    twohappy 评论0 收藏0
  • API Token Authentication

    摘要:新增了很多的新特性,包括了内置多用户认证表单数组输入验证隐式路由模型绑定中间件组的定义中间件访问频率限制等主要功能。相对于变化有点大,简化了的目录结构,并将路由分离出来。由于已将的路由单独分离出来,因此只需在中添加路由规则。 Laravel 5.2 新增了很多的新特性,包括了内置多用户认证、表单数组输入验证、隐式路由模型绑定、中间件组的定义、中间件 throttle 访问频率限制等主要...

    KitorinZero 评论0 收藏0

发表评论

0条评论

adie

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<