phplaravellaravel-5

Create filter in laravel API controller


I want to create a filter to show mosque with event or activities only. Any idea to display the mosque with activities or events only? This one from back-end that later will be fetch using react:

<?php

namespace App\Http\Controllers;

use App\Event;
use App\Mosque;
use App\Activity;
use Illuminate\Http\Request;

class NotificationController extends Controller
{
    public function list()
    {
        $mosques = Mosque::get();

        $array = array();

        foreach ($mosques as $mosque) {
            array_push($array, [
                'mosque_name' => $mosque->name,
                'mosque_image'=> $mosque->image
            ]);
        }

        return $array;

        return response()->json(['result' => $mosques]);
    }
    
    public function show(Request $request)
    {
        $mosque =  Mosque::find($request->mosque_id);
        $mosque->activities;
        $mosque->events;

        return response()->json(['result' => $mosque]);
    }
}
                      

Solution

  • You can try this

      public function show(Request $request)
          {
             $mosque =  Mosque::find($request->mosque_id);
              $mosque->activities;
               $mosque->events;
             return response()->json(['result' => $mosque->events ]);
          }