本篇是接着laravel中使用WangEditor及多图上传(下篇) 因此咱们这里不演示怎么新建项目了。 php
下载以前的项目,完成安装。 css
为了不后面踩到vue版本的坑,请务必阅读此部分 vue
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"
}
}
复制代码
gulpfile.js
将原来的require('laravel-elixir-vue');
修改成require('laravel-elixir-vue-2');
jquery
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');
});
复制代码
resource/assets/js/app.js
将原来的el: 'body'
改成el: '#app'
webpack
const app = new Vue({
el: '#app'
});
复制代码
(若是以前没有执行此操做) ios
npm install
复制代码
咱们须要一个User模型(laravel附带),一个Post模型和一个Favorite模型以及它们各自的迁移文件。 由于咱们以前建立过了Post
的模型,因此咱们只须要建立一个Favorite
模型便可。 laravel
php artisan make:model App\Models\Favorite -m
复制代码
Favorite
模型以及迁移文件。
posts
迁移表及favorites
的up
方法给posts
表在id
字段后面新增一个user_id
字段 git
php artisan make:migration add_userId_to_posts_table --table=posts
复制代码
修改database/migrations/2018_01_18_145843_add_userId_to_posts_table.php
github
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
web
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
复制代码
由于咱们以前就已经建立过了,因此这里就不须要重复建立了。
若是你没有建立过用户认证模块,则须要执行
php artisan make:auth
修改routes/web.php
Auth::routes();
Route::post('favorite/{post}', 'ArticleController@favoritePost');
Route::post('unfavorite/{post}', 'ArticleController@unFavoritePost');
Route::get('my_favorites', 'UsersController@myFavorites')->middleware('auth');
复制代码
因为用户能够将许多文章标记为收藏夹,而且一片文章能够被许多用户标记为收藏夹,因此用户与最收藏的文章之间的关系将是多对多的关系。要定义这种关系,请打开User
模型并添加一个favorites()
app/User.php
注意
post
模型的命名空间是App\Models\Post
因此注意要头部引入use App\Models\Post;
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)列将受到影响。
由于咱们以前建立过了,这里也不须要建立了。
若是你没有建立过,请执行
php artisan make:controller ArticleController
favoritePost
和unFavoritePost
两个方法注意要头部要引入
use Illuminate\Support\Facades\Auth;
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use Illuminate\Support\Facades\Auth;
class ArticleController extends Controller
{
public function index()
{
$data = Post::paginate(5);
return view('home.article.index', compact('data'));
}
public function show($id)
{
$data = Post::find($id);
return view('home.article.list', compact('data'));
}
public function favoritePost(Post $post)
{
Auth::user()->favorites()->attach($post->id);
return back();
}
public function unFavoritePost(Post $post)
{
Auth::user()->favorites()->detach($post->id);
return back();
}
}
复制代码
axios
模块npm install axios --save
复制代码
resource/assets/js/bootstrap.js
在最后加入 import axios from 'axios';
window.axios = axios;
复制代码
// resources/assets/js/components/Favorite.vue
<template>
<span>
<a href="#" v-if="isFavorited" @click.prevent="unFavorite(post)">
<i class="fa fa-heart"></i>
</a>
<a href="#" v-else @click.prevent="favorite(post)">
<i class="fa fa-heart-o"></i>
</a>
</span>
</template>
<script>
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));
}
}
}
</script>
复制代码
在视图组件使用以前,咱们先引入字体文件 resource/views/layouts/app.blade.php
头部引入字体文件
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" />
复制代码
并在app.blade.php
添加个人收藏夹
连接
// 加在logout-form以后
<form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
{{ csrf_field() }}
</form>
<a href="{{ url('my_favorites') }}">个人收藏夹</a>
复制代码
使用组件
// resources/views/home/article/index.blade.php
if (Auth::check())
<div class="panel-footer">
<favorite
:post={{ $list->id }}
:favorited={{ $list->favorited() ? 'true' : 'false' }}
></favorite>
</div>
endif
复制代码
而后咱们要建立favorited()
打开app/Models/Post.php
增长favorited()
方法
注意要在头部引用命名空间
use App\Models\Favorite;
use Illuminate\Support\Facades\Auth;
public function favorited()
{
return (bool) Favorite::where('user_id', Auth::id())
->where('post_id', $this->id)
->first();
}
复制代码
引入Favorite.vue
组件 resources/assets/js/app.js
Vue.component('favorite', require('./components/Favorite.vue'));
复制代码
编译
npm run dev
复制代码
个人收藏夹
php artisan make:controller UsersController
复制代码
修改app/Http/Controllers/UsersController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class UsersController extends Controller
{
public function myFavorites()
{
$myFavorites = Auth::user()->favorites;
return view('users.my_favorites', compact('myFavorites'));
}
}
复制代码
添加视图文件
// resources/views/users/my_favorites.blade.php
extends('layouts.app')
@section('content')
<div class="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 }}">
{{ $myFavorite->title }}
</a>
</div>
<div class="panel-body" style="max-height:300px;overflow:hidden;">
<img src="/uploads/{!! ($myFavorite->cover)[0] !!}" style="max-width:100%;overflow:hidden;" alt="">
</div>
@if (Auth::check())
<div class="panel-footer">
<favorite
:post={{ $myFavorite->id }}
:favorited={{ $myFavorite->favorited() ? 'true' : 'false' }}
></favorite>
</div>
@endif
</div>
@empty
<p>You have no favorite posts.</p>
@endforelse
</div>
</div>
</div>
@endsection
复制代码
而后从新向一下根目录 routes/web.php
添加一条路由
Route::get('/', 'ArticleController@index');
复制代码
最后效果图
参考资料 Implement a Favoriting Feature Using Laravel and Vue.js
github地址 github.com/pandoraxm/l…