在Laravel中从数据库中获取多对多关系的数据的问题

时间:2019-07-01 12:56:07

标签: php laravel many-to-many

我有搜索表单,可以通过某些条件列出属性/广告。我有一个带有两个选项(要约,需求)的单选按钮。我想当我单击任一列出所有连接到该选项的属性时。我与数据透视表有很多关系。我无法在控制器中编写函数,该函数将连接表并在视图中显示它们。我有三张桌子。

properties (id, city, price)

categories (id, category, priority)

category_property (property_id, category_id)

在类别行的类别表中,有“要约”和“需求”,并且在优先行中都具有0值。当我单击“提交”按钮时,表格出现但没有结果。当我dd($ property);在控制器中,我得到了

BelongsToMany {#282 ▼
#table: "category_property"
#foreignPivotKey: "property_id"
#relatedPivotKey: "category_id"
#parentKey: "id"
#relatedKey: "id"
#relationName: "category"
#pivotColumns: []
#pivotWheres: []
#pivotWhereIns: []
#pivotValues: []
+withTimestamps: false
#pivotCreatedAt: null
#pivotUpdatedAt: null
#using: null
#accessor: "pivot"
#query: Builder {#270 ▶}
#parent: Property {#287 ▶}
#related: Category {#271 ▶}
-currentlyAttached: null
}

当我dd($ results);我得到

Collection {#281 ▼
#items: []
}

任何帮助将不胜感激。这是我的代码:

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)) {
           $property = $property->category()->where('category', $request->propertyBidAsk);
        }

        dd($property);

        $results =  $property->get();

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

0 个答案:

没有答案