[英]laravel 5.6 not reading variable from controller in laravel
I am new to laravel so pardon me if this question seems stupid. 我是laravel的新手,如果这个问题看起来很愚蠢,请原谅我。 I am trying to do a basic CRUD app, the create and read seems to work fine but am having issues with editing, updating and deleting. 我正在尝试做一个基本的CRUD应用,创建和读取似乎工作正常,但是在编辑,更新和删除时遇到问题。 When I click the edit button it returns undefined variable log_books but to my understanding I think I have declared it. 当我单击编辑按钮时,它将返回未定义的变量log_books,但据我所知,我认为已声明了它。 Please help. 请帮忙。
controller name: logbook.php 控制器名称: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');
}
}
}
My Route: web.php 我的路线: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');
My View: home.blade.php 我的看法: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> <a href="{{ URL::to('destroy',$row->id) }}"><input type="button" class="btn btn-danger" value="Delete"></a></td>
</tr>
@endforeach
edit view: edit.blade.php 编辑视图: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>
Pardon my code if it looks messed up, the create and edit view are both in a folder called log_books. 请原谅我的代码,如果看起来很混乱,创建和编辑视图都在一个名为log_books的文件夹中。 Thanks for your help. 谢谢你的帮助。
Laravel has Route model binding, you may use this feature in show
and edit
methods as follows: Laravel具有Route模型绑定,您可以在show
和edit
方法中使用此功能,如下所示:
In Controller 在控制器中
public function edit(Log_Book $log_book)
{
return view('log_books.edit', compact($log_book));
}
In web.php 在web.php中
Route::get('/edit/{log_book}', 'logbook@edit');
Also you don't need to check auth in controller methods, use auth
middleware. 另外,您无需检查控制器方法中的auth
,请使用auth
中间件。
In Controller 在控制器中
public function __construct()
{
return $this->middleware('auth');
}
Your edit
method is passing an invalid value to compact: 您的edit
方法正在传递无效值来压缩:
return view('log_books.edit', compact($log_books))->with('id', $id);
Should most likely be the 'name' of the variable you want to compact, not the variable itself: 应该最有可能是您要压缩的变量的“名称”,而不是变量本身:
return view('log_books.edit', compact('log_books'))->with('id', $id);
To avoid these types of mistakes completely, just define the array: 为了完全避免这些类型的错误,只需定义数组即可:
return view('log_books.edit', ['log_books' => $log_books])...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.