dimanche 25 octobre 2015

Multiple select values being retrieved and not being sent as a single slug ID

Ok I've looked through the site and a few others trying to find an answer to this, but I am struggling. Essentially I am sending from my users index page -> user change primary and from there the user then chooses a house from the select box and then when accepts it sends the ID back so that I can modify and designate the house as being primary.

The code has been changed quite often as tried different solutions, however what happens is that when a user sends the form back to the index page it then submits all house ID's instead of only the specific ID of the house that has been selected.

This is the changeprimary view

 @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>

This is the user controller for show primary and 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');
    }

And this is the code for the index view

@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

And this is my routes file

<?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');



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire