使用Laravel 5.2.45
我对Laravel和PHP一般都很新,所以我正在制作一个电话簿项目,我存储有关人员的联系信息。
我在名为'phonebook'的数据库中有一个名为'contacts'的表。
我有一个带有表单的视图,可以在此表中记录新条目。
这是观点:
phonebook.blade.php
....... css and stuff here .......
<body>
<h1> Contact Form </h1><br/>
{{-- FORM --}}
<form method = "POST" action = "contacts">
{{ csrf_field() }}
<div class = "form-group
@if($errors->has('name'))
has-error
@endif">
<label for = "name"> Name </label><br/>
<input type = "text" id = "name" class = "form-control" name = "name" placeholder = "Name your Name" value = "{!! old('name') !!}">
@if($errors->has('name'))
<p class = "help-block">{{ $errors->first('name') }}</p>
@endif
</div><br/>
<div class = "form-group
@if($errors->has('lastname'))
has-error
@endif">
<label for = "lastname"> Lastname </label><br/>
<input type = "text" id = "lastname" class = "form-control" name = "lastname" placeholder = "Name your Lastname" value = "{!! old('lastname') !!}">
@if($errors->has('lastname'))
<p class = "help-block">{{ $errors->first('lastname') }}</p>
@endif
</div><br/>
<div class = "form-group
@if($errors->has('email'))
has-error
@endif">
<label for = "email"> E-mail </label><br/>
<input type = "text" id = "email" class = "form-control" name = "email" placeholder = "somesomething@email.com" >
@if($errors->has('email'))
<p class = "help-block">{{ $errors->first('email') }}</p>
@endif
</div><br/>
<div class = "form-group
@if($errors->has('phone'))
has-error
@endif">
<label for = "phone"> Phone Number </label><br/>
<input type = "text" id = "phone" class = "form-control" name = "phone" placeholder = "I'll call you">
@if($errors->has('phone'))
<p class="help-block">{{ $errors->first('phone') }}</p>
@endif
</div><br/>
<div class = "form-group">
<label for = "address"> Address </label><br/>
<input type = "text" id = "address" class = "form-control" name = "address" placeholder = "I'll even visit you" value = "{!! old('address') !!}">
</div><br/>
<div>
<button type = "submit" class = "submit"> Submit Information </button>
<a href="contacts"><button type = "button"> View Contacts </button></a>
</div>
</form>
</body>
</html>
当我录制新条目时,我会被重定向到我可以看到该表的视图。 以简单的方式构建,这是视图:
contacts.blade.php
....... css and stuff here .......
<body>
<h1> Contacts </h1>
<br/>
<div>
<a href = "phonebook"><button class = "ret" type = "button"> Add New Entry </button></a>
</div>
<br/><br/>
<table class = "contacts">
<thead>
<tr>
<th> ID </th>
<th> Name </th>
<th> Lastname </th>
<th> E-Mail </th>
<th> Phone </th>
<th> Address </th>
<th> Edit </th>
<th> Delete </th>
</tr>
</thead>
<tbody>
@foreach($contact as $contact)
<tr class = "tableBody">
<td class = "id"> {{ $contact->id }} </td>
<td class = "name"> {{ $contact->name }} </td>
<td class = "lastname"> {{ $contact->lastname }} </td>
<td class = "email"> {{ $contact->email }} </td>
<td class = "phone"> {{ $contact->phone }} </td>
<td class = "address"> {{ $contact->address }} </td>
<td class = "edit"> <a href = "edit"> Edit </a> </td>
<td class = "delete"> <a href = "delete"> Delete </a> </td>
</tr>
@endforeach
</tbody>
</table>
<br/>
<div>
<a href = "phonebook"><button class = "ret" type = "button"> Add New Entry </button></a>
</div>
</body>
</html>
请注意,你会看到我的表格中有一个删除链接,我想用它来删除该给定行中的记录。
要做到这一点,我有一个删除视图,它是在destroy()
函数中的一段代码之后加载的,在我使用artisan控制台的控制器中提供了
这是我的routes.php文件,带有注释
routes.php文件
<?php
use Illuminate\Support\Facades\Input;
use App\Contact;
use App\Http\Controllers;
// default welcome view from Laravel
Route::get('/', function ()
{
return view('welcome');
});
// getting the routes from the controller
Route::resource('contacts', 'ContactsController');
// getting the form from the controller, wich is in a .blade.php view
Route::get('phonebook', 'ContactsController@index');
// showing the table in a .blade.php view
Route::get('contacts', 'ContactsController@tab');
// the route to post the contact in the table, using the form in the phonebook.blade.php
Route::post('contacts', 'ContactsController@create');
// routing to the destroy function in my controller
Route::get('delete', [
'as' => 'delete', 'uses' => 'ContactsController@destroy'
]);
// getting the edit.blade.php, not working
// permeate the inputs of name, lastname, email, phone and address
// with the same inputs we have in the row of our table
Route::get('edit', function()
{
return view('edit');
});
也许错误在我的路线中,这也是我在这里粘贴它的原因。
这是我的控制者,整体
带有评论的ContactsController.php
<?php
namespace App\Http\Controllers;
use App\Contact;
use DB;
use Illuminate\Http\Request;
use App\Http\Requests;
use Illuminate\Support\Facades\Input;
use Validator;
class ContactsController extends Controller
{
// showing the form in the phonebook.blade.php
// the form is structured using html and stuff
public function index()
{
return view('phonebook');
}
// showing the table where I save my contacts
// simple view stuff
public function tab()
{
return view('contacts');
}
// the parameters to save my contact, with errors and stuff
//working since day one
public function create()
{
$rules = array(
'name' => 'alpha|min:3|max:15|required',
'lastname' => 'alpha|min:3|max:15',
'email' => 'required|unique:contacts,email|email',
'phone' => 'required|unique:contacts,phone|alpha_num|between:3,25',
'address' => 'max:255',
);
$messages = array(
'alpha' => 'The :attribute must contain only letters.',
'max' => 'The :attribute must have a maximum of 15 letters.',
'min' => 'The :attribute must have at least 3 characters.',
'required' => 'The :attribute is really important. We are storing your contact info after all...',
'email' => 'The :attribute must be a valid e-mail format address.',
'numeric' => 'The :attribute must contain only numbers.',
'between' => 'The :attribute content must have a lenght between 3 and 25 characters.',
);
$validator = Validator::make(Input::all(), $rules, $messages);
if ($validator->fails())
{
$messages = $validator -> messages();
return redirect('phonebook')
->withInput()
->withErrors($validator);
}
else
{
$contact = new Contact;
$contact-> name = Input::get('name');
$contact-> lastname = Input::get('lastname');
$contact-> email = Input::get('email');
$contact-> phone = Input::get('phone');
$contact-> address = Input::get('address');
$contact->save();
// $contact = App\Contact::all();
return view('contacts', compact('contact'));
};
}
我还有一个编辑功能,它不起作用,但我不关心它。
我已经用几种方式编辑了我的销毁功能。
我现在会在这里列出它们,包括评论和它们返回的错误。
由于每个函数都应该返回delete.blade.php
,我不会在以下代码中写这一行。
销毁功能1
//error: undefined variable in the findOrFail()
$contact = Contact::findOrFail($contact);
$contact->delete();
独立于我想在findOrFail()中传递的参数,无论是$ id,$ name还是唯一的$ phone或$ email,错误与代码注释中提到的相同。
销毁功能2
//FatalErrorException in ContactsController.php line 93:
//Class 'App\Http\Controllers\Contacts' not found
Contacts::where('id','=',$id);
$id->delete();
在这一个和下一个中,我甚至不知道如何摆脱这个错误。
销毁功能3
//Same error as above
//Class not found
$contact = Contacts::where('id', '=', $id);
$contact->delete();
在下一篇文章中,我添加了一个if
语句来检查事情是否正常。
销毁功能4
//Class contacts not found
$id = contacts::where('id', $id)->first();
if ($id != null) {
$id->delete();
return view('delete');
}else{
return view('delete')->with(['message'=> 'Contact not found']);
};
下一个返回我不理解的错误。
销毁功能5
//Call to a member function delete() on null
//I don't know what that is supposed to mean
$id = Contact::all();
Contact::find($id) -> delete();
现在,下一段代码是感觉最好的代码,因为它返回了删除视图,但是没有删除我的表中的条目,甚至没有使用forceDelete()。
销毁功能6
//loading the view page, but not deleting the entry
// not even using forceDelete()
Contact::where('id', '=', '$id')
-> where('name', '=', '$name')
-> delete();
最后,为了好玩,我想,当我点击表格中的删除链接时,我正在发布下一行代码,使我的服务器停机
//This line destroys my local server
//I have to up it again, using art serve command line
// $this -> destroy(Contact::get());
所以,我在使我的销毁功能工作时遇到了麻烦。
这就是我需要的
我需要对我的功能进行一些修正,对于其中一个功能中的一次修正,我会非常高兴。 重申,没有这些函数正在运行,每个函数都会返回某种错误,正如您在代码本身提供的注释中所看到的那样。
就像我之前说过的那样, destroy function 6 是我觉得那个会起作用的那个,因为它正在返回我想要的视图,它只是没有删除我的条目桌子。
另外,我发布了所有这些代码,因为MAYBE错误是从后面一路走来的。
可以随意询问其他文件,例如模型,迁移和其他内容。
修改
这是我的联系模式:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Contact extends Model
{
// not using an id field
// could this be the error?
// isn't laravel making an automatic id field?
// dunno
public $fillable = array('name', 'lastname', 'email', 'phone', 'address');
//added because of error at migration
public $timestamps = false;
}
如代码中所述,我必须为false
添加timestamps
语句,因为它返回了错误。
现在,这里有点有用。
我使用第一个销毁功能并将其应用到我的代码中。
错误仍然相同,findOrFail()
中的未定义变量。
所以,我定义了变量,我不知道它是否是常见的做法。 这是它的样子。
$contact=Contact::first();
Contact::find($contact->id);
$contact->delete();
但是,这段代码会删除我桌面上第一个条目,与我点击删除按钮的位置无关,这是有道理的,这是 first()结束那里。
所以,既然这有点工作,我会使用什么声明来删除我想要的条目?
结束修改
很抱歉问题中代码过载。 并提前谢谢你们。
答案 0 :(得分:1)
** 编辑 **
您需要更新“删除”按钮所在的刀片式模板,以将联系人ID作为网址的一部分传递
Additional information: Unable to read data from the transport connection: Une connexion existante a dû être fermée par l’hôte distant.
然后更新您的删除路由,以在网址
中包含ID<tbody>
@foreach($contact as $contact)
<tr class = "tableBody">
<td class = "id"> {{ $contact->id }} </td>
<td class = "name"> {{ $contact->name }} </td>
<td class = "lastname"> {{ $contact->lastname }} </td>
<td class = "email"> {{ $contact->email }} </td>
<td class = "phone"> {{ $contact->phone }} </td>
<td class = "address"> {{ $contact->address }} </td>
<td class = "edit"> <a href = "edit"> Edit </a> </td>
<td class = "delete"> <a href = "delete/{{ $contact->id }}"> Delete </a> </td>
</tr>
@endforeach
</tbody>
然后让你的Controller destroy方法接受id
Route::get('delete/{id}', [
'as' => 'delete', 'uses' => 'ContactsController@destroy'
]);
原始版
使用$ contact-&gt; id作为此ID。 findOrFail需要传递id。
销毁功能1
public function destroy($id)
{
$contact = Contact::findOrFail($id);
$contact->delete();
}
销毁功能2
好像你没有Contacts类的import语句。检查Contacts类的命名空间,并在控制器类的顶部添加如下所示:
//error: undefined variable in the findOrFail()
$contact = Contact::findOrFail($contact->id);
$contact->delete();
OR
use App\Contact;
$contact = Contact::where('id','=',$id)->get();
$contact->delete();
销毁功能3
与上述相同。添加import / use语句。
Contact::destroy($id);