いいね機能を作りたいですが、view.blade.phpにボタンは表示されるけれど押してもそのまま何も起こりません。
こんな状態で止まります。
npm run dev を実行してもエラーは出ません。
どなたか解決方法を教えてください。
**app.js **
javascript
1import Vue from 'vue'; 2import VueRouter from 'vue-router'; 3/** 4 * First we will load all of this project's JavaScript dependencies which 5 * includes Vue and other libraries. It is a great starting point when 6 * building robust, powerful web applications using Vue and Laravel. 7 */ 8 9require('./bootstrap'); 10 11window.Vue = require('vue'); 12 13// vue-routerを使う宣言 14Vue.use(VueRouter); 15 16 17/** 18 * Next, we will create a fresh Vue application instance and attach it to 19 * the page. Then, you may begin adding components to this application 20 * or customize the JavaScript scaffolding to fit your unique needs. 21 */ 22 23Vue.component('example-component', require('./components/ExampleComponent.vue')); 24 25Vue.component('like', require('./components/Like.vue')); 26 27 28const app = new Vue({ 29 router, 30 el: '#app' 31}); 32 33const router = new VueRouter({ 34 mode: 'history', 35 routes: [ 36 { path: '/like', component: require('./components/Like.vue') }, 37 ] 38}); 39
Like.vue
javascript
1<template> 2 3<like :postid="1" inline-template> 4<button class="button is-medium" @click="iLikeIt":disabled="disabled" >いいね</button> 5</like> 6 7</template> 8 9<script> 10export default{ 11props:['postid'], 12 13data(){ 14return{ 15disabled:false 16} 17}, 18 19methods:{ 20iLikeIt(){ 21this.disabled=true; 22axios.post('/like', { 23id:this.postid.id 24}) 25.then(function (response) { 26console.log(response); 27}); 28} 29} 30 31} 32</script> 33
web.php
php
1Route::post('/like','ArticleController@like')->name('like'); 2
view.blade.php
php
1@extends('layouts.app') 2 3@section('content') 4<div class="main"> 5 <div class="contents"> 6 @foreach($article as $art) 7 <div class="head_contents"> 8 9 <ul class="user"> 10 <li class="top"><a href="{{route('profile',$art->author)}}"><img src="/images/{{$art->user->p_image}}" class="maru"></a></li> 11 <li class="top">{{$art->user->username}}</li> 12 <li class="top"><small>{{$art->created_at->format('Y年m月d日')}}</small></li> 13 </ul> 14 </div> 15 <h1>{{$art->title}}</h1> 16 {{$art->tag}} 17 18<br> 19 20 21 22 23 <like></like> 24 25 <hr> 26<p>{!! $art->article !!}</p> 27 @endforeach 28 </div> 29 <div class="menu"> 30 <h5 class="new_article">記事</h5> 31 <div class="new"> 32 @foreach($new as $n) 33 <ul> 34 <li class="new_li"><a href="/article/past/{{$n->id}}" class="titles">{{$n->title}}</a></li> 35 </ul> 36 @endforeach 37 </div> 38 </div> 39</div> 40 41@endsection 42
Controller
php
1public function like(Request $request){ 2 $post_id = $request->id; 3 //post_idが存在しない場合は終わり 4 $post = Article::where('id','=',$post_id)->get(); 5 if (!$post) { 6 return null; 7 } 8 //ログインしているユーザーの情報を取得します 9 $user = Auth::user(); 10 //ユーザーがライクを押しているか 11 $like = $user->likes()->where('post_id', $post_id)->first(); 12 //既に押してるときは何もしない 13 if ($like) { 14 return null; 15 } 16 //初めての押下時は新規にLikeテーブルにレコードを入れます 17 else{ 18 $like = new Like(); 19 } 20 $like->like = 1;//ここで1のみを扱います 21 $like->user_id = $user->id; 22 $like->post_id = $post->id; 23 24 $like->save(); 25 26 return null; 27 }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/10 12:23
2018/12/10 13:41
2018/12/10 15:02
2018/12/10 15:31
2018/12/11 01:40
2018/12/11 02:29