在Laravel中显示数据库结果的问题

时间:2019-07-23 12:39:21

标签: php laravel

我正在Laravel中为广告/属性构建应用程序。我有一个编辑配置文件页面,在该页面上有一个用户首选项部分,其中有多个复选框,我要单击一个或多个选项,提交表单时,我想重定向到主页,该页面将向我显示来自数据库的所有带有这些内容的广告我检查过的偏好。复选框值是来自类别表和类别列的值,类别表通过数据透视表properties_categories连接到属性表。当前使用此代码可以列出正确的值,但是当我刷新页面时它们会消失,并且希望它们始终存在并能够更新它们。任何帮助表示赞赏。这是我的代码。

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,
            'last_name' => $request->last_name,
            'email' => $request->email,
            'city' => $request->city,
            'type' => $request->type,
            'updated_at' =>  Carbon::now()
        ]
    );


    // FROM HERE IS RELEVANT CODE!!!

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


    // TO HERE IS RELEVANT CODE!!!


    $photo = ($request->image[0] === ',') ?
        ltrim($request->image, ',') : $request->image;
    DB::table('photos')->where('filename', $photo)
        ->update(
            [
                'user_id' => $user->id,
                'model_type' => 'App\User',
                'model_id' => '',
                'original_src' => "user/$user->id/gallery",
                'primary' => '1',
                'alt_attribute' => '',
                'updated_at' =>  Carbon::now()
            ]
        );

    $image_data = DB::table('photos')
        ->where('user_id', $user->id)
        ->where('alt_attribute', 'Delete')
        ->first();

    if ($image_data) {
        Storage::delete(
            "public/$image_data->original_src/$image_data->filename"
        );
        DB::table('photos')
            ->where('filename', $image_data->filename)
            ->delete();
    }

    Storage::exists("public/tmp/$photo") ?
        Storage::move(
            "public/tmp/$photo",
            "public/user/$user->id/gallery/$photo"
        ) : '';

    return view('startpage', compact('resultsFiltered', 'category', 'request', 'user.photo', 'photos', 'propertyType'));
}

edit.blade.php

<form id="form" method="POST" action="{{ route('user.update',['id'=>$user->id]) }}">

{{-- PREFERENCES !!!!! --}}

<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 class="col-md-1" style="margin-right:15px;">
        <div class="custom-control custom-checkbox">
            <input id="shop" name="propertyType1[]" value="shop" type="checkbox" class="custom-control-input">
            <label class="custom-control-label" for="shop">shop</label>
        </div>
    </div>
    <div class="col-md-1" style="margin-right:15px;">
        <div class="custom-control custom-checkbox">
            <input id="lot" name="propertyType1[]" value="lot" type="checkbox" class="custom-control-input">
            <label class="custom-control-label" for="lot">lot</label>
        </div>
    </div>
    <div class="col-md-1" style="margin-right:15px;">
        <div class="custom-control custom-checkbox">
            <input id="garage" name="propertyType1[]" value="garage" type="checkbox" class="custom-control-input">
            <label class="custom-control-label" for="garage">garage</label>
        </div>
    </div>           
</div>

{{-- END PREFERENCES !!!!! --}}

<button form="form" type="submit" class="btn btn-primary">
    Save changes
/button>

</form>

home.blade.php

<div class="col-md-3 justify-content-md-center">
<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
    @endif
</div>

web.php

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

Property.php

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

Category.php

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

0 个答案:

没有答案