正在检索多个选择值,而不是作为单个段塞ID

时间:2015-10-25 17:14:00

标签: php mysql laravel laravel-5.1 laravel-routing

好的,我已经浏览过该网站以及其他一些试图找到答案的人,但我很挣扎。基本上我从我的用户索引页面发送 - >用户更改主要用户然后从那里用户从选择框中选择一个房子,然后在接受时它将该ID发回,这样我就可以修改并将房屋指定为主要房屋。

代码经常在尝试不同的解决方案时进行了更改,但是当用户将表单发送回索引页面时,它会提交所有的房屋ID而不是仅选择已选择的房屋的特定ID

  

这是改变主要观点

 @extends('app')
    @section('links')
    <ul class="nav navbar-nav">
        <li><a href="/user">Back</a></li>
    </ul>
    @stop

    @section('content')

        <div class="container-fluid">
            <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="form-box">
                    <div class="panel panel-default">
                        <div class="panel-heading" style="padding-left: 30px;">Change your password</div>
                        <div class="panel-body">

                            @if (count($errors) > 0)
                                <div class="alert alert-danger">
                                    <strong>Whoops!</strong> There were some problems with your input.<br><br>
                                    <ul>
                                        @foreach ($errors->all() as $error)
                                            <li>{{ $error }}</li>
                                        @endforeach
                                    </ul>
                                </div>
                            @endif

                            <p>
                                {!! Form::model($houses,['method' => 'POST','route'=>['user.cprimary',$house_id->$houses->id]]) !!}
                                {!! Form::open(['url' => 'user']) !!}

                            <div class="form-group">
                                @if($houses != null)
                                    <div class="form-group">
                                        {!! Form::label('primary', 'Choose a house to make primary') !!}
                                        {{--{!! Form::select('primary', $houses, $houses) !!}--}}

                                            {!! Form::select('primary', $houses) !!}

                                    </div>
                                        {!! Form::close() !!}
                                    <div class="container">
                                        {!! Form::submit('Make House Primary House', ['class' => 'btn btn-primary']) !!}
                                    </div>
                                @else
                                        <li><a href={{url('/house/create')}}>Add new house</a></li>

                                    </form>
                                @endif
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
  

这是show primary和change priamry的用户控制器

    public function showcp()
    {
        $houses = House::where('user_id', '=', Auth::id())
            ->lists('h_name');

        $house_id = House::where('user_id', '=', Auth::id())
            ->lists('id');
//        foreach($house_ids as $house_id) {
//            $house_id = $house_id->id;
//        }
//        $house = House::find($house_id)->h_name->first();
        if (!$houses->isEmpty()) {
            return view('users.changeprimary', compact('houses','house_id'));
        }
        return view('house.nohouse');
    }

    public function changeprimary($id)
    {
        //Find and update password linked to User ID.

        $house_all = House::all()->where('user_id', Auth::id());
        foreach($house_all as $houses)
        {
            $houses->primary = '0';
            $houses->save();

        }
        $houses->save();
        $updateHouse = House::find($id);
        $updateHouse->primary = '1';
        $updateHouse->save();
        return redirect('user')->with('message', 'House '.House::find($id)->h_name.' is primary');
    }
  

这是索引视图的代码

@extends('app')

@section('links')
    <ul class="nav navbar-nav">
        <li><a href="/dash">Back</a></li>
    </ul>
@stop

@section('content')

    <div class="container-fluid">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="form-box">
                    <div class="panel panel-default">
                        <div class="panel-heading">Profile Options</div>
                        <div class="panel-body">

                            @if (session('message'))
                                <div class="alert alert-info" style="text-align: center; width: 200px; ">
                                    {{ session('message') }}
                                </div>
                            @endif

                            {!! Form::open() !!}

                            <div class="form-group">

                                <!-- Modify settings accordingly -->

                                <div class="form-group">
                                    <a href="{{route('user.edit') }}" class="btn btn-primary">Change User Settings and Details</a>
                                </div>
                                <div class="form-group">
                                    <a href="{{url('user/changepassword') }}" class="btn btn-primary">Change Password</a>
                                </div>
                                <div class="form-group">
                                    <a href="{{route('user.changeprimary', $houses) }}" class="btn btn-primary">Change Primary House</a>
                                </div>
                            </div>
                            {!! Form::close() !!}
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@stop
  

这是我的路线档案

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
 */

//Home Routes

Route::get('/', 'HomeController@index');
Route::get('home', 'HomeController@index');


//User Routes

Route::post('user/{id}/cprimary',['as' => 'user.cprimary', 'uses' => 'UserController@changeprimary']);
Route::get('user/changeprimary',['as' => 'user.changeprimary', 'uses' => 'UserController@showcp']);
Route::get('user/changepassword', 'UserController@changepassword');
Route::any('user/cpassword',['as' => 'user.cpassword', 'uses' => 'UserController@cpassword']);
Route::resource('user','UserController');
//House Routes

Route::get('createfirsthouse', 'CreateFirstHouseController@index');
Route::get('/house/alternate', 'HouseController@alternate_index');
Route::resource('house', 'HouseController');

//Google Routes
Route::get('googlemap', 'GoogleMapsTest@index');

//Profile Routes

Route::resource('profile', 'ProfileController');
Route::get('profile/resetpassword', 'ProfileController@resetpassword');


//Route to reverse schemas and get migrations.

//Route::get('tc', 'TController@index');


//Any Controller Routes not covered above

Route::controllers([
    'auth' => 'Auth\AuthController',
    'password' => 'Auth\PasswordController',
]);

Route::get('/dash', 'DashController@index');
Route::get('/', 'DashController@index');
Route::get('/temp', 'TempReset@index');
Route::resource('dash', 'DashController');

好的,正确的答案是更改控制器,不知道为什么我要为form :: open添加两个选项,我在laracasts上看到并认为它是标准。

    public function changeprimary($id)
    {

        // Get the current users ID
        $users= Auth::id();

        // Fetch all houses linked to user and loop through collection setting primary to 0
        $allhouses=House::where('user_id', $users)->get();
        foreach($allhouses as $all) {
            $all->primary = '0';
            $all->save();
        }

        //        Retrieve the selected input
        $houses = Input::get('primary');

//        Find house linked to ID
        $house = House::find($houses);
        $house->primary = '1';
        $house->save();
        return redirect('user')->with('message', 'House '.$house->h_name.' is primary');
}

这是我需要解决此问题的正确代码:)

0 个答案:

没有答案