MethodNotAllowedHttpException - php

I'm trying to update the fields in the database, but I couldn't
here is my routes :
Route::get('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
here the controller:
public function postOrder()
{
$this->orderForm->validate(Input::all());
$order = $this->orders->getNew([
'link' => Input::post('link'),
'size' => Input::post('size'),
'color' => Input::post('color')
]);
$this->orders->save($order);
return Redirect::back()->withMessage('Order has been updated');
}
here is the blade:
{{ Form::open() }}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
so each time I try to update the order I got error "MethodNotAllowedHttpException ", I tried a lot of methods but I'm lost. I'm still beginner in php and this problem drive me crazy, so I'll be glad if you can help guys.
thanks
*** I've updated the code

So you're posting to the route, /orders. Therefor you need a HTTP POST request. You're now assigning a GET request to the /orders route.
You need to change your code to:
Route::post('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
Also you need to add a CSRF Token, this can be done through adding {!! csrf_field() !!} in your blade (inside your Form open and close).
{{ Form::open() }}
{!! csrf_field() !!}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Hope this works!

You must specify the method in the Form::open method.
{{ Form::open(array('method' => 'post')) }}

Just added this in the repository:
public function updateOrder($id, array $data)
{
$orders = $this->getById($id);
if (!empty($data['title'])) {
$orders->title = $data['title'];
}
if (!empty($data['link'])) {
$orders->link = $data['link'];
}
(AND SO ON)
$orders->save();
and in the controller:
public function postOrder($id)
{
$this->orders->updateOrder($id, Input::all());
return Redirect::back()->withMessage('Updated');
}
and that's it

Related

why when editing data using modal, change all IDs?

Why are all ids are called when editing on one number
Controller
public function update(Request $request, $id)
{
$spent_time = SpentTime::find($id);
$plan = $spent_time->plan;
$total_spent_times = $plan->spent_times()->where('task_category', $spent_time->task_category)->sum('spent_time');
$request['spent_time'] = (int)$total_spent_times + (int)$request->get('daily_spent_time');
$total_percentage = $plan->spent_times()->where('task_category', $spent_time->task_category)->sum('percentage');
$request['percentage'] = (int)$total_percentage + (int)$request->get('daily_percentage');
$spent_time->update($request->all());
return redirect()->route('real.index', compact('spent_time','plan','total_spent_time','total_percentage'));
}
index.blade.php
<td>
<a href="" class="edit_real" data-toggle="modal" data-target="#edit_real-{{$spent_time->plan_id}}">
{{$spent_time->plan->user_story}}
</a>
#include('reals._form')
</td>
_form.blade.php
<div class="modal fade" id="edit_real-{{$spent_time->plan_id}}" role="dialog" >
<div class="modal-dialog modal-sm-8">
<div class="modal-content">
<div class="nav-tabs-custom">
<ul class="nav nav-tabs pull-left">
<li class="">Plan</li>
<li class="active">Real</li>
</ul>
<div class="tab-content">
<div class="chart tab-pane active" id="revenue-chart">
{!! Form::open([$spent_time, 'url' => route('real.update', $spent_time->id), 'method' => 'POST', 'role'=>'form']) !!}
{{ csrf_field() }} {{ method_field('PUT') }}
<div class="box-body">
<div class="form-group">
{!! Form::label('user_story','Today Plan *', ['class' => 'control-label', 'name'=>'user_story']) !!}</label>
{!! Form::text('user_story', old('plan_id', is_null($spent_time->plan->user_story) ? null : $spent_time->plan->user_story),
['class'=>'form-control', 'readonly' => 'true']) !!}
</div>
<div class="form-group">
{!! Form::label('daily_spent_time','Spent Time *', ['class' => 'control-label', 'name'=>'daily_spent_time']) !!}</label>
{!! Form::text('daily_spent_time', old('daily_spent_time', $spent_time->daily_spent_time ?: null),
['class'=>'form-control', 'id'=>'daily_spent_time']) !!}
</div>
<div class="form-group">
{!! Form::label('daily_percentage','% Done *', ['class' => 'control-label', 'name' => 'daily_percentage']) !!}</label>
{!! Form::text('daily_percentage', old('daily_percentage', $spent_time->daily_percentage ?: null),
['class'=>'form-control', 'id'=>'daily_percentage']) !!}
</div>
<div class="form-group">
{!! Form::label('reason','Reason', ['class' => 'control-label', 'name'=>'reason']) !!}</label>
{!! Form::textarea('reason', old('reason', $spent_time->reason ?: null), ['class'=>'form-control', 'id'=>'reason']) !!}
</div>
<div class="form-group">
{!! Form::submit('Save', ['class' => 'btn btn-block btn-primary btn-flat', 'type'=>'submit']) !!}
</div>
</div>
</div>
</div>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<script type="text/javascript">
// jQuery
$('.edit_real').on('click', function (event) {
event.preventDefault();
$('#edit_real').modal();
});
Preliminary data, when editing in number 2, data will be saved to data number 1
Why when editing data using modal, change all IDs?
What should be improved? where is the error that must be corrected? why all ids are called when editing on one number?
Answer
add {!! Form::close() !!} in _form.blade.php
Controller
public function edit($id)
{
$spent_times = SpentTime::all();
$user_story = Plan::pluck('user_story', 'id');
$spent_time = SpentTime::find($id);
return view('reals.edit', compact('spent_times', 'user_story', 'spent_time'));
}
public function update(Request $request, $id)
{
$spent_time = SpentTime::find($id);
$spent_time->update($request->all());
return redirect()->route('real.index', compact('spent_time'));
}

Invalid argument supplied for foreach() - laravel 5.6

newbie here practicing laravel, im making an inventory for movies using laravel 5.6.. where I can do basic CRUD functionalities.
In here, Im sorting movies by genre. however I came up with this error Invalid argument supplied for foreach();
here is my Route: web.php
Route::resource('movies', 'MoviesController');
Route::get('movies/year/{movie}', 'MoviesController#released');
Route::get('movies/genre/{genre}', 'MoviesController#getgenre');
here is my controller: MoviesController.php
public function getgenre($genre){
$movies = Movie::where('genre', $genre)->whereIn('status', [1])->orderBy('title', 'asc')->get();
return view('genre', compact('movies'));
}
here is my view: genre.blade.php
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-md-10">
#if($movies->count() < 1)
<h1 style="text-align: center">No Movie Found</h1>
#else
#foreach($movies as $movie)
<div class="card" style="margin-bottom: 10px;">
<div class="card-body">
<div class="card-title">
{{ ucwords($movie->title) }}
</div>
<div class="card-text">
{{ ucfirst($movie->genre) }} | {{$movie->released}} |
#if($movie->seen == 1)
Seen: Yes
#else
Seen: No
#endif
<div style="margin-top: 5px">
{{ Form::open(['route' => ['movies.edit', $movie->id], 'class' => 'formupdate', 'method' => 'GET']) }}
{{ Form::submit('Update', ['class' => 'btn btn-primary col-md-2']) }}
{{ Form::close() }}
</div>
<div style="margin-top: 5px">
{{ Form::open(['route' => ['movies.destroy', $movie->id], 'class' => 'formdelete', 'method' => 'DELETE']) }}
{{ Form::hidden('hidden', 'hidden') }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger col-md-2']) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
#endforeach
<div class="pagination" style="margin-top: 10px; text-align: center;">
{{ $movie->links() }}
</div>
#endif
</div>
<div class="col-md-2">
</div>
</div>
#stop
I tested it dd(): and it has results:
tried testing it using dd()
here is the error:
here is the error
thanks,
you have two issues:
1- Update this whereIn expects array and you use pagination in view and you do not return the data as pagination
$movies = Movie::where('genre', $genre)->whereIn('status', [1])>orderBy('title', 'asc')->paginate(10);
2- in the view you written $movie->links(); update it to
$movies->links();
Update this line, whereIn expects array
$movies = Movie::where('genre', $genre)->whereIn('status', [1])->orderBy('title', 'asc')->get();

Form model binding isn't working inside section

Parent blade template:
#extends('layouts.master')
#section('content')
<div class="panel-body">
#if(count($errors))
#include('common.errors')
#endif
{!! Form::model($entry, ['method' => $method, 'class' => 'form-horizontal', 'route' => [$route]]) !!}
#section('fields')
#foreach (['title', 'url', 'content', 'meta_tags', 'meta_description', 'is_active'] as $field)
#include('entries.fields.' . $field)
#endforeach
#show
<div class="form-group">
<div class="col-sm-offset-3 col-sm-6">
#section('submit')
{!! Form::submit('Добавить', ['class' => 'btn btn-default']) !!}
#show
</div>
</div>
{!! Form::close() !!}
</div>
#endsection
Child template:
#extends('entries.create')
#section('title')
Добавить статью / {{ config('site.site_name') }}
#endsection
#section('fields')
#foreach ([
'entries.fields.title',
'entries.fields.url',
'articles.fields.description',
'entries.fields.content',
'entries.fields.meta_description',
'entries.fields.is_active',
'articles.fields.enable_comments',
'articles.fields.show_on_main',
'articles.fields.rank',
] as $field)
#include($field)
#endforeach
#endsection
Model binding works for parent template, but doesn't work in child template.
For example, I have enable_comments checkbox:
{!! Form::label('enable_comments', 'Включить комментарии', ['class' => 'col-sm-3 control-label']) !!}
<div class="col-sm-6">
{!! Form::checkbox('enable_comments', null, null, ['class' => 'checkbox']) !!}
</div>
</div>
And it is always unchecked, while the $entry->enable_comments === true
I don't know why this is happening. Maybe anyone can go through the code and find the problem.
Thanks in advance for your help.
The problem is that form model binding works only inside the current template (with included files), but doesn't fill the values in child temlates. So the only solution is to write form start ( {!! Form::model(... )again, in the child template

Cannot Access $errors in laravel 5.2

I am having a issue with Laravel 5.2 accessing the $errors variable in my partial.
routes.php
I have wrapped my routes in the middleware web.
and in my auth.blade.php
<div class="col-md-4">
<div class="panel panel-{{ $errors->all() ? 'danger' : 'default'}}">
<div class="panel-heading">
`enter code here`<h2 class="panel-title">#yield('heading')</h2>
</div>
<div class="panel-body">
#if($errors->all())
<div class="alert alert-danger">
<strong>We found some errors</strong>
<ul>
#foreach($errors->all() as $errors)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
#yield('content')
</div>
</div>
</div>
here is the code for my login partial
#extends('layouts.auth')
#section('title','Login')
#section('heading','Welcome Please login')
#section('content')
{!! Form::open() !!}
<div class="form-group">
{!! Form::label('email') !!}
{!! Form::text('email', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('password') !!}
{!! Form::password('password', ['class' => 'form-control']) !!}
</div>
{!! Form::submit('Login', ['class' => 'btn btn-primary']) !!}
Forgot password?
{!! Form::close() !!}
#endsection
when i click login it just redirects to the same page buit not flashing the errors?
When i click login it shows this error
enter image description here
Remove web middleware, this may cause empty $errors. Since 5.2.27, web middleware aplies automatically to all routes and you shouldn't add it again manually.

Missing argument 1 for CommentsController::store()

Im making a simple blog with Laravel and I have some issues with the comments section. I'm passing the article_id trough the comments form but I get this error.
This is the Article View
<?php
$comment = new Comment;
?>
#extends('layout')
#section('showArticle')
<div class="row">
<div class="col-lg-12">
<h4>{{$idArticle->title}}</h4>
<p>{{$idArticle->text}}</p>
<div class="col-lg-4">Autor: {{$idArticle->author}}</div>
<div class="col-lg-4">Categorias</div>
</div>
</div>
<div class="comments">
<p>Comentarios</p>
</div>
<div>
{{Form::open(array('route' => array('comments.store', $idArticle->id, 'method' => 'POST')))}}
#include ('errors', array('errors' => $errors))
<div class="row">
<div class="form-group col-md-4">
{{ Form::label('comment', 'Contenido') }}
{{ Form::textarea('comment', null, array('placeholder' => 'Comment', 'class' => 'form-control')) }}
</div>
</div>
{{ Form::button('Publicar', array('type' => 'submit', 'class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
#stop
The controller
public function store($idArticle)
{
var_dump($id);
exit();
}
And my route
Route::resource('comments', 'CommentsController');
What could be wrong?
Try it
Change the Article View
{{Form::open(array('route' => array('comments.store', 'method' => 'POST')))}}
{{ Form::hidden('article_id',$idArticle->id) }}
.......
.......
Change Your Controller
public function store()
{
var_dump(Input::all());
exit();
}
It sounds like you're looking for nested resources.
Change your route to:
Route::resource('articles.comments', 'CommentsController');
This will generate urls like:
articles/{articleId}/comments
articles/{articleId}/comments/{commentId}
Now you open your form as you did before (with updated route name):
{{Form::open(array('route' => array('articles.comments.store', $idArticle->id), 'method' => 'POST'))}}

Categories