vendredi 18 août 2017

Uncaught (in promise) 500 (Internal Server Error) BadMethodCallException

I use vue 2.0 and Laravel 5.4

I trying to build a matching system

that has a dynamic vue component in it (i.e when someone likes another person it immediately tells you that you have liked that person and/or when accepting it immediately tells you that you're a match)

but the component keeps loading, and in my dev-tools it tells me it has an internal server error(500) (+ Uncaught (in promise))

and in my network it shows a BadMethodCallException--> Call to undefined method Illuminate\Database\Query\Builder::matches_with()

i'v included the component in my app.js file (resources)-->> Vue.component('match', require('./components/Match.vue'));

my Vue file

<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>

my Controller

<?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'));
   }






}

my Trait

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;
        }
    }



 }

my Model

namespace App;

use Illuminate\Database\Eloquent\Model;

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



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2wgxHG2
via IFTTT

Aucun commentaire:

Enregistrer un commentaire