如何重定向到仅显示从该 ID 插入数据库的数据的页面

How to redirect to the page which shows only the data inserted to the database from that id

例如,如果有一个创建客户的表单,当您填写表单并提交页面时,页面应该重定向并显示通过特定个人 ID 插入该表单的数据。在我的例子中,它重定向到一个页面并查看数据库中的所有数据。

这是我的 Web.php

Route::get('customers','CustomersController@index');
Route::get('customers/create','CustomersController@create');
Route::post('customers','CustomersController@store');
Route::get('customers/{customer}','CustomersController@show');

这是我的控制器

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Customer;

use App\Company;

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'));
    }
}

这是我的 Customer Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

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);
      }
}

这是我的 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

这是我的 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

重定向到 show 路线而不是客户 index 路线:

$customer = Customer::create($data);

return redirect('customers/' . $customer->id);