Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
132 views
in Technique[技术] by (71.8m points)

php - Difficulties with creating a function to delete data from the database

I created a button in the table to implement the function of deleting data on the site, I did everything according to the video tutorial and after I did everything refreshing the page I got an error:

Undefined variable: value (View: /home/vagrant/code/bacon/resources/views/business.blade.php)

(I still attached a screenshot of the error) https://i.stack.imgur.com/4nLEX.png

My Controller "BusinessController.php":

<?php

namespace AppHttpControllers;
    
use AppModelsBusiness;
use IlluminateHttpRequest;

class BusinessController extends Controller
{
    public function index()
    {
        $business = AppModelsBusiness::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');
    }
}

My route "web.php":

<?php

    use AppHttpControllersUserController;
    use IlluminateSupportFacadesRoute;

    /*
    |--------------------------------------------------------------------------
    | 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', 'AppHttpControllersMainController@welcome');
    Route::get('/users', 'AppHttpControllersMainController@users');
    Route::get('/business', 'AppHttpControllersBusinessController@index');
    Route::post('/business', 'AppHttpControllersBusinessController@store');
    Route::get('/projects', 'AppHttpControllersProjectsController@index');
    Route::post('/projects', 'AppHttpControllersProjectsController@store');
    Route::get('/projects/create', 'AppHttpControllersProjectsController@create');
    Route::get('/business/create', 'AppHttpControllersBusinessController@createbusiness');
    Auth::routes();
    Route::get('/delete/{idd}','AppHttpControllersBusinessController@delete');
    Route::get('/home', [AppHttpControllersHomeController::class, 'index'])->name('home');

My view "business.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
question from:https://stackoverflow.com/questions/66064283/difficulties-with-creating-a-function-to-delete-data-from-the-database

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just change <td><a href="/delete/{{ $value->idd }}"> to <td><a href="/delete/{{ $singleBusiness->idd }}">


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...