Php 删除表中的记录
使用Laravel 5.2.45Php 删除表中的记录,php,laravel-5.2,Php,Laravel 5.2,使用Laravel 5.2.45 //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(); 一般来说,我对Laravel和PHP还比较陌生,所以我正在做一个电话簿项目,在那里我存储人们的联系信息 我的数据库中有一个名为“联系人”的表,名为“电话簿” 我有一个表单
//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();
一般来说,我对Laravel和PHP还比较陌生,所以我正在做一个电话簿项目,在那里我存储人们的联系信息
我的数据库中有一个名为“联系人”的表,名为“电话簿”
我有一个表单视图,可以在此表中记录新条目
这是一种观点:
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>
....... 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>
<?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'));
};
}
与我要在findOrFail()中传递的参数无关,无论是$id、$name还是uniques$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();
现在,下一段代码是感觉最好的代码,因为它返回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());
因此,我在使销毁函数工作时遇到了困难
这就是我需要的
我需要在我的函数中做一些修改,如果其中一个函数中有一个修改,我会非常高兴。
再次重申,这些函数都不起作用,它们中的每一个都返回某种错误,正如大家在代码本身提供的注释中看到的那样
正如我之前所说的,销毁函数6是我认为将要运行的函数,因为它返回我想要的视图,而不是删除表中的条目
另外,我发布了所有这些代码,因为可能是从后面传来的错误
可以随意要求其他文件,如模型、迁移等
编辑
这是我的联系方式:
<?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;
}
但是,这段代码删除了我表格中的第一个条目,与我单击delete按钮的位置无关,这很有意义,那是一个first()
既然这是一种工作方式,我应该用什么语句让它删除我想要的条目?
//Same error as above
//Class not found
$contact = Contacts::where('id', '=', $id);
$contact->delete();
结束编辑
抱歉,问题中的代码太多。
提前谢谢大家。***编辑** 您需要更新“删除”按钮所在的刀片模板,以将联系人id作为url的一部分传递
<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>
然后让控制器销毁方法接受id
public function destroy($id)
{
$contact = Contact::findOrFail($id);
$contact->delete();
}
原始版本
使用$contact->id进行此操作。findOrFail需要传递id
销毁功能1
//error: undefined variable in the findOrFail()
$contact = Contact::findOrFail($contact);
$contact->delete();
//error: undefined variable in the findOrFail()
$contact = Contact::findOrFail($contact->id);
$contact->delete();
销毁功能2
//FatalErrorException in ContactsController.php line 93:
//Class 'App\Http\Controllers\Contacts' not found
Contacts::where('id','=',$id);
$id->delete();
似乎您没有联系人类的导入语句。检查Contacts类的名称空间,并将其添加到控制器类的顶部,如下所示:
use App\Contact;
$contact = Contact::where('id','=',$id)->get();
$contact->delete();
或
销毁功能3
同上。添加导入/使用语句
//Same error as above
//Class not found
$contact = Contact::where('id', '=', $id)->get();
$contact->delete();
我尝试使用“use”语句导入该类,但显示了相同的错误。另外,我的模型位于根应用程序目录中。是否添加了
use-App\Contact代码>?另外,我在你的问题中看到两个不同的名字。类名是联系人
还是联系人
?使用正确的导入。是的,我添加了Use-App\Contact代码>。这次我用对了名字。我在测试这些代码,所以在放弃之前我改变了很多东西。它仍然不起作用吗?你能发布你的更新代码和你可能会遇到的任何错误吗?很抱歉迟到了。错误仍然是一样的,它无法识别Contact类,即使我在文件中声明了它。请输入一些错误。你好,Pandhi。错误与代码块以及其他类型的注释有关。