【问题标题】:ErrorException Undefined variable: contacts (View:MyCrud/resources/views/contacts/index.blade.php)ErrorException 未定义变量:联系人(查看:MyCrud/resources/views/contacts/index.blade.php)
【发布时间】:2019-09-05 12:39:17
【问题描述】:

我正在关注 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');

我无法找出问题所在。如果我需要提供任何其他详细信息,请告诉我。

附上错误截图:

【问题讨论】:

  • 请始终显示所有错误信息,包括文件名和行号
  • 试试get()方法
  • @RiggsFolly 我附上了错误截图。那会有帮助吗?显示的错误列表很长。
  • 是的,抱歉,看到顶部的错误,但最终到了底部的图片:)
  • @farooq 你能帮忙看看格式吗?

标签: php laravel laravel-5.8


【解决方案1】:

您将“/”直接返回到查看页面。 这就是为什么它没有从控制器获取联系人变量的原因。

在您的 web.php 文件中,

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

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

php artisan config:cache
php artisan route:clear

如果您仍然遇到问题,请告诉我

【讨论】:

  • 我应该替换 Route::get('/', function () { return view('contacts/index'); }); with Route::get('/', 'ContactController@index')->name('/');
  • @FEBzX 啊是的> 但问题是您为此使用了不同的方法。 Route::resource() 我从未在路由文件中使用过。我正在考虑为 CRUD 路由找到解决方案。一旦我得到答案,我会更新我的问题。
  • 好的。谢谢
猜你喜欢
  • 2019-08-06
  • 1970-01-01
  • 2019-08-29
  • 2021-03-16
  • 2021-11-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多