'Problem with fetching data from database in Laravel

I have a search form to list properties/ads through certain criteria. I am trying to fetch all properties based on offer or demand, depending on what is clicked in the form. I have three tables

properties (id, price, location)

properties_categories (property_id, category_id)

categories (id, category, priority) 

In the row category in the categories table, there are two values, offer, and demand. With the current code when I select offer or demand and click submit I get [] empty array. Any help is appreciated. Here is my code:

Category.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    public $timestamps = false;

    public function property()
    {
        return $this->belongsToMany(Property::class);
    }

}

Property.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Property extends Model
{
    protected $guarded = ['id'];

    public function category()
    {
        return $this->belongsToMany(Category::class, 'properties_categories')->orderBy('priority', 'asc');
    }
}

CategoryController.php

<?php
namespace App\Http\Controllers;

use App\Category;
use App\Http\Controllers\Controller;
use App\Property;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;

class CategoryController extends Controller
{
    public function index()
    {
        return view('categories.search', compact('data'));
    }

    public function search($propertyBidAsk, $propertyType, $propertyPayment, $city, $price, $quadrature, Request $request, Property $property)
    {
        $category = $property->category;

        if (!empty($request->propertyBidAsk)) {

           return $property->category()->where('category', $request->propertyBidAsk)->get();
        }  
    
        $results = $property;

        return view('categories.search', compact('category', 'results'));
    }

}

search.blade.php

<div>

 @if(isset($results))
    <table class="table">
        <thead>
            <th>Property Bid Ask</th>
        </thead>
        <tbody>

        @foreach ($results as $result)
            <tr>
                <td>{{ $result->category[0]->category }}</td>
            </tr>
        @endforeach

      </tbody>
    </table>
 @endif

 </div>

 <form id="searchForm" method="GET" action="/search">
     <div class="col-md-4 mb-6">
     <h5>Payment</h4>
         <div class="d-block my-3">
             <div class="custom-control custom-radio">
             <input id="offer" name="propertyBidAsk" value="offer" type="radio" class="custom-control-input">
             <label class="custom-control-label" for="offer">Offer</label>
        </div>
            <div class="custom-control custom-radio">
            <input id="demand" name="propertyBidAsk" value="demand" type="radio" class="custom-control-input">
            <label class="custom-control-label" for="demand">Demand</label>
        </div>
     </div>
  </div>
 <button class="btn btn-primary btn-lg btn-block" type="submit">Search</button>
 </form>

web.php

Route::get('/search', 'CategoryController@index');

Route::get('/search/{propertyBidAsk}/{propertyPayment}/{propertyType}/
{city}/{price}/{quadrature}', 'CategoryController@search');


Solution 1:[1]

Use different route names and remove route params like the below code,

Route::get('/search-data', 'CategoryController@search'); 

Also, remove parameters from the search action and use request() in your search function.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sivan