To jest struktura bazy danych Wpisz opis obrazu tutaj To jest API.

Route::post('/friend', 'FriendController@index');
Route::post('/removerequest/{id}', 'FriendController@removerequest');

To jest kod kontrolera, który w metodzie żądania znajomego i metody usuwania, ale błąd w metodzie usuwania znajomego.

 public function index(Request $request) {
       
        $sender = Friend::where('sender_id', $request->sender_id)->where('receiver_id',$request->receiver_id)->first();
        if(empty($sender)){
            Friend::create(['sender_id'=>$request->sender_id,'receiver_id'=>$request->receiver_id, 'approved'=>'pending']);
            
            $response = ['message'=>'Friend Request has been sent','status'=>200];
            return response()->json($response);
        }else{
            $response = ['message'=>'Request has been sent already','status'=>200];
            return response()->json($response);
        }
        
    }
    public function removerequest($id){
       $friends = Friend::all()
                    ->where('receiver_id')
                    ->where('sender_id')
                    ->approved('accept')
                    ->delete();
            
    }

Błąd jest

BadMethodCallException: Method Illuminate\Database\Eloquent\Collection::approved does not exist. in file /home/ynvih0l26evc/public_html/vendor/laravel/framework/src/Illuminate/Support/Traits/Macroable.php on line 104
enter code here
0
Kinnu123 20 grudzień 2019, 08:42

1 odpowiedź

Najlepsza odpowiedź

Zaktualizuj Route do

Route::delete('/removerequest/{id}', 'FriendController@removerequest');

//change 
->approve('approved', 'accept')

Do

->where('approved', 'accept')

Zaktualizuj metodę kontrolera do

public function removerequest($id){
        $friends = Friend::Find($id)->delete(); //see update here
                    //->where('receiver_id')
                    //->where('sender_id')
                    //->where('approved', 'accept')
                    //->delete();

    }

LUB

public function removerequest($id){
     Friend::where( ['id' => $id, 'approved' => 'accept'])->delete();   
}
1
codervine 20 grudzień 2019, 07:42