You need to give the route a parameter
Route::get('/show/{id}', 'Controller@show');
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
You need to give the route a parameter
Route::get('/show/{id}', 'Controller@show');
@topvillas Ok thanks One more thing, please suggest changes in the blade file as well
show.blade
@extends('app')
@section('content')
<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>
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.
@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 -
please format your code blocks
you can do this by putting three backticks ``` before and after your code
<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>
@Snapey Please suggest the changes required.
My queries are :
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);
'''
@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.