在Laravel中显示数据库中的数据时出现问题

时间:2019-07-18 13:06:14

标签: php laravel has-many

我正在Laravel中为广告/媒体资源构建应用程序。我有一个注册表单,我想在其中选择一种或多种类型的广告/属性,当我提交注册表单时,我想重定向到主页,该页面将列出所有我在表单中检查过的过滤器的广告/属性

users (id, first_name, last_name, email, city, etc...)

categories (id, category)

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

category_property (id, category_id, property_id)

在类别表中,列类别包含以表格形式使用的房屋,公寓,公寓,房间等值。我在将要选择和显示这些值的控制器中编写代码时遇到麻烦。任何帮助表示赞赏。这是我的表格和代码。

RegisterController.php

<?php
namespace App\Http\Controllers\Auth;

use App\User;
use App\Photo;
use \Carbon\Carbon;
use Illuminate\Validation\Rule;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;

class RegisterController extends Controller
{
    use RegistersUsers;

    protected $redirectTo = '/';

    public function __construct()
    {
        $this->middleware('guest');
    }

    protected function validator(array $data)
    {
        return Validator::make($data, [
            'first_name' => ['required', 'string', 'max:255'],
            'last_name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:users'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
            'city' => ['required', 'exists:cities,city'],
            'type' => ['required', Rule::in(['personal','agency'])],
            'image' => ['required']
        ]);
    }

    protected function create(array $data, User $user)
    {
        $category = DB::table('categories')
            ->whereIn(
                'category',
                [
                    $data->propertyType
                ]
            )
            ->pluck('id')
            ->toArray();

        $userId = $user->insertGetId([
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'email' => $data['email'],
            'password' => Hash::make($data['password']),
            'city' => $data['city'],
            'type' => $data['type']
        ]);

        $user->find($userId)->categories()->attach($category);

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

        $newuser = User::find($user->id);
        $newuser->attachRole('4');
        Storage::move(
            "public/tmp/$photo",
            "public/user/$user->id/gallery/$photo"
        );

        return $user;
    }
}

register.blade.php

@extends('layouts.app')

@section('content')

<div class="container">
<div class="row justify-content-center">
    <div class="col-md-8">
        <div class="card">
            <div class="card-header">{{ __('Register') }}</div>
            <a href="{{route('social.redirect',['provider'=> 'facebook' ])}}" class="btn btn-labeled btn-primary">
                <span class="btn-label"><i class="fa fa-facebook-f"></i></span>
                    Register with Facebook
            </a>
            <br />
            <a href="{{route('social.redirect',['provider'=> 'google' ])}}" class="btn btn-labeled btn-light">
                <span class="btn-label"><i class="fa fa-google"></i></span>
                Register with Google</a>
            <br />
            <div class="card-body">
                <form id="form"method="POST" action="{{ route('register') }}">
                   @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') }}" required autocomplete="first_name" autofocus>

                            @error('first_name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="last_name" class="col-md-4 col-form-label text-md-right">{{ __('Last Name') }}</label>

                        <div class="col-md-6">
                            <input id="name" type="text" class="form-control @error('last_name') is-invalid @enderror" name="last_name" value="{{ old('last_name') }}" required autocomplete="last_name" autofocus>

                            @error('last_name')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('E-Mail Address') }}</label>
                        <div class="col-md-6">
                            <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email">

                            @error('email')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="password" class="col-md-4 col-form-label text-md-right">{{ __('Password') }}</label>
                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control @error('password') is-invalid @enderror" name="password" required autocomplete="new-password">

                            @error('password')
                                <span class="invalid-feedback" role="alert">
                                    <strong>{{ $message }}</strong>
                                </span>
                            @enderror
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="password-confirm" class="col-md-4 col-form-label text-md-right">{{ __('Confirm Password') }}</label>
                        <div class="col-md-6">
                            <input id="password-confirm" type="password" class="form-control" name="password_confirmation" required autocomplete="new-password">
                        </div>
                    </div>
                    <div class="form-group row">
                        <label for="city" class="col-md-4 col-form-label text-md-right">{{ __('Select City') }}</label>

                        <input name="city" list="result" id="input" value="" class="form-control  @error('city') is-invalid @enderror col-sm-6 custom-select custom-select-sm"required autocomplete="city">
                        <datalist id="result">
                        </datalist>

                        @error('city')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror

                    </div>

                    <div class="form-group row">
                        <label for="type" class="col-md-4 col-form-label text-md-right">{{ __('Select Account Type') }}</label>
                        <select class="form-control @error('type') is-invalid @enderror col-sm-6 custom-select custom-select-sm" required name="type" >
                            <option value='personal' {{ old('type') == 'personal'?'selected':'' }}>Personal</option>
                            <option value='agency'  {{ old('type') == 'agency'?'selected':'' }}>Agency</option>
                        </select>

                        @error('type')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror

                    </div>




                    {{-- 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="propertyType" 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="propertyType" 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="propertyType" 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="propertyType" 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="propertyType" 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="propertyType" 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="propertyType" value="garage" type="checkbox" class="custom-control-input">
                                <label class="custom-control-label" for="garage">garage</label>
                            </div>
                        </div>           
                    </div>

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




                    <hr class="mb-4">

                    <div id="img" class="row">
                        <input type="hidden" id="hideninput" data-src="tmp"value="{{old('image')}}" name="image" class="image">
                            @if (old('image'))
                            @foreach (array_filter(explode(',',old('image')), 'strlen') as $key =>$value)
                        <div id="{{'div_image_'.$key}}"class="col-md-3 img-wrap" >
                            <button data="{{$key}}" type="button" class="closebutton">
                                <span aria-hidden="true">&times;</span>
                            </button>
                            <img id="{{'image_'.$key}}" data={{$value}} src="/storage/tmp/{{$value}}" class="img-fluid">
                        </div>
                            @endforeach
                            @endif
                    </div>
                    @error('image')
                    <div class="alert alert-danger" role="alert">
                        {{ 'At least one image is required' }}
                    </div>
                    @enderror
                </form>

                <form action="/PhotoRequest" method="POST"class="dropzone"  data-count="{{ old('image') ? 1-count(array_filter(explode(',',old('image')), 'strlen')) : 1 }}" id="dropzone">
                    {{csrf_field()}}
                    Drop files here to Upload <br>
                    Size limit 2MB
                </form>

                <hr class="mb-4">

                <div class="form-group row mb-0">
                    <div class="col-md-6 offset-md-4">
                        <button form="form" type="submit" class="btn btn-primary">
                            {{ __('Register') }}
                        </button>
                     </div>
                 </div>
             </div>
         </div>
     </div>
 </div>
</div>

@endsection

User.php

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

public function properties()
{
    return $this->hasMany(Property::class, 'user_id');
}

Category.php

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

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

Property.php

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

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

0 个答案:

没有答案