删除 table 中的记录
Deleting a record in a table
使用Laravel 5.2.45
总的来说,我对 Laravel 和 PHP 还比较陌生,所以我正在制作一个 phone 书籍项目,我在其中存储有关人员的联系信息。
我在名为 'phonebook' 的数据库中有一个名为 'contacts' 的 table。
我有一个视图,可以在 table 中记录新条目。
这是观点:
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>
当我记录新条目时,我被重定向到可以看到 table 的视图。
结构简单,这是视图:
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>
注意,您会看到我的 table 中有一个 Delete Link,我想用它来删除给定行中的记录。
为此,我有一个删除视图,它在 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'));
};
}
我也有一个编辑功能,但是没有用,但我不关心它。
我以多种方式编辑了销毁函数。
现在我将在此处列出它们,包括它们return编辑的评论和错误。
因为每个函数都应该 return delete.blade.php
,所以我不会在下面的代码中写这一行。
销毁函数1
//error: undefined variable in the findOrFail()
$contact = Contact::findOrFail($contact);
$contact->delete();
独立于我想在 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']);
};
下一个return我不明白的错误。
销毁函数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();
现在,下一段代码感觉是最好的,因为它 return 正在删除视图,但没有删除我的 table 中的条目,不是即使使用 forceDelete().
销毁函数6
//loading the view page, but not deleting the entry
// not even using forceDelete()
Contact::where('id', '=', '$id')
-> where('name', '=', '$name')
-> delete();
最后,我想,只是为了好玩,我发布了下一行代码,当我单击 table
中的删除 link 时,它会关闭我的服务器
//This line destroys my local server
//I have to up it again, using art serve command line
// $this -> destroy(Contact::get());
所以,我在使我的销毁功能起作用时遇到了问题。
这就是我需要的
我需要对我的功能进行一些更正,如果其中一个功能中的一个更正,我会非常满意。
重申一下,NONE 这些函数正在运行,其中每个函数 return 都存在某种错误,正如你们都可以在代码本身提供的注释中看到的那样。
就像我之前说的,destroy 函数 6 是我认为会起作用的函数,因为它正在 returning 视图我想要,它只是不删除我在 table.
中的条目
此外,我发布了所有这些代码,因为错误可能是从后面一直传来的。
随意询问其他文件,例如模型、迁移和其他文件。
编辑
这是我的联系人模型:
<?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;
}
如代码中所述,我必须为 timestamps
添加 false
语句,因为它 return 出错了。
现在,这里有些东西有点管用。
我采用了第一个销毁函数并将其应用到我的代码中。
错误还是一样,undefined variable in the findOrFail()
.
所以,我定义了变量,我不知道这是不是惯例。
这是它的样子。
$contact=Contact::first();
Contact::find($contact->id);
$contact->delete();
但是,这段代码删除了 我 table 中的第一个 条目,与我单击删除按钮的位置无关,这是有道理的,那是 first()那边
所以,既然这有点管用,我用什么语句让它删除我想要的条目?
结束编辑
很抱歉问题中的代码过多。
提前谢谢大家。
** 编辑 **
您需要更新删除按钮所在的 blade 模板,以将联系人 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 包含在 url
Route::get('delete/{id}', [
'as' => 'delete', 'uses' => 'ContactsController@destroy'
]);
然后让您的 Controller destroy 方法接受 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->id);
$contact->delete();
销毁函数2
您似乎没有联系人 class 的导入语句。检查您的联系人 class 的命名空间并将其添加到控制器的顶部 class:
use App\Contact;
$contact = Contact::where('id','=',$id)->get();
$contact->delete();
或
Contact::destroy($id);
销毁函数3
同上。添加 import/use 语句。
//Same error as above
//Class not found
$contact = Contact::where('id', '=', $id)->get();
$contact->delete();
使用Laravel 5.2.45
总的来说,我对 Laravel 和 PHP 还比较陌生,所以我正在制作一个 phone 书籍项目,我在其中存储有关人员的联系信息。
我在名为 'phonebook' 的数据库中有一个名为 'contacts' 的 table。
我有一个视图,可以在 table 中记录新条目。
这是观点:
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>
当我记录新条目时,我被重定向到可以看到 table 的视图。 结构简单,这是视图:
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>
注意,您会看到我的 table 中有一个 Delete Link,我想用它来删除给定行中的记录。
为此,我有一个删除视图,它在 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'));
};
}
我也有一个编辑功能,但是没有用,但我不关心它。
我以多种方式编辑了销毁函数。
现在我将在此处列出它们,包括它们return编辑的评论和错误。
因为每个函数都应该 return delete.blade.php
,所以我不会在下面的代码中写这一行。
销毁函数1
//error: undefined variable in the findOrFail()
$contact = Contact::findOrFail($contact);
$contact->delete();
独立于我想在 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']);
};
下一个return我不明白的错误。
销毁函数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();
现在,下一段代码感觉是最好的,因为它 return 正在删除视图,但没有删除我的 table 中的条目,不是即使使用 forceDelete().
销毁函数6
//loading the view page, but not deleting the entry
// not even using forceDelete()
Contact::where('id', '=', '$id')
-> where('name', '=', '$name')
-> delete();
最后,我想,只是为了好玩,我发布了下一行代码,当我单击 table
中的删除 link 时,它会关闭我的服务器//This line destroys my local server
//I have to up it again, using art serve command line
// $this -> destroy(Contact::get());
所以,我在使我的销毁功能起作用时遇到了问题。
这就是我需要的
我需要对我的功能进行一些更正,如果其中一个功能中的一个更正,我会非常满意。 重申一下,NONE 这些函数正在运行,其中每个函数 return 都存在某种错误,正如你们都可以在代码本身提供的注释中看到的那样。
就像我之前说的,destroy 函数 6 是我认为会起作用的函数,因为它正在 returning 视图我想要,它只是不删除我在 table.
中的条目此外,我发布了所有这些代码,因为错误可能是从后面一直传来的。
随意询问其他文件,例如模型、迁移和其他文件。
编辑
这是我的联系人模型:
<?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;
}
如代码中所述,我必须为 timestamps
添加 false
语句,因为它 return 出错了。
现在,这里有些东西有点管用。
我采用了第一个销毁函数并将其应用到我的代码中。
错误还是一样,undefined variable in the findOrFail()
.
所以,我定义了变量,我不知道这是不是惯例。 这是它的样子。
$contact=Contact::first();
Contact::find($contact->id);
$contact->delete();
但是,这段代码删除了 我 table 中的第一个 条目,与我单击删除按钮的位置无关,这是有道理的,那是 first()那边
所以,既然这有点管用,我用什么语句让它删除我想要的条目?
结束编辑
很抱歉问题中的代码过多。 提前谢谢大家。
** 编辑 **
您需要更新删除按钮所在的 blade 模板,以将联系人 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 包含在 url
Route::get('delete/{id}', [
'as' => 'delete', 'uses' => 'ContactsController@destroy'
]);
然后让您的 Controller destroy 方法接受 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->id);
$contact->delete();
销毁函数2
您似乎没有联系人 class 的导入语句。检查您的联系人 class 的命名空间并将其添加到控制器的顶部 class:
use App\Contact;
$contact = Contact::where('id','=',$id)->get();
$contact->delete();
或
Contact::destroy($id);
销毁函数3
同上。添加 import/use 语句。
//Same error as above
//Class not found
$contact = Contact::where('id', '=', $id)->get();
$contact->delete();