simran2350's avatar

TypeError: Too few arguments in function

I got this error while trying the following code.

Route - Route::get('/show', 'Controller@show'); Controller - public function show($id)

{
  $user = User::findOrFail($id);
  return view("show")->withUser($user);
}

Error - Type error: Too few arguments to function App\Http\Controllers\Controller::show(), 0 passed and exactly 1 expected

Please suggest

0 likes
8 replies
topvillas's avatar

You need to give the route a parameter

Route::get('/show/{id}', 'Controller@show');
simran2350's avatar

@topvillas Ok thanks One more thing, please suggest changes in the blade file as well

show.blade

@extends('app')

@section('content')

Add Information | Listing Page

      <table class="table is-narrow">
        <thead>
          <tr>
            <th>Name</th>
            <th>Province</th>
            <th>Telephone</th>
            <th>Postalcode</th>
            <th>Salary</th>
            <th></th>
          </tr>
        </thead>

        <tbody>
          @foreach ($user as $usr)
            <tr>
              <th>{{$usr->name}}</th>
              <td>{{$usr->province}}</td>
              <td>{{$usr->telephone}}</td>
              <td>{{$usr->postalcode}}</td>
              <td>{{$usr->salary}}</td>
              <td class="has-text-right">
                <a class="button is-outlined m-r-5" href="/show">View</a>
                <a class="button is-light" href="/edit">Edit</a>
              </td>
            </tr>
          @endforeach
        </tbody>
      </table>
      {{ $user->links() }}

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

@endsection

Jaytee's avatar

The show method is for showing one user. IF you want to show multiple, use an index method.

Your show method is only querying one user, so why are you looping over it? Again, thats a job for the index method.

simran2350's avatar

@Jaytee Basically I want to show the detail of individual user and also want to update/delete the details of each user. Please suggest the changes required.

Route file - Route::get('/add_info', 'Controller@index'); Route::post('/store', 'Controller@store'); Route::get('/list', 'Controller@index1'); Route::get('/edit/{id}', 'Controller@edit'); Route::get('/show/{id}', 'Controller@show');

Controller file -

Snapey's avatar

please format your code blocks

you can do this by putting three backticks ``` before and after your code

Snapey's avatar
      <table class="table is-narrow">
        <thead>
          <tr>
            <th>Name</th>
            <th>Province</th>
            <th>Telephone</th>
            <th>Postalcode</th>
            <th>Salary</th>
            <th></th>
          </tr>
        </thead>

        <tbody>
          @foreach ($user as $usr)
            <tr>
              <th>{{$usr->name}}</th>
              <td>{{$usr->province}}</td>
              <td>{{$usr->telephone}}</td>
              <td>{{$usr->postalcode}}</td>
              <td>{{$usr->salary}}</td>
              <td class="has-text-right">
                <a class="button is-outlined m-r-5" href="/show/{{ $usr->id }}">View</a>
                <a class="button is-light" href="/edit/{{ $usr->id }}">Edit</a>
              </td>
            </tr>
          @endforeach
        </tbody>
      </table>
      {{ $user->links() }}

     </div>
     </div>
     </div>
     </div>
simran2350's avatar

@Snapey Please suggest the changes required.

My queries are :

  1. How to redirect the user to show page after storing the form details in db?
  2. Please suggest the changes requires in the store and update method?
  3. How to update the user's details and then redirect to the show page?

Route file -

''' Route::get('/add_info', 'Controller@index'); Route::post('/store', 'Controller@store'); Route::get('/list', 'Controller@index1'); Route::get('/edit/{id}', 'Controller@edit'); Route::get('/show/{id}', 'Controller@show');

'''

Controller file -

''' public function store(Request $request) { $this->validate($request, [ 'name' => 'required|min:2', ]);

  $user = new User();
  $user->name = $request->name;
  $user->province = $request->province;
  $user->telephone = $request->telephone;
  $user->postalcode = $request->postalcode;
  $user->salary = $request->salary;
  $user->save();
  return redirect()->route('show/{id}', $user->id);
 }

public function show($id)

{
  $user = User::findOrFail($id);
  return view("show")->withUser($user);
}

public function edit($id) { $user = User::where('id', $id)->first(); return view("edit")->withUser($user); }

public function update(Request $request, $id)
{
  $this->validate($request, [
    'name' => 'required|min:2',
  ]);

  $user = User::findOrFail($id);
  $user->name = $request->name;
  $user->province = $request->province;
  $user->telephone = $request->telephone;
  $user->postalcode = $request->postalcode;
  $user->salary = $request->salary;
  $user->save();

  return redirect()->route('show/{id}', $id);

'''

simran2350's avatar

@Snapey Blade files

edit.blade

''' form action="{{route('update')}}" method="POST" {{method_field('PUT')}}

'''

add_info.blade

''' form action="{{route('store')}}" method="POST" '''

Please or to participate in this conversation.