gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:49:00 25 4
gpt4 key购买 nike

我有一个搜索表单,可以通过特定条件列出属性/广告。我正在尝试根据报价或需求获取所有属性,具体取决于在表单中单击的内容。我有三张 table

properties (id, price, location)

properties_categories (property_id, category_id)

categories (id, category, priority)

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

分类.php

<?php
namespace App;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
public $timestamps = false;

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

}

属性.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');
}
}

分类 Controller .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'));
}

}

搜索.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>

网络.php

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

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

最佳答案

使用不同的路由名称并删除路由参数,如下面的代码,

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

此外,从搜索操作中删除参数并在您的search 函数中使用request()

关于php - 在 Laravel 中从数据库中获取数据的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56848258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com