Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从复选框传递值并在Laravel的另一页中显示值时出现问题_Php_Laravel - Fatal编程技术网

Php 从复选框传递值并在Laravel的另一页中显示值时出现问题

Php 从复选框传递值并在Laravel的另一页中显示值时出现问题,php,laravel,Php,Laravel,我正在为Laravel中的广告/属性构建一个应用程序。我有一个个人资料页面,其中包含可以更新的用户信息。在这里,我有一个用户偏好部分,我可以选中一个或多个复选框(房子、公寓),根据我在提交表单后选中的内容,我希望在首页显示具有这些值的属性/广告。目前,当我提交表单时,我会收到一条成功消息,但startpage上不会显示任何结果。非常感谢您的帮助。这是我的表格和代码 users (id, first_name) properties (id, user_id, title, location,

我正在为Laravel中的广告/属性构建一个应用程序。我有一个个人资料页面,其中包含可以更新的用户信息。在这里,我有一个用户偏好部分,我可以选中一个或多个复选框(房子、公寓),根据我在提交表单后选中的内容,我希望在首页显示具有这些值的属性/广告。目前,当我提交表单时,我会收到一条成功消息,但startpage上不会显示任何结果。非常感谢您的帮助。这是我的表格和代码

users (id, first_name)

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

properties_categories (id, property_id, category_id)

categories (id, category)
在“类别”表的“类别”列中,是“房屋”、“公寓”、“房间”的值

UserController.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'));
}
edit.blade.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>
<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>
Property.php

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

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

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

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

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
在属性模型上,而不是类别模型上,所以您需要在其中定义表和列

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

目前,您正在查找不存在的
属性.category
。您需要的是
属性\u类别。类别

是首选项类别吗@party ring是的。您能验证您的关系是否正常吗?您能否获得一个随机类别(即,
$category=category::first()
)并转储该类别下的属性
dd($category->properties())$query = Property::query();