Laravel5.2 delete doesn't work - laravel-5

I developed website with CRUD on products table .this is the structure of the table.
Create and update works fine But delete not work.
This is the form in blade to delete product
{{ Form::open(array('url' => 'admin/products/' . $product->id, 'class' => 'pull-right')) }}
{{ Form::hidden('_method', 'DELETE') }}
{{ Form::submit('Delete ', array('class' => 'btn btn-warning')) }}
{{ Form::close() }}
And this the destroy function in controller
public function destroy($id)
{
$product = Product::find($id);
$product->delete();
// Product::destroy($id);
return redirect('admin/products')->with('message', 'Successfully deleted the product!');
}
And This is my routes
Route::group(['middleware' =>'App\Http\Middleware\AdminMiddleware'], function () {
//resource
Route::resource('admin/products','AdminFront');
});
When I click delete button it enter the destroy function and dd($id) correct
But when write
$product = Product::find($id);
$product->delete();
Or
Product::destroy($id);
I get this error
The localhost page isn’t working
localhost is currently unable to handle this request.
This error tired me . I developed delete fun with resource API in another table and work fine.I don't know are the problem in the db or where. please any one help me ,

What does your routes.php look like?
You may need to include the resource route in routes.php.
Route::resource('admin/products/', 'TheNameOfYourController');
But make sure the route is protected either in the controller or routes.php.
Here is somewhat the same setup you have:
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/routes.php LINE 119
https://github.com/jeremykenedy/laravel-material-design/blob/master/app/Http/Controllers/UsersManagementController.php LINES 369-376
https://github.com/jeremykenedy/laravel-material-design/blob/master/resources/views/admin/edit-user.blade.php LINES 243-246
Cheers!

Related

Laravel 5.5 - Missing required parameters for [Route:]

