- add schema and endpoints to make follows relationship with customer and broker - show and update states of follow button on ui
44 lines
984 B
PHP
44 lines
984 B
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Models\Broker;
|
|
use App\Models\Follow;
|
|
|
|
class FollowController extends Controller
|
|
{
|
|
public function __invoke(Broker $broker)
|
|
{
|
|
$follow = $this->checkFollow($broker);
|
|
if ($follow === null) {
|
|
return $this->store($broker);
|
|
} else {
|
|
return $this->destroy($follow);
|
|
}
|
|
}
|
|
|
|
public function store(Broker $broker)
|
|
{
|
|
Follow::create([
|
|
'broker_id' => $broker->id,
|
|
'customer_id' => auth()->id(),
|
|
]);
|
|
|
|
return response()->json(['message' => 'Followed successfully.']);
|
|
}
|
|
|
|
public function destroy(Follow $follow)
|
|
{
|
|
$follow->delete();
|
|
|
|
return response()->json(['message' => 'Unfollowed successfully.']);
|
|
}
|
|
|
|
protected function checkFollow(Broker $broker)
|
|
{
|
|
return Follow::where('broker_id', $broker->id)
|
|
->where('customer_id', auth()->id())
|
|
->first();
|
|
}
|
|
}
|