Laravel 5.6 不从 Laravel 中的控制器读取变量



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

控制器名称:日志.php

namespace AppHttpControllers;
use Request;
use IlluminateSupportFacadesInput;
use AppHttpRequests;
use Applog_book;
use DB;
use IlluminateSupportFacadesAuth;
class logbook extends Controller
{
/**
* Display a listing of the resource.
*
* @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
public function create()
{
//
/**
*
*   @if(Auth::check())
*/
return view('log_books.create');
/**
*
*   @endif
*/
}
/**
* Store a newly created resource in storage.
*
* @param  IlluminateHttpRequest  $request
* @return IlluminateHttpResponse
*/
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 IlluminateHttpResponse
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
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  IlluminateHttpRequest  $request
* @param  int  $id
* @return IlluminateHttpResponse
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param  int  $id
* @return IlluminateHttpResponse
*/
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');
}
}
}

我的路线:网络.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');

我的观点:首页.刀片.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

编辑视图:编辑刀片.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 的文件夹中。感谢您的帮助。

您的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])...

Laravel具有路由模型绑定,您可以在show中使用此功能,并edit方法,如下所示:

在控制器中

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

在网络中.php

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

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

在控制器中

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

最新更新