For example if there is a form which creates customers, and when you filled the form and submit the page should redirect and show the data which was inserted to that form by a specific individual id. in my case it redirects to a page and views all the data in the database.
Here’s my Web.php
Route::get('customers','CustomersController@index'); Route::get('customers/create','CustomersController@create'); Route::post('customers','CustomersController@store'); Route::get('customers/{customer}','CustomersController@show');
Here’s my Controller
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppCustomer; use AppCompany; class CustomersController extends Controller { public function index(){ $customers = Customer::all(); return view('customers.index',compact('customers')); } public function create(){ $companies = Company::all(); return view ('customers.create',compact('companies')); } public function store() { $data = request()->validate([ 'name'=>'required | min:3', 'email'=>'required | email', 'active'=>'required ', 'company_id'=>'required', ]); Customer::create($data); return redirect('customers'); } // * Route Model binding public function show(Customer $customer){ return view('customers.show',compact('customer')); } }
Here’s my Customer Model
<?php namespace App; use IlluminateDatabaseEloquentModel; class Customer extends Model { protected $guarded = []; public function getActiveAttribute($attribute){ return [ 0 => 'Inactive', 1 => 'Active', ] [$attribute]; } public function scopeActive($query){ return $query->where('active',1); } public function scopeInactive($query){ return $query->where('active',0); } public function company() { return $this->belongsTo(Company::class); } }
Here’s my Create Blade
@extends('layouts') @section('title','Add New Customer') @section('content') <div class="row"> <div class="col-12"> <h1>Add New Customer</h1> </div> </div> <div class="row"> <div class="col-12"> <form action="/customers" method="POST" > <div class="form-group"> <label for="name">Name</label> <input type="text" class="form-control" name="name" value="{{old('name')}}"> <div>{{$errors->first('name')}}</div> </div> <div class="form-group"> <label for="email">Email</label> <input type="text" class="form-control" name="email" value="{{old('email')}}"> <div>{{$errors->first('email')}}</div> </div> <div class="form-group"> <label for="">Customer Status</label> <select name="active" id="active" class=" form-control"> <option value="" disabled>Select Customer Status</option> <option value="1">Active</option> <option value="0">Inactive</option> </select> </div> <div class="form-group"> <label for="company_id">Company</label> <select name="company_id" id="company_id" class=" form-control"> @foreach ($companies as $company) <option value="{{ $company->id }}">{{ $company->name }}</option> @endforeach </select> </div> <button type="submit" class=" btn btn-dark">Add Customer</button> @csrf </form> </div> </div> @endsection
Here’s my show blade
@extends('layouts') @section('title','Details for ' . $customer->name) @section('content') <div class="row"> <div class="col-12"> <h1>Details for {{ $customer->name }}</h1> </div> </div> <div class="row"> <div class="col-12"> <p><strong>Name : </strong> {{ $customer->name }}</p> <p><strong>Email : </strong> {{ $customer->email }}</p> <p><strong>Company : </strong> {{ $customer->company->name }}</p> <p><strong>Status : </strong> {{ $customer->active }}</p> </div> </div> @endsection
Advertisement
Answer
Redirect to the show
route rather than the customers index
route:
$customer = Customer::create($data); return redirect('customers/' . $customer->id);