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'))}}
Related
I have created a form in Laravel so here are the following files:
The form that someone should submit some details,
contact.blade.php:
#extends('layouts.layout')
#section('content')
<main role="main">
<section class="jumbotron text-center">
<div class="container">
<h1 class="jumbotron-heading">Laravel demo</h1>
<p class="lead text-muted">Please fill the form</p>
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
</div>
</section>
<div class="album text-muted">
<div class="container">
{!! Form::open(['url' => 'contact/submit']) !!}
{!! csrf_field() !!}
<div class="form-group">
{{Form::label('name', 'Name') }}
{{Form::text('name', 'Enter Name', ['class'=> 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('email', 'E-Mail Address') }}
{{Form::text('email', 'example#gmail.com', ['class'=> 'form-control'])}}
</div>
<div class="form-group">
{{Form::label('message', 'Enter Message') }}
{{Form::textarea('message', 'Enter Message', ['class'=> 'form-control'])}}
</div>
<div>
{{Form::submit('Submit', ['class'=> 'btn btn-primary'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</main>
#endsection
Controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class MessageController extends Controller
{
public function submit(Request $request){
$this->validate($request, [
'name' => 'required',
'email' => 'required'
]);
return 'SUCCESS';
}
}
*
In the Routes web.php file i have included the method as post:
Route::get('/', function () {
return view('home');
});
Route::get('/contact', function () {
return view('contact');
});
Route::post('/contact/submit', 'MessageController#submit');
The error message is " RouteCollection.php (line 251)" .After searching for similar occassions here the problem ussually occurs when in the Routes you use a different method to the specified route method. I'm using POST method for submiting details, I still cannot understand why I get this.
Any help would be appreciated.
Instead of this {!! Form::open(['url' => 'contact/submit']) !!}
Try this.
{!! Form::open(['method'=>'POST','action'=>'MessageController#submit']) !!}
Add the back slash to the url of the form like so :
{!! Form::open(['url' => '/contact/submit']) !!}
I am trying to edit a record in a table. I have created a route and the form, but I can't get past this error. I have figured out the problem but I can't find a fix. Am I correct in thinking that the edit.blade.php file needs the $ad->id passing?
The $ad->id is an ID of a specific add in a List View. The list view has all the tickets displayed from a table, and the below link is meant to edit that one item.
The edit route is accessed using following code:
Edit
I have one route that is supposed to open up the edit view form:
Route::get('/ticket_ads/edit/{ad}', 'TicketAdsController#editTicketAdForm')->name('ticket.edit');
The above route points to this in the controller:
public function editTicketAdForm($id)
{
//$ad = DB::table('ticket_ads')->where('id', $id)->value('id');
return view('Ads.edit')->with('id', $id);
}
This is the view called by the above function:
#extends('Shared.Layouts.MasterWithoutMenus')
#section('title')
Edit a ticket ad
#stop
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h2>Edit your ticket ad</h2></div> <br/>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('comment', 'Comment') }}
{{ Form::text('comment', Input::old('comment'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
#endsection
This is the line that throws the error
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
The ID displays normally in the URL as ticket_ads/edit/7 for example.
How do I get past this?
Change this line:
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
to this:
{{Form::open(array('route' => array('ticket.edit', $id)))}}
This
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
is wrong. Correct syntax is:
{{ Form::open(['route' => ['ticket.edit', $id]]) }}
also you should safely ditch using array() in favor of [] syntax as Laravel requires PHP 5.4+ anyway (unless you are using ancient version of Laravel, like v4?)
The correct syntax for calling route is.
{{ route('/cardetails', ['121','cars'] ) }}
In URL it will be like this below line.
127.0.0.1:8000/cardetails/121/cars
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
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
hi i have this custom validation i declared in my start->global.php what it does is compare the password inside the database and the entered 'current password' here is the code
Validator::extend('chkcpass', function($attribute, $value, $parameters){
$getAcc = DB::table('nsa_systemusers')
->where('sid' , '=' , $parameters)
->get();
foreach ($getAcc as $key => $gcp){
$ucp = $gcp->password;
}
$sptkey = md5('3982f3a0c86f272633fc7105040a83c8');
$decPass = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, $sptkey, base64_decode($ucp), MCRYPT_MODE_ECB));
if($value != $decPass){
return false;
}else{
return true;
}
});
in my controller im calling it like this
$uid = Session::get('sid');
$rules = array(
'ecp' => 'required|min:8|max:255|chkcpass:'.$uid,
'np' => 'required',
'rnp' => 'required',
);
and in my view here is the form
{{ Form::open(array('url' => 'usrchangepassword')) }}
<div class="row">
<div class="input-field col s10">
{{ Form::label('pass', 'Enter Current Password') }}
{{ Form::password('ecp' , array('class' => 'form-control')) }}
</div>
</div>
<div class="row">
<div class="input-field col s10">
{{ Form::label('pass', 'Enter New Password') }}
{{ Form::password('np' , array('class' => 'form-control ')) }}
</div>
</div>
<div class="row">
<div class="input-field col s10">
{{ Form::label('pass', 'Re-type New Password') }}
{{ Form::password('rnp' , array('class' => 'form-control ')) }}
</div>
</div>
#if ($errors->any())
<div class='err'>
<ul>
{{ implode('', $errors->all('<strong><li class="ermsg">:message</li></strong>')) }}
</ul>
</div>
#endif
<br>
{{ Form::submit('save', array('class' => 'btn btn-primary savebtn')) }}
{{ Form::close() }}
but im getting an error saying i have an error in my view blade about
Illegal string offset 'name'
any ideas what i am doing wrong? thanks so much in advance!
$getAcc = DB::table('nsa_systemusers')
->where('sid' , '=' , $parameters)
->get();
It is failing because $parameters is an array.
If you want to use the array, then use this method:
->whereIn('sid', $parameters);