Reputation: 39
I am new to laravel so pardon me if this question seems stupid. 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. When I click the edit button it returns undefined variable log_books but to my understanding I think I have declared it. Please help.
controller name: 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
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
@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
<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. Thanks for your help.
Upvotes: 1
Views: 1120
Reputation: 2960
Laravel has Route model binding, you may use this feature in show
and edit
methods as follows:
In Controller
public function edit(Log_Book $log_book)
{
return view('log_books.edit', compact($log_book));
}
In web.php
Route::get('/edit/{log_book}', 'logbook@edit');
Also you don't need to check auth in controller methods, use auth
middleware.
In Controller
public function __construct()
{
return $this->middleware('auth');
}
Upvotes: 1
Reputation: 50491
Your edit
method is passing an invalid value to compact:
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])...
Upvotes: 1