未捕获(在承诺中)500(内部服务器错误)BadMethodCallException

时间:2017-08-18 11:54:46

标签: javascript php laravel-5 vue.js vue-component

我使用vue 2.0和Laravel 5.4

我试图建立一个匹配系统

其中有一个动态vue组件(即当有人喜欢另一个人时,它立即告诉你,你喜欢那个人和/或当接受它时立即告诉你你是匹配的)

但组件继续加载,在我的开发工具中,它告诉我它有一个内部服务器错误(500)(+未捕获(承诺))

在我的网络中显示了一个      的 BadMethodCallException - >      调用未定义的方法Illuminate \ Database \ Query \ Builder :: matches_with()

我在app.js文件中包含了该组件(资源) - >> Vue.component('match',require('./ components / Match.vue'));

我的Vue文件

<template>
    <div>
        <p class="text-center" v-if="loading">
             Loading...
        </p>
       <p class="text-center" v-if="!loading">

             <a v-if="status == 0" @click="like_user">
              <span title="I like you" class="send-heart"></span>
            </a>
    <a href=""><span title="I like you to" class="pending" v-if="status == 
  'pending'" @click="mutual_like">accept</span></a>


        <a href="">
            <span title="You like this person" class="pending" v-
       if="status == 'waiting'"></span>
        </a>


            <span v-if="status == 'match'" title="Chat" class="chat"></span>
        </a>

    </p>
</div>
</template>

 <script>
export default {
mounted() {
    this.$http.get('/check_match_status/' + this.profile_user_id)
        .then((resp) => {
            console.log(resp)
            this.status = resp.body.status
            this.loading = false
        })
  },
  props: ['profile_user_id'],
  data() {
    return {
        status: '',
        loading: true
    }
},
methods: {
    like_user() {
        this.loading = true
        this.$http.get('/like_user/' + this.profile_user_id)
            .then((r) => {
                if (r.body == 1)
                    this.status = 'waiting'

                this.loading = false
            })
     },
    mutual_like() {
        this.loading = true
        this.$http.get('/mutual_like/' + this.profile_user_id)
            .then((r) => {
                if (r.body == 1)
                    this.status = 'match'

                this.loading = false
            })
        }
      }
   }
 </script>

我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Auth;
use App\Profile;
use Illuminate\Support\Facades\Event;
use App\User;
use App\Matches;
use App\Traits\Matchable;
use DB;


class MatchesController extends Controller
 {

 use Matchable;


 public function check($id){

                if (Auth::user()->matches_with($id) === 1)


                {
                    return [ "status" => "match" ];
                }


                if(Auth::user()->has_pending_like_request_from($id))

                    {
                return [ "status" => "pending" ];

                        }


                        if(Auth::user()->has_pending_like_request_sent_to($id))

                    {
                return [ "status" => "waiting" ];

                        }
       return [ "status" => 0 ];
}





  public function like_user($id)
{

    //notify users
    return Auth::user()->like_user($id);

}
public function mutual_like($id)
{
    //sending nots
   return Auth::user()->mutual_like($id);


}

public function matches() {
    $uid = Auth::user()->id;
    $match1 = DB::table('matches')
            ->leftJoin('users', 'users.id', 'matches.recipient_id') // who is not loggedin but send request to
            ->where('status', 1)
            ->where('sender_id', $uid) // who is loggedin
            ->get();
    //dd($friends1);
    $match2 = DB::table('matches')
            ->leftJoin('users', 'users.id', 'matches.sender_id')
            ->where('status', 1)
            ->where('recipient_id', $uid)
            ->get();
    $matches = array_merge($match1->toArray(), $match2->toArray());
    return view('matches', compact('matches'));
   }






}

我的特质     

use App\Matches;

trait Matchable {

public function like_user($recipient_id)
{   
    if($this->id === $recipient_id)
    {
        return 0;
    }
    if($this->matches_with($recipient_id) === 1)
    {
        return "You are already a match!";
    }
    if($this->has_pending_like_request_sent_to($recipient_id) === 1)
    {
        return "You already liked this person.";
    }
    if($this->has_pending_like_request_from($recipient_id) === 1)
    {
        return $this->like_user($recipient_id);
    }
    $match = Matches::create([
        'sender_id' => $this->id,
        'recipient_id' => $recipient_id
    ]);
    if($match)
    {
        return 1;
    }
    return 0;          
} 
public function mutual_like($sender_id)
{
    if($this->has_pending_like_request_from($sender_id) === 0)
    {
        return 0;
    }
    $match = Matches::where('sender_id', $sender_id)
                    ->where('recipient_id', $this->id)
                    ->first();
    if($match)
    {
        $match->update([
            'status' => 1
        ]);
        return 1;
    }
    return 0;
} 

public function matches()
{   
    $matches = array();

    $m1 = Matches::where('status', 1)
                ->where('sender_id', $this->id)
                ->get();
    foreach($f1 as $match):
        array_push($matches, \App\User::find($match->recipient_id));
    endforeach;
    $matches2 = array();

    $m2 = Matches::where('status', 1)
                ->where('recipient_id', $this->id)
                ->get();
    foreach($m2 as $match):
        array_push($match2, \App\User::find($match->sender_id));
    endforeach;
    return array_merge($matches, $match2);

}

public function pending_like_requests()
{
    $users = array();

    $matches = Matches::where('status', 0)
                ->where('recipient_id', $this->id)
                ->get();
    foreach($matches as $match):
        array_push($users, \App\User::find($match->sender_id));
    endforeach;

    return $users;
}

    public function matches_ids()
{
    return collect($this->matches())->pluck('id')->toArray();
}

public function matches_with($user_id)
{
    if(in_array($user_id, $this->matches_ids()))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}

public function pending_like_requests_ids()
{
    return collect($this->pending_like_requests())->pluck('id')->toArray();
}

    public function pending_like_requests_sent()
{
    $users = array();
    $matches = Matches::where('status', 0)
                    ->where('sender_id', $this->id)
                    ->get();
    foreach($matches as $match):
        array_push($users, \App\User::find($match->recipient_id));
    endforeach;
    return $users;
}


public function pending_like_requests_sent_ids()
{
    return collect($this->pending_like_requests_sent())->pluck('id')-
>toArray();
}

 public function has_pending_like_request_from($user_id)
{
    if(in_array($user_id, $this->pending_like_requests_ids()))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}
public function has_pending_like_request_sent_to($user_id)
{
    if(in_array($user_id, $this->pending_like_requests_sent_ids()))
    {
        return 1;
    }
    else
    {
        return 0;
        }
    }



 }

我的模特

namespace App;

use Illuminate\Database\Eloquent\Model;

class Matches extends Model
{
  protected $fillable = ['sender_id', 'recipient_id', 'status',];
}

1 个答案:

答案 0 :(得分:0)

Matchable特征移至您的User模型而不是您的MatchesController控制器

<?php

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Matches\Matchable;

class User extends Authenticatable
{
    use Matchable;