创建从数据库中删除数据的函数的困难
Difficulties with creating a function to delete data from the database
我在table中创建了一个按钮来实现删除站点数据的功能,我按照视频教程做了所有的事情,然后刷新页面后出现错误:
未定义变量:值(视图:/home/vagrant/code/bacon/resources/views/business.blade.php)
(我还是附上错误截图)https://i.stack.imgur.com/4nLEX.png
我的控制器“BusinessController.php”:
<?php
namespace App\Http\Controllers;
use \App\Models\Business;
use Illuminate\Http\Request;
class BusinessController extends Controller
{
public function index()
{
$business = \App\Models\Business::all();
return view('business', compact('business'));
}
public function createbusiness()
{
return view('/createbusiness');
}
public function create()
{
return view('business.create');
}
function delete($idd)
{
DB::table('business')->where('idd',$idd)->delete();
return redirect ('/business');
}
public function store()
{
$business = new Business();
$business->idd = request('idd');
$business->name = request('name');
$business->mail = request('mail');
$business->website = request('website');
$business->save();
return redirect('/business');
}
}
我的路线“web.php”:
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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!
|
*/
Route::get('/welcome', 'App\Http\Controllers\MainController@welcome');
Route::get('/users', 'App\Http\Controllers\MainController@users');
Route::get('/business', 'App\Http\Controllers\BusinessController@index');
Route::post('/business', 'App\Http\Controllers\BusinessController@store');
Route::get('/projects', 'App\Http\Controllers\ProjectsController@index');
Route::post('/projects', 'App\Http\Controllers\ProjectsController@store');
Route::get('/projects/create', 'App\Http\Controllers\ProjectsController@create');
Route::get('/business/create', 'App\Http\Controllers\BusinessController@createbusiness');
Auth::routes();
Route::get('/delete/{idd}','App\Http\Controllers\BusinessController@delete');
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
我的观点“业务。blade.php”:
@extends('layouts.layout')
@section('title')Бізнес@endsection
@section ('main_content')
<h1>Бизнес</h1>
<p>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Mail</th>
<th scope="col">Website</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
@foreach ($business as $singleBusiness)
<tr>
<th scope="row">{{ $singleBusiness->idd}}</th>
<td>{{ $singleBusiness->name}}</td>
<td>{{ $singleBusiness->mail}}</td>
<td>{{ $singleBusiness->website}}</td>
<td><a href="/delete/{{ $value->idd }}">
<button class="btn btn-danger btn-delete">Delete</button></a></td>
</tr>
@endforeach
</tbody>
</table>
<a class="col-4 btn btn-outline-warning mr-3" href="/business/create">Додати Бізнес</a>
</p>
@endsection
只需将 <td><a href="/delete/{{ $value->idd }}">
更改为 <td><a href="/delete/{{ $singleBusiness->idd }}">
将路由方法 Get 方法更改为 Post 方法!
`路线::post('/delete/{idd}','App\Http\Controllers\BusinessController@delete')->名称('Delete');
<Form action="{{ route( 'Delete', $singleBusiness->id) }}" method="POST" >
@csrf
<button type="submit" class="btn btn-danger btn-delete">Delete</button>
</form>
我在table中创建了一个按钮来实现删除站点数据的功能,我按照视频教程做了所有的事情,然后刷新页面后出现错误:
未定义变量:值(视图:/home/vagrant/code/bacon/resources/views/business.blade.php)
(我还是附上错误截图)https://i.stack.imgur.com/4nLEX.png
我的控制器“BusinessController.php”:
<?php
namespace App\Http\Controllers;
use \App\Models\Business;
use Illuminate\Http\Request;
class BusinessController extends Controller
{
public function index()
{
$business = \App\Models\Business::all();
return view('business', compact('business'));
}
public function createbusiness()
{
return view('/createbusiness');
}
public function create()
{
return view('business.create');
}
function delete($idd)
{
DB::table('business')->where('idd',$idd)->delete();
return redirect ('/business');
}
public function store()
{
$business = new Business();
$business->idd = request('idd');
$business->name = request('name');
$business->mail = request('mail');
$business->website = request('website');
$business->save();
return redirect('/business');
}
}
我的路线“web.php”:
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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!
|
*/
Route::get('/welcome', 'App\Http\Controllers\MainController@welcome');
Route::get('/users', 'App\Http\Controllers\MainController@users');
Route::get('/business', 'App\Http\Controllers\BusinessController@index');
Route::post('/business', 'App\Http\Controllers\BusinessController@store');
Route::get('/projects', 'App\Http\Controllers\ProjectsController@index');
Route::post('/projects', 'App\Http\Controllers\ProjectsController@store');
Route::get('/projects/create', 'App\Http\Controllers\ProjectsController@create');
Route::get('/business/create', 'App\Http\Controllers\BusinessController@createbusiness');
Auth::routes();
Route::get('/delete/{idd}','App\Http\Controllers\BusinessController@delete');
Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');
我的观点“业务。blade.php”:
@extends('layouts.layout')
@section('title')Бізнес@endsection
@section ('main_content')
<h1>Бизнес</h1>
<p>
<table class="table table-dark">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Name</th>
<th scope="col">Mail</th>
<th scope="col">Website</th>
<th scope="col">Edit</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
@foreach ($business as $singleBusiness)
<tr>
<th scope="row">{{ $singleBusiness->idd}}</th>
<td>{{ $singleBusiness->name}}</td>
<td>{{ $singleBusiness->mail}}</td>
<td>{{ $singleBusiness->website}}</td>
<td><a href="/delete/{{ $value->idd }}">
<button class="btn btn-danger btn-delete">Delete</button></a></td>
</tr>
@endforeach
</tbody>
</table>
<a class="col-4 btn btn-outline-warning mr-3" href="/business/create">Додати Бізнес</a>
</p>
@endsection
只需将 <td><a href="/delete/{{ $value->idd }}">
更改为 <td><a href="/delete/{{ $singleBusiness->idd }}">
将路由方法 Get 方法更改为 Post 方法!
`路线::post('/delete/{idd}','App\Http\Controllers\BusinessController@delete')->名称('Delete');
<Form action="{{ route( 'Delete', $singleBusiness->id) }}" method="POST" >
@csrf
<button type="submit" class="btn btn-danger btn-delete">Delete</button>
</form>