繁体   English   中英

从复选框传递值并将它们显示在 Laravel 的另一个页面中的问题

[英]Problem with passing values from checkboxes and displaying them in another page in Laravel

我正在为 Laravel 中的广告/属性构建应用程序。 我有个人资料页面,其中包含可以更新的用户信息。 在那里,我有用户首选项部分,我可以在其中选中一个或多个复选框(房屋、公寓),并根据我在提交表单后检查的内容,我想在起始页上显示具有这些值的属性/广告。 目前,当我提交表单时,我收到成功消息,但起始页上没有显示任何结果。 任何帮助是极大的赞赏。 这是我的表格和代码。

users (id, first_name)

properties (id, user_id, title, location, price, etc...)

properties_categories (id, property_id, category_id)

categories (id, category)

在类别表中的类别列中,价值房子、公寓、公寓、房间。

用户控制器.php

public function update(StoreUserInfo $request, User $user)
{
    if ( !($user->id == Auth::user()->id)) {
        abort(404);
    }

    $request->validated();

    $user->where('id', $user->id)->update(
        [
            'first_name' => $request->first_name,
        ]
    );

    $query = Property::query();

    if ($request->has('propertyType1')) {
        $request->get('propertyType1');
    }

    $propertyType1 = $request->input('propertyType1');

    if (!empty($propertyType1)) {
        $query->whereHas('category', function ($query) use ($propertyType1) {
            $query->whereIn('category', $propertyType1);
        });
    }

    $resultsFiltered = $query->where('active', 'Q')
    ->orWhere('active', 'A')
    ->orderBy('page_views', 'desc')
    ->with('user.photo', 'photos', 'category')->paginate(5);

    return redirect()->back()->with('message', 'User information updated')->with(compact('resultsFiltered'));
}

编辑刀片.php

<form id="form" method="POST" action="{{ route('user.update',['id'=>$user->id]) }}">
    @method('PUT')
    @csrf
    <div class="form-group row">
        <label for="first_name" class="col-md-4 col-form-label text-md-right">{{ __('First Name') }}</label>
        <div class="col-md-6">
            <input id="name" type="text" class="form-control @error('first_name') is-invalid @enderror" name="first_name" value="{{ old('first_name',$user->first_name) }}" required autocomplete="first_name" autofocus>
            @error('first_name')
                <span class="invalid-feedback" role="alert">
                    <strong>{{ $message }}</strong>
                </span>
            @enderror
        </div>
    </div>
    <div class="row page-hero d-flex align-items-center justify-content-center">
        <label for="preferences" class="text-center">Select your preferences</label>         
    </div>
    <div class="row">                
        <div class="col-md-1" style="margin-right:15px; margin-left:60px;">
            <div class="custom-control custom-checkbox">
                <input id="house" name="propertyType1[]" value="house" type="checkbox" class="custom-control-input">
                <label class="custom-control-label" for="house">house</label>
            </div>
        </div>
        <div class="col-md-1" style="margin-right:15px;">
            <div class="custom-control custom-checkbox">
                <input id="flat" name="propertyType1[]" value="flat" type="checkbox" class="custom-control-input">
                <label class="custom-control-label" for="flat">flat</label>
            </div>
        </div>
        <div class="col-md-1" style="margin-right:50px;">
            <div class="custom-control custom-checkbox">
                <input id="apartment" name="propertyType1[]" value="apartment" type="checkbox" class="custom-control-input">
                <label class="custom-control-label" for="apartment">apartment</label>
            </div>
        </div>
        <div class="col-md-1" style="margin-right:15px;">
            <div class="custom-control custom-checkbox">
                <input id="room" name="propertyType1[]" value="room" type="checkbox" class="custom-control-input">
                <label class="custom-control-label" for="room">room</label>
            </div>
        </div>        
    </div>
    <div class="col-md-6 offset-md-4">
        <button form="form" type="submit" class="btn btn-primary">
            Save changes
        </button>
    </div>
</form>

起始页.blade.php

<div class="col-2">
    <h1>Prefered ads</h1>
        @if (isset($resultsFiltered))
            @foreach ($resultsFiltered as $resultFiltered)
                <div class="row">
                    <a href="{{route('property.show',['id'=>$resultFiltered->id])}}">
                        @if ($resultFiltered->active == 'A')
                            <button class="btn btn-success" disabled="dissabled">Verified</button>
                        @endif
                        <img  class="img-fluid" src="/storage/{{$resultFiltered->main_photo['original_src']}}/{{$resultFiltered->main_photo['filename']}}" alt="Card image cap">
                        <button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px;">{{$resultFiltered->price}} eur</button>
                        <button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px;">{{$resultFiltered->quadrature}} m<sup>2</sup></button>
                        <button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px; margin-bottom:10px;">{{$resultFiltered->city}}</button>
                        <hr>
                    </a>
                </div>
            @endforeach
            <div class="row">
                <div class="col"></div>
                <div class="col">{{ $resultsFiltered->links() }}</div>
                <div class="col"></div>
            </div>
        @endif
</div>

网页.php

Route::put('/user/{user}', 'UserController@update')->name('user.update');

属性.php

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

public function user()
{
    return $this->belongsTo(User::class);
}

分类.php

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

public function users()
{
    return $this->belongsToMany(User::class);
}

用户名

public function categories()
{
    return $this->hasMany(Category::class);
}

public function property()
{
    return $this->hasMany('App\Property', 'user_id', 'id')->where('active', 'Q')->orWhere('active', 'A');
}

我认为你的问题就在这里,因为$query在属性模型上而不是在类别模型上,所以你需要在 whereIn 中定义表和列。

if (!empty($propertyType1)) {
    $query->whereHas('category', function ($query) use ($propertyType1) {
        $query->whereIn('category', $propertyType1);
    });
}

你需要这样做

if (!empty($propertyType1)) {
    $query->whereHas('category', function ($query) use ($propertyType1) {
        $query->whereIn('properties_categories.category', $propertyType1);
    });
}

编辑:尽管在您的模型中设置了关系,您仍然需要在语句中提供表,因为您的查询在属性模型中而不是在类别中。

$query = Property::query();

就目前而言,您正在寻找不存在的property.category 你需要的是properties_categories.category

暂无
暂无

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

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