Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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 显示来自数据库列的json结果时出现问题?_Php_Laravel - Fatal编程技术网

Php 显示来自数据库列的json结果时出现问题?

Php 显示来自数据库列的json结果时出现问题?,php,laravel,Php,Laravel,我有一个在拉威尔的广告/物业项目。我有很多用户和属性、用户和类别之间的关系,也有很多属性和类别之间的关系。这是我的桌子 users (id, first_name, user_preferences) properties (id, user_id, location, price) categories (id, category) category_property (id, category_id, property_id) 作为登录用户,当我选中编辑配置文件页面中的一个或多个复选

我有一个在拉威尔的广告/物业项目。我有很多用户和属性、用户和类别之间的关系,也有很多属性和类别之间的关系。这是我的桌子

users (id, first_name, user_preferences)

properties (id, user_id, location, price)

categories (id, category)

category_property (id, category_id, property_id)
作为登录用户,当我选中编辑配置文件页面中的一个或多个复选框时,我会在用户表的用户首选项列中插入json值,例如,[“house”,“flat”]。我希望登录的用户在startpage上显示其在表单中签入的值的所有属性。使用当前代码,我得到了这个错误

未找到列:1054“where子句”中的未知列“user\u preferences”(SQL:选择count(*)作为从
properties
where
active
=Q或
active
=A和
user\u preferences
=garage中的聚合)

我需要帮助在控制器中编写正确的查询

CategoryController.php

public function index(Category $category, Property $property, User $user)
{
    $preferedAdsResults = $property->where('active', 'Q')
    ->orWhere('active', 'A')
    ->orderBy('page_views', 'desc')
    ->with('category', 'user')
    ->where('user_preferences', 'garage')
    ->paginate(5, ['*'], 'preferedAdsResults');

    return view('startpage', compact('preferedAdsResults'));
}
edit.blade.php

<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="user_preferences[]" value="house" type="checkbox" class="custom-control-input" @if(is_array(old('user_preferences', $user->user_preferences)) && in_array('house', old('user_preferences', $user->user_preferences))) checked @endif>
                <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="user_preferences[]" value="flat" type="checkbox" class="custom-control-input" @if(is_array(old('user_preferences', $user->user_preferences)) && in_array('flat', old('user_preferences', $user->user_preferences))) checked @endif>
                <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="room" name="user_preferences[]" value="room" type="checkbox" class="custom-control-input" @if(is_array(old('user_preferences', $user->user_preferences)) && in_array('room', old('user_preferences', $user->user_preferences))) checked @endif>
                <label class="custom-control-label" for="room">room</label>
            </div>
        </div>          
</div>
<div class="col-2">
    <h1>Prefered ads</h1>

        @if (isset($preferedAdsResults))
            @foreach ($preferedAdsResults as $preferedAdsResult)
                <div class="row">
                    <a href="{{route('property.show',['id'=>$preferedAdsResult->id])}}">
                        @if ($preferedAdsResult->active == 'A')
                        <button class="btn btn-success" disabled="dissabled">Verified</button>
                        @endif

                        <img  class="img-fluid" src="/storage/{{$preferedAdsResult->main_photo['original_src']}}/{{$preferedAdsResult->main_photo['filename']}}" alt="Card image cap">
                        <button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px;">{{$preferedAdsResult->price}} eur</button>
                        <button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px;">{{$preferedAdsResult->quadrature}} m<sup>2</sup></button>
                        <button type="button" class="btn btn-lg btn-primary" disabled style="margin-top:10px; margin-bottom:10px;">{{$preferedAdsResult->city}}</button>
                        <hr>
                    </a>
                </div>
            @endforeach
            <div class="row">
                    <div class="col"></div>
                    <div class="col">{{ $preferedAdsResults->links() }}</div>
                    <div class="col"></div>
                </div>
        @endif
</div>
Property.php

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

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

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

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

如果你想雄辩地加入,请使用whereHas

public function index(Category $category, Property $property, User $user)
{
    $preferedAdsResults = $property->where('active', 'Q')
    ->orWhere('active', 'A')
    ->whereHas('user_preferences', function($q)use($variable){
    $q->where('column',$variable)})->paginate(5);

    return view('startpage', compact('preferedAdsResults'));
}
你去哪儿了


如果你有动态的where-inside-where,你需要使用我的例子中的use方法来传递它,那么你要求我们帮助你解决的问题是什么。这听起来更像是一个要求,而不是一个编码问题。要显示所有具有这些选中值的属性,我将编辑post,我得到错误。您是否尝试使用
其中的
@RomanBobrik我在一点上做了,但没有解决它的简单
user\u preferences
列在
properties
表中不存在!!!
public function index(Category $category, Property $property, User $user)
{
    $preferedAdsResults = $property->where('active', 'Q')
    ->orWhere('active', 'A')
    ->whereHas('user_preferences', function($q)use($variable){
    $q->where('column',$variable)})->paginate(5);

    return view('startpage', compact('preferedAdsResults'));
}