使用vue如何實現收藏夾
這篇文章主要介紹了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 App\Models\Favorite -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_adduser_table_togrations/2018_01_18_145843_adduser_table_togrations ##
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(); }); }
php artisan migrate
1.4 使用者認證
因為我們之前就已經創建過了,所以這裡就不需要重複創建了。 如果你沒有建立過使用者認證模組,你需要執行php artisan make:auth2. 完成搜藏夾功能
修改routes/web.php2.1 建立路由器Auth::routes(); Route::post('favorite/{post}', 'ArticleController@favoritePost'); Route::post('unfavorite/{post}', 'ArticleController@unFavoritePost'); Route::get('my_favorites', 'UsersController@myFavorites')->middleware('auth');
public function favorites() { return $this->belongsToMany(Post::class, 'favorites', 'user_id', 'post_id')->withTimeStamps(); }
2.3 建立文章控制器
因為我們之前已經建立過了,這裡也不需要建立了。 如果你沒有創建過,請執行php artisan make:controller ArticleController2.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(); } }
•引入axios模組resource/assets/js/bootstrap.js 在最後加入
import axios from 'axios'; window.axios = axios;
// 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>
在視圖元件使用之前,我們先引入字體檔案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') }}" rel="external nofollow" >我的收藏夹</a>
使用元件
// resources/views/home/article/index.blade.php if (Auth::check()) <p class="panel-footer"> <favorite :post={{ $list->id }} :favorited={{ $list->favorited() ? 'true' : 'false' }} ></favorite> </p>
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
效果圖
3. 完成我的最愛
3.1 建立使用者控制器##
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') <p class="container"> <p class="row"> <p class="col-md-8 col-md-offset-2"> <p class="page-header"> <h3>My Favorites</h3> </p> @forelse ($myFavorites as $myFavorite) <p class="panel panel-default"> <p class="panel-heading"> <a href="/article/{{ $myFavorite->id }}" rel="external nofollow" > {{ $myFavorite->title }} </a> </p> <p class="panel-body" style="max-height:300px;overflow:hidden;"> <img src="/uploads/{!! ($myFavorite->cover)[0] !!}" style="max-width:100%;overflow:hidden;" alt=""> </p> @if (Auth::check()) <p class="panel-footer"> <favorite :post={{ $myFavorite->id }} :favorited={{ $myFavorite->favorited() ? 'true' : 'false' }} ></favorite> </p> @endif </p> @empty <p>You have no favorite posts.</p> @endforelse </p> </p> </p> @endsection
Route::get('/', 'ArticleController@index');
上面是我整理給大家的,希望未來會對大家有幫助。
相關文章:
以上是使用vue如何實現收藏夾的詳細內容。更多資訊請關注PHP中文網其他相關文章!

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

Laravel 是一款 PHP 框架,用於輕鬆構建 Web 應用程序。它提供一系列強大的功能,包括:安裝: 使用 Composer 全局安裝 Laravel CLI,並在項目目錄中創建應用程序。路由: 在 routes/web.php 中定義 URL 和處理函數之間的關係。視圖: 在 resources/views 中創建視圖以呈現應用程序的界面。數據庫集成: 提供與 MySQL 等數據庫的開箱即用集成,並使用遷移來創建和修改表。模型和控制器: 模型表示數據庫實體,控制器處理 HTTP 請求。

Netflix使用React作為其前端框架。 1)React的組件化開發模式和強大生態系統是Netflix選擇它的主要原因。 2)通過組件化,Netflix將復雜界面拆分成可管理的小塊,如視頻播放器、推薦列表和用戶評論。 3)React的虛擬DOM和組件生命週期優化了渲染效率和用戶交互管理。

在使用CraftCMS開發網站時,常常會遇到資源文件緩存的問題,特別是當你頻繁更新CSS和JavaScript文件時,舊版本的文件可能仍然被瀏覽器緩存,導致用戶無法及時看到最新的更改。這個問題不僅影響用戶體驗,還會增加開發和調試的難度。最近,我在項目中遇到了類似的困擾,經過一番探索,我找到了wiejeben/craft-laravel-mix這個插件,它完美地解決了我的緩存問題。

Laravel 提供了一個全面的 Auth 框架,用於實現用戶登錄功能,包括:定義用戶模型(Eloquent 模型)創建登錄表單(Blade 模板引擎)編寫登錄控制器(繼承 Auth\LoginController)驗證登錄請求(Auth::attempt)登錄成功後重定向(redirect)考慮安全因素:哈希密碼、防 CSRF 保護、速率限制和安全標頭。此外,Auth 框架還提供重置密碼、註冊和驗證電子郵件等功能。詳情請參閱 Laravel 文檔:https://laravel.com/doc

文章摘要:本文提供了詳細分步說明,指導讀者如何輕鬆安裝 Laravel 框架。 Laravel 是一個功能強大的 PHP 框架,它 упростил 和加快了 web 應用程序的開發過程。本教程涵蓋了從系統要求到配置數據庫和設置路由等各個方面的安裝過程。通過遵循這些步驟,讀者可以快速高效地為他們的 Laravel 項目打下堅實的基礎。

Laravel框架內置了多種方法來方便地查看其版本號,滿足開發者的不同需求。本文將探討這些方法,包括使用Composer命令行工具、訪問.env文件或通過PHP代碼獲取版本信息。這些方法對於維護和管理Laravel應用程序的版本控制至關重要。

在面向初学者的 Laravel 框架版本选择指南中,本文深入探討了 Laravel 的版本差異,旨在協助初學者在眾多版本之間做出明智的選擇。我們將重點介紹每個版本的關鍵特徵、比較它們的優缺點,並提供有用的建議,幫助新手根據他們的技能水準和項目需求挑選最合適的 Laravel 版本。對於初學者來說,選擇一個合適的 Laravel 版本至關重要,因為它可以顯著影響他們的學習曲線和整體開發體驗。

想要學習 Laravel 框架,但苦於沒有資源或經濟壓力?本文為你提供了免費學習 Laravel 的途徑,教你如何利用網絡平台、文檔和社區論壇等資源,從入門到掌握,為你的 PHP 開發之旅奠定堅實基礎。
