dimanche 19 juin 2016

How to insert value to a column in laravel?

I have laravel 'make:auth()' up and running and I did little modification in migration i.e added a Boolean field called 'admin' and by default I have made that field 0 . Now I have created a button in view 'Make Admin' , I want this button to update the admin field from zero to 1.

I tired the below but it is not updating the DB column value.

Controller:

public function makeAdmin($id)
    {
        $user = User::find($id);
            if($user){
                $user->admin = '1';
                $user->save();
                return Redirect::to('/users');
            }
}

View

@extends('layouts.app')

@section('content')
<script src="http://ift.tt/1MVunhH"></script>
<div class="container">
    <div class="row">
        <div class="col-md-10 col-md-offset-1">
            <div class="panel panel-default">
                <div class="panel-heading">All Registered User(s)</div>

                <div class="panel-body" id="toPrint">
                   <table class="table table-striped">
                            <thead>
                              <tr>
                                <th>ID</th>
                                <th>Name</th>
                                <th>Email</th>
                                <th>Password</th>
                                <th>Role</th>
                                <th>Edit</th>
                                <th>Delete</th>
                              </tr>
                            </thead>
                            <tbody>
                                @foreach($users as $user)
                                  <tr>
                                    <td>{!! $user->id !!}</td>
                                    <td>{!! $user->name !!}</td>
                                    <td>{!! $user->email !!}</td>
                                    <td>{!! $user->password !!}</td>
                                    <td>
                                        {!! Form::open(array('action'=>'Users@makeAdmin', 'url' => "/users" , 'method' => 'POST')) !!}
                                            {!! Form::hidden('id', $user->id) !!}
                                                <button type="submit" class="btn btn-default">Make Admin</button>
                                        {!! Form::close() !!}
                                    </td>
                                    <td>
                                        {!! Form::open(array('url' => "/users/$user->id/edit" , 'method' => 'GET')) !!}
                                            {!! Form::hidden('id', $user->id) !!}
                                                <button type="submit" class="btn btn-default">Edit</button>
                                        {!! Form::close() !!}
                                    </td>
                                    <td>
                                        {!! Form::open(array('url' => '/users/destroy' ,  'method' => 'delete')) !!}
                                            {!! Form::hidden('id', $user->id) !!}
                                                <button type="submit" class="btn btn-default">Delete</button>
                                        {!! Form::close() !!}
                                    </td>

                                  </tr>
                                 @endforeach
                            </tbody>
                    </table>

                </div>
            </div>
        </div>
    </div>
</div>

@endsection



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire