简体   繁体   English

在Laravel中从数据库中获取数据时出现问题

[英]Problem with fetching data from database in Laravel

I have search form to list properties/ads through certain criteria. 我有搜索表单,通过某些标准列出属性/广告。 I am trying to fetch all properties based on offer or demand, depends on what is clicked in form. 我试图根据要约或要求获取所有属性,具体取决于在表单中点击的内容。 I have three tables 我有三张桌子

properties (id, price, location)

properties_categories (property_id, category_id)

categories (id, category, priority) 

In row category in categories table there are two values, offer and demand. 在类别表的行类别中,有两个值,即要约和要求。 With 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 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 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 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 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 web.php

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

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

Use different route name and remove route params like below code, 使用不同的路由名称并删除路由参数,如下面的代码,

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

Also remove parameters from search action and use request() in your search function. 同时从搜索操作中删除参数,并在search功能中使用request()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM