laravel 5.6无法从laravel中的控制器读取变量

时间:2018-06-28 14:59:16

标签: php laravel

我是laravel的新手,所以如果这个问题看起来很愚蠢,请原谅我。我正在尝试做一个基本的CRUD应用,创建和读取似乎工作正常,但是在编辑,更新和删除时遇到问题。当我单击编辑按钮时,它将返回未定义的变量log_books,但据我所知,我认为已声明了它。请帮忙。

控制器名称:logbook.php

namespace App\Http\Controllers;

use Request;

use Illuminate\Support\Facades\Input;

use App\Http\Requests;

use App\log_book;
use DB;
use Illuminate\Support\Facades\Auth;

class logbook extends Controller
{
/**
 * Display a listing of the resource.
 *
 * @return \Illuminate\Http\Response
 */
public function index()
{
    //
    /**
    *
    *@if(Auth::check())
    */

    $log_books = log_book::all();
    return view('home')->with('log_books',$log_books);

    /**
    *
    *@endif
    */

}

/**
 * Show the form for creating a new resource.
 *
 * @return \Illuminate\Http\Response
 */
public function create()
{
    //

/**
 *
 *   @if(Auth::check())
 */
        return view('log_books.create');
/**
 *
 *   @endif
 */
}

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function store(Request $request)
{
    //
    log_book::create(Request::all());
    return redirect('home')->with('message','name has been added successfully');
}

/**
 * Display the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function show($id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function edit($id)
{
    //
    /**
     *
     *@if(Auth::check())
     */

    $log_books = log_book::findorFail($id);
    //$logs = DB::table('log_books')->where('id', $id)->first();
    return view('log_books.edit', compact($log_books))->with('id', $id);

    /**
     *
     *@endif
     */


}

/**
 * Update the specified resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function update(Request $request, $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 *
 * @param  int  $id
 * @return \Illuminate\Http\Response
 */
public function destroy($id)
{
    //
    $log_books = log_books::findorFail($id);

    $destroy = $log_books->delete();

    if($destroy) {
        return redirect('home', compact($log_books))->with('message', 'Record has been deleted');
    }
}
}

我的路线:web.php

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

Route::get('create', function () {
return view('create');
});

Auth::routes();

Route::get('/home', 'HomeController@index')->name('home');
Route::get('/create', 'logbook@create')->name('create');
Route::post('store', 'logbook@store');
Route::get('/home', 'logbook@index');
Route::get('edit/{id}', 'logbook@edit');
Route::delete('destroy/{id}', 'logbook@destroy');

我的观点:home.blade.php

 @foreach($log_books as $row)
                                    <tr>
                                        <td>{{$row->name}}</td>
                                        <td>{{$row->present_date}}</td>
                                        <td>{{$row->time_in}}</td>
                                        <td>{{$row->time_out}}</td>
                                        <td><a href="{{ URL::to('edit',$row->id) }}"><input type="button" class="btn btn-success" value="Edit"></a>&nbsp;<a href="{{ URL::to('destroy',$row->id) }}"><input type="button" class="btn btn-danger" value="Delete"></a></td>
                                    </tr>
 @endforeach

编辑视图:edit.blade.php

<form method="post" action="edit">
                    {{csrf_field()}}

                    <input type="hidden" name="_method" value="PUT">
                    <input required="yes" type="text" placeholder="Full name" class="form-control" name="name" value="{{$log_books->name}}"><br>
                    <input required="yes" onfocus="this.type= 'date'" onblur="this.type='text'" placeholder="Date" class="form-control" name="present_date" value="{{$log_books->present_date}}"><br>
                    <input required="yes" onfocus="this.type= 'time'" onblur="this.type='text'" placeholder="Time in" class="form-control" name="time_in" value="{{$log_books->time_in}}"><br>
                    <input onfocus="this.type= 'time'" onblur="this.type='text'" placeholder="Time out" class="form-control" name="time_out" value="{{$log_books->time_out}}"><br>

                    <input type="submit" class="btn btn-primary" value="Submit" name="submit">
</form>

请原谅我的代码,如果看起来混乱了,创建和编辑视图都在一个名为log_books的文件夹中。感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

Laravel具有Route模型绑定,您可以在showedit方法中使用此功能,如下所示:

在控制器中

public function edit(Log_Book $log_book)
{
    return view('log_books.edit', compact($log_book));
}

在web.php

Route::get('/edit/{log_book}', 'logbook@edit');

此外,您无需在控制器方法中检查身份验证,请使用auth中间件。

在控制器中

public function __construct()
{
    return $this->middleware('auth');
}

答案 1 :(得分:1)

您的edit方法正在将无效值传递给压缩:

return view('log_books.edit', compact($log_books))->with('id', $id);

最有可能是您要压缩的变量的“名称”,而不是变量本身:

return view('log_books.edit', compact('log_books'))->with('id', $id);

要完全避免这些类型的错误,只需定义数组:

return view('log_books.edit', ['log_books' => $log_books])...