欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

laravel5.3 vue 實現(xiàn)收藏夾功能實例詳解

 更新時間:2018年01月21日 11:08:17   作者:iMax  
這篇文章主要介紹了laravel5.3 vue 實現(xiàn)收藏夾功能,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

下面通過本文給大家介紹laravel5.3 vue 實現(xiàn)收藏夾功能,具體代碼如下所述:

{
 "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 模塊

(如果之前沒有執(zhí)行此操作) ​

npm  install

 

1.2 創(chuàng)建模型及遷移

我們需要一個User模型(laravel附帶),一個Post模型和一個Favorite模型以及它們各自的遷移文件。 因為我們之前創(chuàng)建過了 Post 的模型,所以我們只需要創(chuàng)建一個 Favorite 模型即可。 ​

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

 

​​ 這會創(chuàng)建一個 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 用戶認證

因為我們之前就已經(jīng)創(chuàng)建過了,所以這里就不需要重復(fù)創(chuàng)建了。

如果你沒有創(chuàng)建過用戶認證模塊,則需要執(zhí)行 php artisan make:auth ​

2. 完成搜藏夾功能

修改 routes/web.php

2.1 創(chuàng)建路由器


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

2.2 文章和用戶之間多對多關(guān)系

由于用戶可以將許多文章標記為收藏夾,并且一片文章可以被許多用戶標記為收藏夾,所以用戶與最收藏的文章之間的關(guān)系將是多對多的關(guān)系。要定義這種關(guān)系,請打開 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();
 }

​ 第二個參數(shù)是數(shù)據(jù)透視表(收藏夾)的名稱。第三個參數(shù)是要定義關(guān)系(User)的模型的外鍵名稱(user_id),而第四個參數(shù)是要加入的模型(Post)的外鍵名稱(post_id)。 ​ 注意到我們鏈接withTimeStamps()到belongsToMany()。這將允許插入或更新行時,數(shù)據(jù)透視表上的時間戳(create_at和updated_at)列將受到影響。 ​ ​

2.3 創(chuàng)建文章控制器

因為我們之前創(chuàng)建過了,這里也不需要創(chuàng)建了。

如果你沒有創(chuàng)建過,請執(zhí)行 php artisan make:controller ArticleController ​

2.4 在文章控制器添加 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();
 }
}

​2.5 集成 axios 模塊

•安裝axios ​

npm install axios --save

​•引入axios模塊 resource/assets/js/bootstrap.js 在最后加入 ​

import axios from 'axios';
window.axios = axios;

2.6 創(chuàng)建收藏夾組件

// 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'],
​
 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>

2.7 視圖中引入組件

在視圖組件使用之前,我們先引入字體文件 resource/views/layouts/app.blade.php 頭部引入字體文件 ​

<link rel="stylesheet"  />

​ 并在 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') }}" rel="external nofollow" >我的收藏夾</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

​ 然后我們要創(chuàng)建 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();
 }​

2.8 使用組件

引入 Favorite.vue 組件 resources/assets/js/app.js ​

Vue.component('favorite', require('./components/Favorite.vue'));

編譯

npm run dev

效果圖 ​

 

3. 完成 我的收藏夾

3.1 創(chuàng)建用戶控制器​

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 }}" rel="external nofollow" >
    {{ $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');

最后效果圖

總結(jié)

以上所述是小編給大家介紹的laravel5.3 vue 實現(xiàn)收藏夾功能,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復(fù)大家的!

相關(guān)文章

  • vue實現(xiàn)ajax滾動下拉加載,同時具有l(wèi)oading效果(推薦)

    vue實現(xiàn)ajax滾動下拉加載,同時具有l(wèi)oading效果(推薦)

    這篇文章主要介紹了vue實現(xiàn)ajax滾動下拉加載,同時具有l(wèi)oading效果的實現(xiàn)代碼,文章包括難點說明,介紹的非常詳細,感興趣的朋友參考下
    2017-01-01
  • 如何使用el-table+el-tree+el-select動態(tài)選擇對應(yīng)值

    如何使用el-table+el-tree+el-select動態(tài)選擇對應(yīng)值

    小編在做需求時,遇到了在el-table表格中加入多條數(shù)據(jù),并且每條數(shù)據(jù)要通過el-select來選取相應(yīng)的值,做到動態(tài)選擇,下面這篇文章主要給大家介紹了關(guān)于如何使用el-table+el-tree+el-select動態(tài)選擇對應(yīng)值的相關(guān)資料,需要的朋友可以參考下
    2023-01-01
  • vue Render中slots的使用的實例代碼

    vue Render中slots的使用的實例代碼

    本篇文章主要介紹了vue Render中slots的使用的實例代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-07-07
  • vue中復(fù)用vuex.store對象的定義

    vue中復(fù)用vuex.store對象的定義

    這篇文章主要介紹了vue中復(fù)用vuex.store對象的定義,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • Vue中的計算屬性與監(jiān)聽屬性

    Vue中的計算屬性與監(jiān)聽屬性

    這篇文章介紹了Vue中的計算屬性與監(jiān)聽屬性,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-03-03
  • Vue3實現(xiàn)圖片放大鏡效果

    Vue3實現(xiàn)圖片放大鏡效果

    這篇文章主要為大家詳細介紹了Vue3實現(xiàn)圖片放大鏡效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Vue3中pinia的使用與持久化處理詳解

    Vue3中pinia的使用與持久化處理詳解

    Pinia?是一個基于?Vue?3?的狀態(tài)管理庫,可以更好地支持?TypeScript?和更靈活的狀態(tài)管理方式,本文主要介紹了pinia的使用與持久化處理,需要的可以參考一下
    2023-07-07
  • Vue新玩具VueUse的具體用法

    Vue新玩具VueUse的具體用法

    VueUse 是一個基于 Composition API 的實用函數(shù)集合。本文就詳細的介紹了VueUse的具體用法,具有一定的參考價值,感興趣的可以了解一下
    2021-11-11
  • Vue實現(xiàn)驗證碼功能

    Vue實現(xiàn)驗證碼功能

    這篇文章主要為大家詳細介紹了Vue實現(xiàn)驗證碼功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • 關(guān)于nuxt?store中保存localstorage的問題

    關(guān)于nuxt?store中保存localstorage的問題

    這篇文章主要介紹了關(guān)于nuxt?store中保存localstorage的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10

最新評論