Pass $id from blade to controller method show Laravel 4

10,718

You could just use a link and let Laravel handle it to the propper action via your routes.

See the example below:

// Management
Route::get('management', 'ManagementController@showUser');
Route::get('management/add', 'ManagementController@showAdd');
Route::post('management/add', 'ManagementController@postAdd');
Route::get('management/edit/{id}', 'ManagementController@showEdit');
Route::post('management/edit/{id}', 'ManagementController@postEdit');
Route::get('management/delete/{id}', 'ManagementController@showDelete');
Route::post('management/delete/{id}', 'ManagementController@postDelete');

You can then just make links in your tables and style them via css as buttons.

@foreach($ManagementAll as $Management)
<tr>
    <td>{{$Management->username}}</td>
    <td>{{$Management->firstname}}</td>
    <td>{{$Management->lastname}}</td>
    <td>{{$Management->email}}</td>
    <td>{{$Management->created_at}}</td>
    <td>{{$Management->updated_at}}</td>
    <td style="padding-top: 3px; padding-bottom: 0px;">
        <a class="btn btn-default btn-circle" href="{{ URL::to('management/edit/' . $Management->id) }}"><i class="fa fa-pencil"></i></a>
        <a class="btn btn-default btn-circle" href="{{ URL::to('management/delete/' . $Management->id) }}"><i class="fa fa-times"></i></a>
    </td>
</tr>
@endforeach

Controller Show:

public function showEdit($ID)
{
    //Return View With Management User Information
    return View::make('management.edit')
        ->with('User', Management::find($ID));
}

Controller Post:

public function postEdit($ID)
{
    //Handle Input
    //Validation?
    //Update Record
    //Redirect Back
}

See this website for more information about this topic: https://scotch.io/tutorials/simple-laravel-crud-with-resource-controllers

Update

My view folder looks like this:

views
  management
    overview.blade.php
    add.blade.php
    edit.blade.php
    delete.blade.php
Share:
10,718
Wahidul Alam
Author by

Wahidul Alam

Updated on July 14, 2022

Comments

  • Wahidul Alam
    Wahidul Alam almost 2 years

    I am a laravel beginner and trying to pass id to controller method show. It does not showing anything after page reloads. I tried some google stuffs. It didnt bring any help. My related code is given below :

    admin.blade.php

    <div class="showOne">
    <?php if(isset($users)){
        var_dump($users);
    }
    ?>
    

    @foreach($inputs as $key => $user)
      <tr>
       <td>{{ ++$key }}</td>
       <td>{{ $user->name }}</td>
       <td>{{ $user->email }}</td>
       <td>{{ $user->address }}</td>
       <td>{{ $user->phone }}</td>
       <td>
        {{ Form::open(['route' => ['admin.show', $user->id], 'method' => 'get']) }}
        {{ Form::button('Details') }}
        {{ Form::close() }}
       </td>
       <td>
        {{ Form::open(['route' => ['admin.edit', $user->id], 'method' => 'get']) }}
        {{ Form::button('Edit') }}
        {{ Form::close() }}
       </td>
       <td>
        {{ Form::open(['route' => ['admin.delete', $user->id], 'method' => 'get']) }}
        {{ Form::button('Delete') }}
        {{ Form::close() }}
       </td>
     </tr>
    @endforeach
    

    controller:

    class AdminController extends \BaseController {
    
        /**
         * Display a listing of the resource.
         *
         * @return Response
         */
        public function index()
        {
            // get all the inputs
            $inputs = Userdatas::all();
    
            // load the view and pass the inputs
            return View::make('pages.admin')
                ->with('inputs', $inputs);
        }
    
        /**
         * Display the specified resource.
         *
         * @param  int  $id
         * @return Response
         */
        public function show($id)
        {
            $user = Userdatas::find($id);
            var_dump($user);
            die();
            return View::make('pages.admin')
                ->with('users', $user);
        }
    
    }
    

    routes:

    Route::get('admin', [
      'uses' => 'AdminController@index'
    ]);
    
    Route::get('admin/{id}', [
      'uses' => 'AdminController@show',
      'as' => 'admin.show'
    ]);
    
  • Wahidul Alam
    Wahidul Alam about 9 years
    it is working to some extent but now it showing error that "undefined variable inputs at admin.blade.php". I guess it is coming from controller@index method as I used same blade for these two method. Is it necessary to create blade for every method ?
  • Jirennor
    Jirennor about 9 years
    I use a blade for every method. Cause my edit and delete page are different from each other. Besides that I also use different variables on each of them. If I would use one blade if have to check first if the variable exist other wise I get errors like you are having now. Can you maybe update your admin.blade.php in your first post?