-1

テーブルからデータを変更する機能を実装するページを作成しました。ビデオ チュートリアルですべての操作を行った後、このページが機能しません。エラーのテキストは次のとおりです。

Undefined variable: business

(エラーのスクリーンショットを添付しました) https://i.stack.imgur.com/vs7Wj.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 edit_function($idd)
    {
        \DB::select('select * from business where id = ?', [$idd]);
        return view ('/update',['business'=>$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('click_edit/{idd}','App\Http\Controllers\BusinessController@edit_function');
    Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home');

私の見解「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="/click_edit/{{ $singleBusiness->idd }}">
                     <button class="btn btn-success">Edit</button></a></td>
                    <td><a href="/delete/{{ $singleBusiness->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

更新データ関数「update.blade.php」の私の見解:

@extends('layouts.layout')
@section('title')Головна сторінка@endsection
@section ('main_content')
 <h1>Update info</h1> 
<div class="container">
<div class="jumbotron">

 <form action="/update/{{ $business[0]->idd }}" method="post">
    {{ csrf_field() }}
        <div class="form-group">
            <label>Business Name</label>
            <input type="text" class="form-control" name="name" id="name" value="{{$business[0]->name}}" aria-describedby="emailHelp" placeholder="Enter new Business Name">
        </div>
        <div class="form-group">
            <label>Business Name</label>
            <input type="text" class="form-control" name="name" id="name" value="{{$business[0]->mail}}" aria-describedby="emailHelp" placeholder="Enter new Business Name">
        </div>
        <div class="form-group">
            <label>Business Name</label>
            <input type="text" class="form-control" name="name" id="name" value="{{$business[0]->website}}" aria-describedby="emailHelp" placeholder="Enter new Business Name">
        </div>
        <button type="submit" name="sumbit" class="btn btn-danger" style="width: 50%;">Update Data</button>

@endsection
4

1 に答える 1

-1

@El_Vanja が既に書いたように、エラーはあなたのedit_function. 変数$businessは定義されていませんが、使用されています。

public function edit_function($idd)
{
    \DB::select('select * from business where id = ?', [$idd]);
    
    // Here you use $business, but never declared it before
    return view ('/update',['business'=>$business]);
}

できることは、の戻り値をDB::select変数に渡すことです$business

$business = \DB::select('select * from business where id = ?', [$idd]);
于 2021-02-05T21:39:46.810 に答える