Laravel中的CRUD错误

时间:2017-09-07 19:06:23

标签: php laravel crud

大家好我在Laravel项目中做了一个CRUD(只是创建和阅读),创建工作正常,我在show函数中传递了“贷款”,但在视图中它显示了未定义的变量:

我在控制器中的show函数:

public function show($id)
    {
        $loan=loan::find($id);

        return view('finance/loans')->with('loan',$loan); 
    }

我的路线:

Auth::routes();
Route::get('/loans', 'LoanController@viewLoanForm');
Route::post('/loans', 'LoanController@store');

Route::get('/home', 'HomeController@index')->name('home');

Route::resource('loan','loanController');
Route::get('finance/loans', function () {
    return view('loans');
});

以下是我在视图中调用可变贷款的地方:

@extends('layouts.app')

@section('content')
<link href="{{ asset('css/loans.css') }}" rel="stylesheet">


<html>

        <div class="container">
...

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

                <div class="row">
                <h1>{{$loan->nume}}</h1>//"nume"is the row from db i want to show

                </div>


            </div>

    </html>
    @endsection

The ERROR

4 个答案:

答案 0 :(得分:3)

你拼错了路线! 您的请求未达到控制器方法

Route::get('finance/loans', function () {
    return view('loans');
 }); 

应该如此

Route::get('finance/loans/{$id}','loanController@show');

或者应该使用控制器资源路径

Route::resource('loans','loanController');

url&#39; loan / show / {$ loan_id}&#39;

<a href="route('loans.show',[$id])">Show Loan</a>

答案 1 :(得分:1)

你的回报应该是这样的:

return view('finance/loans')->with(['loan'=>$loan]);

而不是

return view('finance/loans')->with('loan',$loan);

答案 2 :(得分:0)

使用此:

return view('finance.loans',compact(['loan']));

答案 3 :(得分:0)

我认为你需要在route :: resource('finance / loans')而不是''loan')。 并且肯定会工作