Form model binding isn't working inside section - php

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

Related

MethodNotAllowedHttpException

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

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.

Laravel MethodNotAllowedHttpException is being thrown

I am watching a Laracast on forms, and my code is quite basic, and yet so is my knowledge of the framework. My code is below, and what your looking at, if you haven't taken a guess, is that I wish to create a list of values to be stored in a database, and to "Redirect" the user to there newly stored values. The "store" part seems to be the area of issue. Whenever I click "add food items" I am not redirected, yet this error appears, MethodNotAllowedHttpException.
Route::get('food/create', 'FoodController#create');
Route::post('fond/post', 'FoodController#store');
Route::get('food/{id}', 'FoodController#index');
namespace App\Http\Controllers;
use App\Food;
use Illuminate\Http\Request;
class FoodController extends Controller
{
public function index($id)
{
$food = Food::find($id);
return view('index')->with('food', $food);
}
public function create()
{
return view('vendor.create');
}
public function store(Request $request)
{
$input = Request::all();
$food = Food::create($input);
return redirect('food/'.$food->id);
}
}
#extends('app')
#section('body')
<h1>Foods!</h1>
<h2>{{ $food->vegetables }}</h2>
<h2>{{ $food->fruit }}</h2>
<h2>{{ $food->grains }}</h2>
<h2>{{ $food->meat }}</h2>
<h2>{{ $food->sugar }}</h2>
#stop('body')
#extends('app')
#section('body')
{!! Form::open(['url'=>'food/store']) !!}
<div class="form-group">
{!! Form::label('vegetables', 'Vegetable item:') !!}
{!! Form::text('vegetables', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('fruit', 'Fruit item:') !!}
{!! Form::text('fruit', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('meat', 'Meat item:') !!}
{!! Form::text('meat', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('grains', 'Grain item:') !!}
{!! Form::text('grains', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('sugar', 'Sugar item:') !!}
{!! Form::text('sugar', null, ['class' => 'form-control']) !!}
</div>
<div>
{!! Form::submit('Add Food items', ['class' => 'btn btn-primary form-control']) !!}
{!! Form::close() !!}
</div>
#stop('body')
Add the get method in you form opening tag. It should look like this:
{!! Form::open(['url'=>'food/store','method'=>'get']) !!}

Laravel 5 form model binding with hasMany

I have two models: Category and CategoryValue.
Inside my Category model I have:
public function values()
{
return $this->hasMany('App\CategoryValue');
}
Inside my CategoryValue I have:
function category()
{
return $this->belongsTo('App\Category');
}
In my edit view, I have my form model binding like so:
{!! Form::model($category, ['route' => ['category.update', $category->slug], 'method' => 'PATCH']) !!}
#include('partials.categoryForm')
{!! Form::close() !!}
Where $category is a chosen Category Model.
The categoryForm partial looks like:
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }}">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', null, ['class' => 'form-control']) !!}
{!! $errors->first('name', '<span class="help-block">:message</span>') !!}
</div>
<div class="form-group {{ $errors->has('slug') ? 'has-error' : '' }}">
{!! Form::label('slug', 'Slug') !!}
{!! Form::text('slug', null, ['class' => 'form-control']) !!}
{!! $errors->first('slug', '<span class="help-block">:message</span>') !!}
</div>
<div class="form-group">
{!! Form::submit('Save Page', ['class' => 'btn btn-primary']) !!}
</div>
That's all working, but when editing the Category, I'd like to be able to edit (add/remove) associate CategoryValues. To start, I want the items returned from $category->values to be inside a textarea with each CategoryValue on a new line.
I can think of a couple of ways to do this: use a #foreach and generate the textarea, or popular a variable and use that in the binding. I'm not sure which of these, if either, is the "Laravel" way to do it. I'm also not sure if either of those can be used to update the records in the DB properly. How should this be done?

Angular remove values from Laravel form

I have a angular module named shop and controller named CartCtrl:
angular.module('shop').controller('CartCtrl', function($scope, $http, ngCart) {
$scope.order_name = null;
$scope.order_phone = null;
});
In template i have:
<div class="modal-body" ng-module="shop" ng-controller="CartCtrl">
{!! Form::model(Auth::user()->profile, [ 'route' => 'order.send'], [ 'id'=>'order_form']) !!}
<div class="form-group">
{!! Form::text('name', old('name'), ['class'=>'form-control', 'placeholder'=>'Ваше имя', 'ng-model'=>'order_name']) !!}
</div>
<div class="form-group">
{!! Form::text('phone', old('phone'), ['class'=>'form-control', 'placeholder'=>'Ваш телефон', 'ng-model'=>'order_phone']) !!}
</div>
{!! Form::close() !!}
</div>
But when I go to the page and look at the form, then it is empty. But if I take away: ng-module="shop" ng-controller="CartCtrl" all will be ok. Why is that?
UPD:
If i remove ng-model it will be the same, values in inputs has will be empty

Categories