What I'm trying to achieve is very simple, but I'm just making this a lot harder than it needs to be. So am seeking help for what is apparently, my lack of Laravel experience.
All I want to do is have a form that can update database entries via a text input. This has to be dynamic as it's being used for a few databases and I don't want to have multiple files for them.
Sorry in advance for the probable messy/crap code...
Here's the routes I have:
Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#updateGreeting')->name('greeting.update');
The PlayerProfileController
// Display greeting message
public function greeting($server, $playerID) {
$greetInfo = DB::connection($server)
->table('clients')
->where('id', $playerID)
->first();
return view('servers.greeting')
->with('greetInfo', $greetInfo);
}
// Update greeting message
public function updateGreeting(Request $request, $server, $playerID) {
$gUpdate = DB::connection($server)
->table('clients')
->where('id', $playerID)
->update(['greeting' => $request->input('greet')]);
return back()->with('success', 'Greeting updated successfully!');
}
And finally the form
{{ Form::open(['action' => ['PlayerProfileController#updateGreeting', $greetInfo->greeting], 'method' => 'POST']) }}
{{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
{{ Form::hidden('_method', 'PUT') }}
<br>
{{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}
Any and all help is appreciated. Thank you in advace.
The ErrorException you're seeing is caused by the missing argument for the route you're trying to call. Your route is expecting server and playerId, wheres you're only sending it $greetInfo->greeting.
As you're using named route, my suggestion would be to use route() helper like so:
Route::get('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#greeting');
Route::post('/server/{server}/players/{playerID}/greeting', 'PlayerProfileController#updateGreeting')
->name('greeting.update');
class PlayerProfileController extends Controller {
public function greeting($server, $playerID) {
$greetInfo = DB::connection($server)
->table('clients')
->where('id', $playerID)
->first();
return view('servers.greeting')
->with('greetInfo', $greetInfo);
}
// Update greeting message
public function updateGreeting(Request $request, $server, $playerID) {
$gUpdate = DB::connection($server)
->table('clients')
->where('id', $playerID)
->update(['greeting' => $request->input('greet')]);
return back()->with('success', 'Greeting updated successfully!');
}
}
{{ Form::open(['action' => route('greeting.update', [$server, $playerId]), 'method' => 'post']) }}
{{ Form::bsText('greet', '', ['placeholder' => 'Update greeting or leave blank to remove current message']) }}
{{ Form::bsSubmit('Update',['class' => 'btn btn-outline-secondary']) }}
{!! Form::close() !!}
Please replace $server and $playerId variables with the ones representing the arguments you want to send with the request.
You can see I've also removed {{ Form::hidden('_method', 'PUT') }}, as this was trying to overwrite the post method on the Form::open method.

Laravel undefine variable in view

I'm new to laravel. Using version 5.4 and tried to search but don't see what I'm doing wrong. I keep getting an "Undefined variable: post" in my view. I'm also doing form model binding. Model binding works properly when manually entering URL. Just can't click on link to bring up edit view.
My routes:
Route::get('test/{id}/edit','TestController#edit');
My controller:
public function edit($id)
{
$post = Post::find($id);
if(!$post)
abort(404);
return view('test/edit')->with('test', $post);
}
My form:
{{ Form::model($post, array('route' => array('test.update', $post->id), 'files' => true, 'method' => 'PUT')) }}
You're assigning the post value to 'test', so should be accessible with $test rather than $post.
You probably want to do either of these two things instead:
return view('test/edit')->with('post', $post);
or
return view('test/edit', ['post' => $post]);
https://laravel.com/docs/5.4/views
Your controller is sending a variable named "test", but your error says that your blade file doesn't have the $post variable passed into it. This can be fixed by changing "test" to "post" in your controller.

Missing required parameters for in a Update Form Laravel 5.2

I've been working on a webapp recently in laravel and i wanted to have a eddit function within tthe application. but im getting this error Missing required parameters for [Route: producten.update] [URI: producten/{producten}], and i dont know what i've done wrong.
This is the Routes im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'update', 'delete', 'edit', 'destroy', 'create']]);
This is the controller function im using for showing the edit page and updating.
The Edit function
public function edit(Request $request, Product $product)
{
// $product = Product::FindorFail($id);
// Product is a table with all products, with sellprice and buy price
// fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
The Update Function:
public function update(Request $request, Product $product)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
and this is the view i use for it.
{{Form::model($model, ['method' => 'PATCH', 'action' => 'ProductenController#update', $model ]) }}
{{ Form::label('naam:')}}
{{ Form::text('naam') }} <br>
{{ Form::label('inkoopPrijs:')}}
{{ Form::text('inkoopPrijs') }} <br>
{{ Form::label('verkoopPrijs:') }}
{{ Form::text('verkoopPrijs') }} <br>
{{Form::label('Fabrieken', 'Fabrieken Id:') }}
{{ Form::select('Fabrieken_Id', $fabrieks)}} <br>
{{ Form::submit('edit')}}
{{ Form::close() }}
if there is anything else that i need to add to the question just let me know and i'll add it
Missing thing is the id you are not getting id there in your edit function
your edit function should as i am assuming that you are just showing the form from this method where user can edit
public function edit($id)
{
$product = Product::FindorFail($id);
//Product is a table with all products, with sellprice and buy price
//fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
your update method should seem like this
public function update(Request $request, $id)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
your routes should like this no need for only
Route::resource('/producten', 'productionController');
edit route will be as
<a href="{{ route('production.edit', $model->id) }}">
Try this hope it will help

Laravel explain me how working query

I have a problem with query on laravel.
Please show me how it work, because I can't understand doc.
For example, I have VideoController.php and I have some data from forms:
$gall = array(
'name' => Input::get('name'),
'user_id' => Auth::id()
);
now I want to add this data to DB, but I don't know how to call to create function in model (and how this function should look).
And please explain me, how I should select data from database and display it on view, where for example user_id = 15;
In your VideoController.php file you should have a method to store the data, which you should post your data to through a form or something.
In a view, this form should look something like this:
{{ Form::open(['route' => 'video.store']) }} <!-- check your routes to see if video.store exists, you'll get an error otherwise -->
... form elements ...
{{ Form::submit('Submit') }}
{{ Form::close() }}
In your store() function you should have code similar to this
$video = new Video(); // if your video model is Video.php
$video->name = Input::get('name');
$video->user_id = Auth::id();
$video->save();
And if you want to display data, i.e. your video index or something, in your VideoController.php file, in the index() function:
$videos = Video::all();
return View::make('video.index', array('videos' => $videos));
then in your view
#foreach($videos as $video)
{{ $video->name }}
#endforeach

Laravel 4 - Route not defined

I've created my route:
Route::get('fichas/orden/{id}', 'FichaController#orden');
My Controller:
public function orden($id)
{
return $id;
}
And I have this in my View:
{{ link_to_route('fichas.orden', 'Guardar orden', array($ficha->id), array('class' => 'btn btn-primary')) }}
And I don't know why I'm having this error. I have been using this kind of routes and views all the time and I haven't got any problem until now. Does anybody know why it doesn't work now?
If you want to link to a named route, you have to define the route as follows:
Route::get('fichas/orden/{id}', array('as'=>'fichas.orden', 'uses'=>'FichaController#orden'));

Resources