I have my DB, already migrated everything, my code can record new entries in my table when required, and so on.
I have a table with the contents of: 'id', 'name', 'lastname', 'email', 'phone', 'address'.
What I want is, when you hit the submit button, you'll be redirected to a new 'view.blade.php' where you can see all of the DB's entries, including the most recent. BUT I don't know how to develop this code to make it work.
I need help with that, please. Thank you.
In your controller function do this right after you store a new record to table. assuming your model name is User
fetch all user records from User model,
$users = App\User::all();
pass this users variable to view(assuming your view.blade.php
is in resources/views
folder)
return View::make('view', compact('users'));
and then in your view.blade.php you can do something like this to display all user records.
<table>
<thead>
<tr>
<th> id</th>
<th> name</th>
<th> last name </th>
<th> email </th>
<th> phone</th>
<th> adddress </th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td> {{$user->id}} </td>
<td> {{$user->name}} </td>
<td> {{$user->last_name}} </td>
<td> {{$user->email}} </td>
<td> {{$user->phone}} </td>
<td> {{$user->address}} </td>
</tr>
@endforeach
</tbody>
</table>
//form.blade.php
<form method="post" action="{{ route('submitForm') }}">
{!! csrf_field() !!}
<input type="text" name="name"/>
<input type="text" name="last_name"/>
<input type="text" name="email"/>
<input type="text" name="phone"/>
<input type="text" name="address"/>
</form>
<?php
//app/Http/controllers/YourController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
//user is your eloquent model
class YourController extends Controller
{
//show user form
public function showForm()
{
return view('form');
}
//post url for submit form
public function submitForm(Request $request){
$this->validate($request, [
'name' => 'required|min:2|max:30',
'lastname' => 'required|min:2|max:30',
'email' => 'required',
'phone' => 'required',
'address' => 'required'
]);
$user = new User();
$user->name = $request->name;
$user->lastname = $request->lastname;
$user->email = $request->email;
$user->phone = $request->phone;
$user->address = $request->address;
try{
$user->save();
return redirect()->route('showAllusers')->with('success', "User was successfully created..!");
}
catch(Exception $e){
return redirect()->back()->with('error', "Could not save the user!");
}
return redirect()->back()->with('error', "Error Occured, please try again!");
}
// after form submit, redirect to this page where all users will be showcased
public function showAllusers()
{
$users = User::all();
return view('allusers', compact('users'));
}
}
?>
//allusers.blade.php
@if($users->count > 0)
<table>
<thead>
<tr>
<th> id</th>
<th> name</th>
<th> last name </th>
<th> email </th>
<th> phone</th>
<th> adddress </th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td> {{$user->id}} </td>
<td> {{$user->name}} </td>
<td> {{$user->last_name}} </td>
<td> {{$user->email}} </td>
<td> {{$user->phone}} </td>
<td> {{$user->address}} </td>
</tr>
@endforeach
</tbody>
</table>
@else
<p> No users found..</p>
@endif
//routes.php
<?php
Route::get('/showForm', 'YourController@index')->name('showForm');
Route::post('/submitForm', 'YourController@index')->name('submitForm');
Route::get('/showAllusers', 'YourController@index')->name('showAllusers');
?>
I should write this code for display all users
table data :
//Function for store users data
public fucntion store(Request $request){
$insertData = DB::table('users')->insert(array(
'name'=>$request->name,
'lastname'=>$request->lastname,
'email'=>$request->email,
'phone'=>$request->phone,
'address'=>$request->address
));
return Redirect::to('/view')
}
//fucntion for display users data
public fucntion view(){
$rsltUsers = User::get();
return view('view', compact('rsltUsers'));
}
view.blade.php
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Name</th>
<th>Last Name</th>
<th>Email</th>
<th>Phone</th>
<th>Address</th>
</tr>
</thead>
<tbody>
@foreach($rsltUsers as $rsltUser)
<td>{!! $rsltUser->name !!}</td>
<td>{!! $rsltUser->lastname !!}</td>
<td>{!! $rsltUser->email !!}</td>
<td>{!! $rsltUser->phone !!}</td>
<td>{!! $rsltUser->address !!}</td>
@endforeach
</tbody>
</table>