在Laravel中从数据库中获取数据的问题

时间:2019-07-02 08:25:29

标签: php laravel many-to-many pivot-table

我有搜索表单,可以通过某些条件列出属性/广告。我正在尝试根据要约或需求获取所有属性,具体取决于单击的形式。我有三张桌子

properties (id, price, location)

properties_categories (property_id, category_id)

categories (id, category, priority) 

在类别表的行类别中,有两个值,要约和需求。使用当前代码,当我选择要约或需求并单击提交时,我得到[]空数组。任何帮助表示赞赏。这是我的代码:

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

1 个答案:

答案 0 :(得分:0)

使用其他路由名称并删除以下代码等路由参数,

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

还要从搜索操作中删除参数,并在request()函数中使用search