ErrorException未定义的变量:联系人(View:MyCrud / resources / views / contacts / index.blade.php)

时间:2019-09-05 12:39:17

标签: php laravel laravel-5.8

我正在按照本教程进行CRUD操作:https://www.techiediaries.com/php-laravel-crud-mysql-tutorial/

集成完整的教程后,在运行应用程序时出现此错误。

  

未定义变量:联系人(视图:MyCrud / resources / views / contacts / index.blade.php)

我的 Controller 如下 ContactController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Contact;  
class ContactController extends Controller
{

public function index()
{
$contacts = Contact::all();
return view('contacts.index', compact('contacts'));
}

public function create()
{
    return view('contacts.create');
}
public function store(Request $request)
{
    $request->validate([
      'first_name'=>'required',
      'last_name'=>'required',
      'email'=>'required'
    ]);

      $contact = new Contact([
        'first_name' => $request->get('first_name'),
        'last_name' => $request->get('last_name'),
        'email' => $request->get('email'),
        'job_title' =>$request->get('job_title'),
        'city' =>$request->get('city'),
        'country' =>$request->get('country'),
      ]);
      $contact->save();
      return redirect('/contacts')->with('success', 'Contact Saved');
}
public function show($id)
{
    //
}

public function edit($id)
{
  $contact = Contact::find($id);
  return view('contacts.edit', compact('contact'));
}


public function update(Request $request, $id)
{
  $request->validate([
     'first_name'=>'required',
     'last_name'=>'required',
     'email'=>'required'
 ]);

 $contact = Contact::find($id);
 $contact->first_name =  $request->get('first_name');
 $contact->last_name = $request->get('last_name');
 $contact->email = $request->get('email');
 $contact->job_title = $request->get('job_title');
 $contact->city = $request->get('city');
 $contact->country = $request->get('country');
 $contact->save();

 return redirect('/contacts')->with('success', 'Contact updated!');
}
public function destroy($id)
{
  $contact = Contact::find($id);
  $contact->delete();

  return redirect('/contacts')->with('success', 'Contact deleted!');
}
}

下面是我的视图 index.blade.php

@extends('base')
@section('main')
<div class="row">
 <div class="col-sm-12">
   <h1 class="display-3">Contacts</h1>
     <table class="table table-striped">
       <thead>
         <tr>
           <td>ID</td>
           <td>Name</td>
           <td>Email</td>
           <td>Job Title</td>
           <td>City</td>
           <td>Country</td>
           <td colspan = 2>Actions</td>
        </tr>
      </thead>
   <tbody>
    @foreach($contacts as $contact)
    <tr>
        <td>{{$contact->id}}</td>
        <td>{{$contact->first_name}} {{$contact->last_name}}</td>
        <td>{{$contact->email}}</td>
        <td>{{$contact->job_title}}</td>
        <td>{{$contact->city}}</td>
        <td>{{$contact->country}}</td>
        <td>
            <a href="{{ route('contacts.edit',$contact->id)}}" class="btn btn-primary">Edit</a>
        </td>
        <td>
            <form action="{{ route('contacts.destroy', $contact->id)}}" method="post">
              @csrf
              @method('DELETE')
              <button class="btn btn-danger" type="submit">Delete</button>
            </form>
        </td>
    </tr>
    @endforeach
</tbody>
 </table>
<div>
</div>
@endsection

这是 routes / web.php

<?php
Route::get('/', function () {
return view('contacts/index');
});
Route::resource('contacts','ContactController');

我无法找出问题所在。让我知道是否需要提供其他详细信息。

附加错误的屏幕截图: enter image description here

1 个答案:

答案 0 :(得分:3)

您直接将“ /”返回到查看页面。 所以这就是为什么它没有从controller中获取contacts变量的原因。

在您的web.php文件中,

Route::get('/', 'ContactController@index')->name('/');

现在尝试使用清除缓存和路由

php artisan config:cache
php artisan route:clear

让我知道您是否仍然遇到问题