编辑表单不显示要在laravel 5中编辑的值

时间:2017-02-28 12:23:53

标签: laravel-5.4

我正在尝试在Laravel 5.4中创建CRUD应用程序但是我完成了创建和显示表单现在剩余编辑和删除但编辑表单确实显示信息而不是它带来创建表单并创建新记录而不是创建。任何帮助将不胜感激.Below是我的控制者

    <?php

namespace App\Http\Controllers;

use \App\Http\Requests\contactsrequest;
use App\contacts; // contacts model included using "use"


class contactscontroller extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //Display Data
        //find contact by contacts model
        // use model contacts and include the use App\contacts model 
        // variable contact holds  all values of contacts table
        $contact=contacts::all(); // contavt
        return view('contacts.index',['contacts'=>$contact]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('contacts.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(contactsrequest $request)
    {
        //contacts model
        contacts::create($request->all());
        return redirect()->route('contacts.index')->with('message', 'Contacts has been added successfully');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(contacts $contacts)
    {
        //

        return view('contacts.edit', compact('contacts'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(contactsrequest $request, contacts $contacts)
    {
        //
        $contacts->update($request->all());
        return redirect()->route('contacts.index')->with('message', 'Contacts has been updated successfully');

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}





The edit form is below
<!-- Home page to Display contacts -->
@extends('master')

@section('content')
<div class="container">
<div class="row">
  <div class="col-md-10 col-md-offset-1">
<div class="panel panel-primary">
  <div class="panel-heading">
    <h3 class="panel-title">Update Contacts</h3>
  </div>
  <div class="panel-body">

           <!-- $contacts is object -->
           {{Form::model($contacts, array('route'=>['contacts.update', $contacts->id],'method'=>'POST'))}}
    <!-- input name, value is null, the parameters e.g class, placeholder etc -->
      <div class="form-group">
        {{Form:: label('Name','Enter name') }}
        {{Form:: text('Name',null, ['class'=>'form-control','placeholder'=>'Enter Name']) }}  
      </div>
      <div class="form-group">
        {{Form:: label('Phone','Mobile Number') }}
        {{Form:: text('Phone',null, ['class'=>'form-control','placeholder'=>'Enter Mobile Number']) }}
      </div>
      <div class="form-group">
        {{Form:: label('Email','Email Address') }}
        {{Form:: email('Email', null, ['class'=>'form-control','placeholder'=>'Enter Email Address']) }}
      </div>

      <div class="form-group">
           {{ Form::button('Update',['class'=>'btn btn-primary', 'type'=>'submit'] )}}  
           |
         <a href="{{route('contacts.index')}}" class="btn btn-warning btn-sm"> <i class="fa fa-plus-square fa-lg" aria-hidden="true" style="border: none;"></i>Back</a>   
            </div>
      {{Form:: close() }}

  </div>
  </div>

</div>
</div>
</div>
@endsection

2 个答案:

答案 0 :(得分:0)

您的视图希望将模型称为$ contacts,因此将控制器中的edit()方法更改为以下内容:

public function edit($id)
    {
        $contact = Contacts::findOrFail($id);
        return view('contacts.edit', ['contacts' => $contact]);
    }

答案 1 :(得分:0)

在您的控制器中尝试此操作

public function edit($id)
{
    $contacts = Contacts::findOrFail($id);
    Return to view ('contacts. edits', compact ('contacts'));
}