Laravel 软删除在一张表中不起作用
Posted
技术标签:
【中文标题】Laravel 软删除在一张表中不起作用【英文标题】:Laravel Soft Deletes dosent work in one table 【发布时间】:2020-08-03 10:27:52 【问题描述】:我试图在我的数据库表中进行软删除,但它在一个表中失败。我用其他桌子做了同样的事情,但只有一张不起作用。当我在视图中单击删除时,它会删除数据库中的行。我将展示这个表的软删除代码,希望你能帮助我。
这是我的迁移:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Invoices extends Migration
/**
* Run the migrations.
*
* @return void
*/
public function up()
Schema::create('invoices', function (Blueprint $table)
$table->increments('id');
$table->string('invoicenumber')->nullable();
$table->date('invoicedate')->nullable();
$table->date('selldate')->nullable();
$table->integer('user_id')->unsigned()->nullable();
$table->integer('form_id')->unsigned()->nullable();
$table->integer('currency_id')->unsigned()->nullable();
$table->integer('proform_id')->unsigned()->nullable();
$table->string('paymentmethod')->nullable();
$table->date('paymentdate')->nullable();
$table->string('status')->nullable();
$table->string('comments')->nullable();
$table->string('city')->nullable();
$table->string('paid')->nullable();
$table->string('autonumber')->nullable();
$table->string('automonth')->nullable();
$table->string('autoyear')->nullable();
$table->string('name')->nullable();
$table->string('PKWIU')->nullable();
$table->string('quantity')->nullable();
$table->string('unit')->nullable();
$table->string('netunit')->nullable();
$table->string('nettotal')->nullable();
$table->string('VATrate')->nullable();
$table->string('grossunit')->nullable();
$table->string('grosstotal')->nullable();
$table->timestamps();
$table->time('deleted_at')->nullable();
);
Schema::table('invoices', function (Blueprint $table)
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade');
$table->foreign('form_id')
->references('id')
->on('forms')
->onDelete('cascade');
$table->foreign('currency_id')
->references('id')
->on('currencys')
->onDelete('cascade');
$table->foreign('proform_id')
->references('id')
->on('proforms')
->onDelete('cascade');
);
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
Schema::dropIfExists('invoices');
这是发票模型。发票.php
<?php
namespace App;
use Kyslik\ColumnSortable\Sortable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Invoice extends Model
/**
* The attributes that are mass assignable.
*
* @var array
*/
use SoftDeletes;
use Sortable;
protected $table = 'invoices';
protected $fillable = [
'invoicenumber', 'invoicedate', 'id', 'selldate', 'user_id', 'paymentmethod',
'paymentdate', 'status', 'comments', 'city', 'paid', 'autonumber', 'automonth', 'autoyear', 'name',
'PKWIU', 'quantity', 'unit', 'netunit', 'nettotal',
'VATrate', 'grossunit', 'grosstotal', 'form_id', 'currency_id',
];
public $sortable = [ 'invoicenumber', 'invoicedate', 'id', 'selldate',
'user_id', 'paymentmethod', 'paymentdate', 'status', 'comments', 'grosstotal', 'nettotal', 'form_id', 'currency_id',];
protected $dates = ['deleted_at'];
public $primaryKey = 'id';
public function user()
return $this->belongsTo('App\User');
public function form()
return $this->hasOne('App\Form');
public function currency()
return $this->hasOne('App\Currency');
public function proform()
return $this->belongsTo('App\Proform');
InvoiceController.php
<?php
namespace App\Http\Controllers;
use Kyslik\ColumnSortable\Sortable;
use App\Invoice;
use Illuminate\Http\Request;
use App\User;
use App\Proform;
use App\Form;
use App\Currency;
use DB;
class InvoiceController extends Controller
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
function __construct()
$this->middleware('permission:product-list|product-create|product-edit|product-delete', ['only' => ['index','show']]);
$this->middleware('permission:product-create', ['only' => ['create','store']]);
$this->middleware('permission:product-edit', ['only' => ['edit','update']]);
$this->middleware('permission:product-delete', ['only' => ['destroy']]);
public function search4(Request $request)
$user = User::all('showname','id');
$invoices = Invoice::sortable()->paginate(5);
$query = DB::table('users')
->join('invoices', 'users.id', '=', 'invoices.user_id');
$search = $request->get('search');
$requestData = ['showname'];
/* $query = Proform::query(); */
foreach ($requestData as $field)
$query->orWhere($field, 'like', '%'.$search.'%');
$data2=$query->paginate(5);
return view('invoices.index', ['invoices' => $data2, 'user'=> $user])->with('i', ($request->input('page', 1) - 1) * 5);
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
$user = User::all('showname','id');
$invoices = Invoice::sortable()->paginate(5);
return view('invoices.index',compact('invoices', 'user'))
->with('i', (request()->input('page', 1) - 1) * 5);
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
$users = User::all('showname','id');
$forms = Form::all('id', 'form');
$currencys = Currency::all('id', 'currency', 'course');
return view('invoices.create')->with('users', $users, 'forms', 'currencys');
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
request()->validate([
'invoicedate' => 'required',
'user_id' => 'required',
'selldate' => 'required',
'paymentdate' => 'required',
'paymentmethod' => 'required',
'status' => 'required',
'comments' => 'nullable',
'city' => 'nullable',
'paid' => 'nullable',
'name' => 'required',
'PKWIU' => 'nullable',
'quantity' => 'required',
'unit' => 'required',
'netunit' => 'required',
'nettotal' => 'required',
'VATrate' => 'required',
'grossunit' => 'required',
'grosstotal' => 'required',
]);
Invoice::create($request->all());
return redirect()->route('invoices.index')
->with('success','Invoice created successfully.');
/**
* Display the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Invoice $invoice)
return view('invoices.show',compact('invoice'));
/**
* Show the form for editing the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function edit(Invoice $invoice)
$users = User::all('showname','id');
$forms = Form::all('id', 'form');
$currencys = Currency::all('id', 'currency', 'course');
return view('invoices.edit',compact('invoice', 'users', 'forms', 'currencys'));
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request, Invoice $invoice)
request()->validate([
'invoicedate' => 'required',
'user_id' => 'required',
'selldate' => 'required',
'paymentdate' => 'required',
'paymentmethod' => 'required',
'status' => 'required',
'comments' => 'nullable',
'city' => 'nullable',
'paid' => 'nullable',
'name' => 'required',
'PKWIU' => 'nullable',
'quantity' => 'required',
'unit' => 'required',
'netunit' => 'required',
'nettotal' => 'required',
'VATrate' => 'required',
'grossunit' => 'required',
'grosstotal' => 'required',
]);
$invoice->update($request->all());
return redirect()->route('invoices.index')
->with('success','Invoice updated successfully');
/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy(Invoice $invoice)
$invoice->delete();
return redirect()->route('invoices.index')
->with('success','Invoice deleted successfully');
路线:
<?php
use Illuminate\Support\Facades\Route;
Route::get('/', function()
return view('welcome');
);
Route::get('home');
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Auth::routes();
Route::get('/home', 'HomeController@index')->name('home');
Route::group(['middleware' => ['auth']], function ()
Route::get('/search', 'UserController@search');
Route::get('/search2', 'ProductController@search2');
Route::get('/search3', 'ProformController@search3');
Route::get('/search4', 'InvoiceController@search4');
Route::post('/duplicate', 'ProformController@duplicate')->name('proforms.duplicate');
Route::get('data', 'UserController@index');
Route::get('posts', 'PostController@index');
Route::get('/prodview', 'TestController@prodfunct');
Route::resource('roles', 'RoleController');
Route::resource('users', 'UserController');
Route::resource('permissions', 'PermissionController');
Route::resource('products', 'ProductController');
Route::resource('invoices', 'InvoiceController');
Route::resource('category', 'CategoryController');
Route::resource('invoices', 'InvoiceController');
Route::resource('proforms', 'ProformController');
);
查看:
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Zarządzanie fakturami</h2>
</div>
<div class="col-md-4">
<form action="/search4" method="get">
<div class="input-group">
<input type="search" name="search" class="form-control">
<span class="input-group-prepend">
<button type="submit" class="btn btn-primary">Wyszukaj</button>
</span>
</div>
</form>
</div>
<div class="pull-right">
@can('product-create')
<a class="btn btn-success" href=" route('invoices.create') "> Utwórz nową fakturę</a>
@endcan
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p> $message </p>
</div>
@endif
<table class="table table-bordered">
<tr>
<th scope="col">@sortablelink('id', 'Numer')</th>
<th scope="col">@sortablelink('invoicnumber', 'Numer faktury')</th>
<th scope="col">@sortablelink('invoicedate', 'Data wystawienia')</th>
<th scope="col">@sortablelink('user_id', 'Kontrachent')</th>
<th scope="col">@sortablelink('selldate', 'Data sprzedaży')</th>
<th scope="col">@sortablelink('paymentdate', 'Termin płatności')</th>
<th scope="col">@sortablelink('status', 'Status')</th>
<th scope="col">@sortablelink('nettotal', 'Netto razem')</th>
<th scope="col">@sortablelink('grosstotal', 'Brutto razem')</th>
<th >Akcja</th>
</tr>
@foreach ($invoices as $invoice)
<tr>
<td> ++$i </td>
<td> $invoice->invoicenumber </td>
<td> $invoice->invoicedate </td>
<td> $invoice->user->showname ?? $invoice->showname </td>
<td> $invoice->selldate </td>
<td> $invoice->paymentdate </td>
<td> $invoice->status </td>
<td> $invoice->nettotal </td>
<td> $invoice->grosstotal </td>
<td>
<form action=" route('invoices.destroy',$invoice->id) " method="POST">
<a class="btn btn-info" href=" route('invoices.show',$invoice->id) ">Więcej</a>
@can('product-edit')
<a class="btn btn-primary" href=" route('invoices.edit',$invoice->id) ">Edytuj</a>
@endcan
@csrf
@method('DELETE')
@can('product-delete')
<button type="submit" class="btn btn-danger">Usuń</button>
@endcan
</form>
</td>
</tr>
@endforeach
</table>
!! $invoices ?? ''->appends(request()->except('page'))->render() !!
<p class="text-center text-primary"><small>ARTplus 2020</small></p>
@endsection
【问题讨论】:
【参考方案1】:您必须使用$table->softDeletes();
而不是$table->time('deleted_at')->nullable();
。
softDeletes()
方法使用timestamp
而不是time
格式日期。也许它会解决你的问题;
【讨论】:
它没有帮助 @PiotrŚwitlicki 你跑php artisan migrate:refresh
了吗?请注意,它会清除数据库中的内容。以上是关于Laravel 软删除在一张表中不起作用的主要内容,如果未能解决你的问题,请参考以下文章