编辑页面不起作用(未定义的变量分支)在Laravel中

时间:2018-03-28 04:52:34

标签: php laravel laravel-5.5 laravel-5.6

我有编辑页面包含3个字段公司,经销商,分支,当我点击其中一个分支的保存列表时我需要向页面显示详细信息但是当我这样做时它显示未定义的变量错误抛出,如何要解决此问题,还需要在列出相应字段中的详细信息后更新表单

编辑页面

 @include('theme.header')
<?php
use App\Company;
?>
<div class="page-content-wrapper ">

    <div class="container-fluid">

        <div class="row">
            <div class="col-sm-12">
                <div class="page-title-box">
                    <div class="btn-group float-right">

                    </div>
                    <h4 class="page-title">Branch Management</h4>
                </div>
            </div>
        </div>
        <!-- end page title end breadcrumb -->

        <div class="row">
            <div class="col-12">
                <div class="card m-b-30">
                    <div class="card-body">

                        <h4 class="mt-0 header-title">Branch</h4>
                        <br>
                        <br>
                        {!! Form::open(['method' => 'PUT', 'route' => ['branchs.update',$branch->id]] ) !!}
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Company</label>
                                <div class="col-sm-10">
                                    <?php
                                    $comp=Company::where('comp_id',$branch->comp_id)->first();

                                    $companies=Company::where('status','0')
                                        ->get();

                                    ?>
                                    <input type="hidden" name="br_id" id="br_id" value="{{$branch->br_id}}">
                                    <select class="form-control" id="company" name="company">
                                        <option selected value="{{$branch->br_id}}">{{$comp->name}}</option>
                                        @foreach($companies as $company)
                                            <option value="{{$company->comp_id}}">{{$company->name}}</option>
                                        @endforeach
                                    </select>
                                </div>
                            </div>
                            <div class="form-group row">
                                <label class="col-sm-2 col-form-label">Dealership</label>
                                <div class="col-sm-10">
                                    <?php
                                    $cn = App\Dealership::where('dlr_id', $branch->dlr_id)->first();

                                    $companies =App\Dealership::where('status', '0')
                                        ->get();

                                    ?>
                                    <select class="form-control" id="dealer" name=" dealer">
                                        <option>Select Dealership</option>
                                        @foreach($dealership as $dealerships)

                                            <option value="{{$dealerships->dlr_id}}">{{$dealerships->name}}</option>
                                        @endforeach

                                    </select>
                                </div>
                            </div>

                            <div class="form-group row">
                                <label for="example-text-input" class="col-sm-2 col-form-label">Branch Name</label>
                                <div class="col-sm-10">
                                    <input class="form-control" type="text" id="branch" name="branch" value="{{$branch->name}}">
                                </div>
                            </div>


                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="page-title-box">
                                        <div class="btn-group float-right">
                                            <button class="btn btn-primary" type="submit">Button</button>
                                        </div>
                                    </div>
                                </div>
                            </div>

                        </form>


                    </div>
                </div>
            </div> <!-- end col -->
        </div> <!-- end row -->
    </div>
</div>
@include('theme.footer')

控制器文件

<?php

namespace App\Http\Controllers;

use App\Branch;
use App\Company;
use App\Dealership;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BranchController extends Controller
{
        public function index()
    {
        $companies = Company::where('status', '0')->get();

        $dealership = Dealership::where('status', '0')->get();

        $branches = Branch::where('status', '0')->get();

    return view('branch.index', compact('branches'));

    }



    public function store(Request $request)
    {
        $branch_id = new Branch;
        $branch_id = Branch::orderBy('br_id', 'desc')->take(1)->get();
        if (count($branch_id) > 0) {
            $id = $branch_id[0]->br_id;
            $id = $id + 1;
        } else {
            $id = 1;
        }
        $branch = new Branch;
        $branch->br_id = $id;
        $branch->name = $request->input('branch');
        $branch->dlr_id = $request->input('dealer');
        $branch->comp_id = $request->input('company');
        $branch->created_id = '0';
        $branch->save();
        return redirect()->back()->with('message', 'Successfully saved');


    }



    public function edit(Branch $branch)
    {

        $branch=Branch::where('id',$branch->id)->first();
        return view('branch.edit',['branches'=>$branch]);
    }


    public function update(Request $request,$id)
    {
        $branch = Branch::findOrFail($id);
        $branch->status = '1';
        $branch->save();
        if ($branch) {
            $branchs = new Branch();
            $branchs->comp_id = $request->input('company');
            $branchs->dlr_id = $request->input('dealer');
            $branchs->name = $request->input('branch');

            $branchs->created_id = '0';

            $branchs->save();
            if ($branchs) {

                return redirect('/branch')->with('message', 'Successfully saved');
            }
        }
    }



    public function destroy(Branch $branch)
    {
        DB::table('branches')
            ->where('id', $branch->id)
            ->update(['status' => '-1']);
        return back()->with('message', 'Successfully Deleted');
    }


}

3 个答案:

答案 0 :(得分:1)

您正在传递branch参数名branches。将您的控制器代码更改为:

 public function edit(Branch $branch)
{
    $branch=Branch::where('id',$branch->id)->first();
    return view('branch.edit',['branch'=>$branch]);
}

答案 1 :(得分:1)

您从控制器发送'branches'并在视图中使用$branch!尝试在edit()函数中更改它,如:

public function edit(Branch $branch)
{
   $branch=Branch::where('id',$branch->id)->first();
   return view('branch.edit',['branch' => $branch]);
}

此外,在index()功能更改

return view('branch.index', compact('branches')); 

return view('branch.index', compact(['branches', 'companies', 'dealership']));

希望这会对你有帮助!

答案 2 :(得分:0)

一切似乎都是正确的,只是你发送的是分支而不是分支,当视图查找分支时它不可用..这就是你得到的错误