I have problem with my creating offers form.
Page displays nicely, but when you add data and after sending form, page gives error.
Laravel 4
enter link description here
My routes:
Route::get('/user/{username}', array(
'as' => 'profile-user',
'uses' => 'ProfileController#user'
));
Route::get('/profile/offers', array(
'as' => 'profile-offers',
'uses' => 'OffersController#offers'
));
Route::post('/profile/offers', array(
'as' => 'profile-offers',
'uses' => 'OffersController#postDestroy' ));
Route::post('/profile/offers/create', array(
'as' => 'profile-create',
'uses' => 'OffersController#postCreate'
));
My controller
controllers/OffersController.php
<?php
class OffersController extends BaseController {
public function __construct() {
$this->beforeFilter('csrf', array('on'=>'post'));
$this->beforeFilter('user');
}
public function Offers() {
$offers = array();
foreach(Category::all() as $category) {
$categories[$category->id] = $category->name;
}
//return View::make('offers.index')
return View::make('profile.offers')
->with('offers', Offer::all())
->with('categories', $categories);
}
public function postCreate() {
$validator = Validator::make(Input::all(), Offer::$rules);
if($validator->passes()){
$offer = new Offer;
$offer->category_id = Input::get('category_id');
$offer->title = Input::get('title');
$offer->description = Input::get('description');
$offer->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s').'.'.$image->getClientOriginalName();
$path = public_path('img/offers/' . $filename);
Image::make($image->hetRealPath())->resize(468, 249)->save('public/img/offers/'.$filename);
$offer->image = 'img/offers/'.$filename;
$offer->save();
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers.create') //
->with('global', 'Dodano ogloszenie');
}
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers')
->with('global', 'Cos poszlo nie tak')
->withErrors($validator)
->withInput();
}
public function postDestroy(){
$offer = Offer::find(Input::get('id'));
if ($offer){
File::delete('public/'.$offer->image);
$offer->delete();
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers.destroy')
->with('global', 'Skasowano ogłoszenie');
}
}
public function postToggleAvailability() {
$offer = Offer::find(Input::get('id'));
if ($offer){
$offer->availability = Input::get('availability');
$offer->save();
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers')->with('global', 'Zaktualizowano')
->with('global', 'Zaktualizowano');
}
//return Redirect::route('profile-user', Auth::user()->username);
return Redirect::to('profile.offers')->with('global', 'zle ogloszenie')
->with('global', 'Zaktualizowano');
}
}
my views
views/profile/offers
<h1>Offerts</h1>
<ul>
#foreach($offers as $offer)
<li>
{{ HTML::image($offer->image, $offer->title, array('width'=>'50')) }}
{{ $offer->title }} -
{{ Form::open(array('url'=>'profile/offers/destroy', 'class'=>'form-inline')) }}
{{ Form::hidden('id', $offer->id) }}
{{ Form::submit('delete') }}
{{ Form::close() }} -
{{ Form::open(array('url'=>'profile/offers/toggle-availability', 'class'=>'form-inline')) }}
{{ Form::hidden('id', $offer->id) }}
{{ Form::select('availability', array('1'=>'In Stock', 'O'=>'Out of Stock'), $offer->availability)}}
{{ Form::submit('Update') }}
{{ Form::close() }}
</li>
#endforeach
</ul>
<h2>Create new offers</h2><hr>
#if($errors->has())
<div id="form-errors">
<p>the following errors have occurred:</p>
<ul>
#foreach($errors->all() as $error)
<li>{{ error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('url'=>'profile/offers', 'method' => 'POST', 'files'=>true)) }}
<p>
{{ Form::label('category_id', 'Category') }}
{{ Form::select('category_id', $categories) }}
</p>
<p>
{{ Form::label('title') }}
{{ Form:: text('title', null, array('class' => 'form-control', 'required' => '')) }}
</p>
<p>
{{ Form::label('description') }}
{{ Form:: text('description', null, array('class' => 'form-control', 'required' => '')) }}
</p>
<p>
{{ Form::label('price') }}
{{ Form::text('price', null, array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('image', 'Choose an image') }}
{{ Form::file('image') }}
</p>
{{ Form::submit('Create offers', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
My models/Offer.php
<?php
class Offer extends Eloquent {
protected $fillable = array('category_id,', 'title', 'description', 'price', 'availability', 'image');
public static $rules = array(
'category_id'=>'required|integer',
'title'=>'required|min:2',
'description'=>'required|min:20',
'price'=>'required|numeric',
'availability'=>'integer',
'image'=>'required|image|mimes:jpge,jpg,bmp,png,gif'
);
public function category() {
return $this->belongsTo('Category');
}
}
You don't have the route for the POST request and have not correctly set-up the routes for the rest of your controller actions, hence the 404 exception. Form submissions have method of POST unless explicitly specified to GET. You also have to properly set the controller action you want. Laravel will not know profile/offers/destroy should route to postDestroy() unless you tell it to.
Route::post('/profile/offers/destroy', array(
'as' => 'profile-destroy',
'uses' => 'OffersController#postDestroy'
));
You have to do this each for your controller action.
Related
I have a form, I have troubles in Upload an Image :(
I am trying to upload some image and I don't know what I am doing bad :(
{{ Form::open (['route' => 'titulos.store', 'class'=> 'form', 'files'=> 'true']) }}
{{ Form::label('title', "Titulo:", ['class' => 'col-sm-2 control-label']) }}
{{ Form::text('title') }}
{{ $errors->first('title') }}
<div class="form-group">
{{ Form::label('date', "Fecha:", ['class' => 'col-sm-2 control-label']) }}
<input type="date" name="date" >
</div>
{{ Form::label('description', "Description:", ['class' => 'col-sm-2 control-label']) }}
{{ Form::textarea('description') }}
{{ $errors->first('description') }}
<div class="form-group">
{{ Form::file('image') }}
</div>
{{ Form::label('category_id', 'Category:', ['class' => 'col-sm-2 control-label']) }}
<div class="col-sm-10">
{{ Form::select('category_id', array('1' => 'TBLeaks', '2' => 'Quejas', '3' => 'Denuncias', '4' => 'Ideas'), null, array('class' => 'form-control')) }}
</div>
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
{{ Form::submit('Submit', ['class' => "btn btn-primary"]) }}
</div>
</div>
<div class="row">
<div class="col-sm-offset-2 col-sm-10">
<a class="btn btn-success" href="{{ URL::to('admin') }}">Back to Admin</a>
</div>
</div>
{{ Form::close() }}
</div>
#if(Session::has('message'))
<div class="alert alert-{{ Session::get('class') }}">{{ Session::get('message')}}</div>
#endif
#stop
In my store function I have:
class TitulosController extends BaseController {
public function store(){
$rules = array(
'title' => 'required',
'description' => 'required',
'category_id' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
// proceso de valicion
if ($validator->fails()) {
return Redirect::to('titulos/create')
->withErrors($validator)
->withInput()->withErrors($validator);
} else {
//store
$image = Input::file('image');
$filename = $image->getClientOriginalName();
if(Input::hasFile('image')){
Input::file('image')->move(public_path().'/assets/img/', $filename);
}
$titulo = new Titulo();
$titulo->id = Input::get('id');
$titulo->title = Input::get('title');
$titulo->description = Input::get('description');
$titulo->date = Input::get('date');
$titulo->image = Input::$filename('image');
$titulo->category_id = Input::get('category_id');
$titulo->save();
return Redirect::to('titulos');
}
I have this model for the Titulo table:
class Titulo extends Eloquent {
use SoftDeletingTrait; // for soft delete
protected $dates = ['deleted_at']; // for soft delete
protected $table = 'titulos';
protected $primaryKey = 'id';
protected $fillable = array('image');
public $timestamps = true;
public function __construct() {
parent::__construct();
}
public function category(){
return $this->belongsTo('Category');
}
}
I have this for Image model:
class Image extends Eloquent {
public $timestamps = false;
protected $fillable = array('image');
}
At this line where your store to public path should be no problem
if(Input::hasFile('image')){
Input::file('image')->move(public_path().'/assets/img/', $filename);
}
Only your save Tutilo object looks not good. I think you have mistakenly add $ on Input::filename at this line. which will call the image
$titulo->image = Input::$filename('image');
change your code to this
$titulo = new Titulo();
$titulo->id = Input::get('id');
$titulo->title = Input::get('title');
$titulo->description = Input::get('description');
$titulo->date = Input::get('date');
$titulo->image = $filename; // I change this line. assuming you want to store the file name
$titulo->category_id = Input::get('category_id');
I just have a very simple product category creation form in laravel , like so:
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
For the create method i have the following code:
public function postCreate() {
$validator = Validator::make(Input::all() , Category::$rules);
if($validator->passes()) {
$category = new Category;
$category->name = Input::get('name');
$category->save();
return Redirect::to('admin/categories/index')
->with('message' , 'Category created');
}
return Redirect::to('admin/categories/index')
->with('message' , 'something went wrong')
->withError($validator)
->withInput();
}
Now when i click on the submit button, i get the following error:
C:\xampp\htdocs\ecomm\bootstrap\compiled.php
if (!is_null($route)) {
return $route->bind($request);
}
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0) {
return $this->getOtherMethodsRoute($request, $others);
}
throw new NotFoundHttpException();
}
protected function checkForAlternateVerbs($request)
You can see the error more visvually HERE.
What am i doing wrong ?
Instead of
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
try this:
{{ Form::open(array('route'=>'post.homes')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
In routes.php:
Route::post('aboutus', array('as' => 'post.homes', 'uses' => 'HomeController#postContactUs'));
routes.php
Route::put('questions/{id}', array('as' => 'update.question','before' => 'csrf', 'uses'=> 'QuestionsController#update'));
Route::get('question/{id}', array('as' => 'question', 'uses'=> 'QuestionsController#getview'));
Route::get('questions/{id}/edit', array('as' => 'edit_question', 'uses'=> 'QuestionsController#edit'));
QuestionsController.php
public function edit($id)
{
if(!$this->question_belongs_to_user($id))
{
return Redirect::to('your_questions')
->with('message', 'Invalid Question');
}
return View::make('questions.edit')
->with('title', 'Make It Snappy Q&A - Home')
->with('question', Question::find($id));
}
public function update($id)
{
$id = Input::get('question_id');
if(!$this->question_belongs_to_user($id))
{
return Redirect::to('your_questions')
->with('message', 'Invalid Question');
}
$validation = Question::validate(Input::all());
if ($validation->passes()) {
$newData =[
'question' => Input::get('question'),
'solved' => Input::get('solved'),
];
$Question=Question::find($id);
$Question->fill($newData)->save();
return Redirect::to('question', $id)
->with('message', 'Your question has been updated!.');
} else {
return Redirect::to('edit_question')->withErrors($validation)->withInput();
}
}
edit.blade.php
#extends('layouts.default')
#section('content')
<h1>Edit Your Question</h1>
#if($errors->has())
<ul id="form-errors">
{{ $errors->first('question', '<li>:message</li>>') }}
{{ $errors->first('solved', '<li>:message</li>>') }}
</ul>
#endif
{{ Form::model($question, array('route' => array('update.question', $question->id), 'method' => 'put')) }}
<p>
{{ Form::label('question', 'Question') }}
{{ Form::text('question', $question->question) }}
</p>
<p>
{{ Form::label('solved', 'Solved') }}
#if($question->solved == 0 )
{{ Form::checkbox('solved', 1, false) }}
#elseif($question->solved == 1 )
{{ Form::checkbox('solved', 0, true) }}
#endif
</p>
{{ Form::hidden('question_id', $question->id) }}
<p> {{ Form::submit('Update') }} </p>
{{ Form::close() }}
#stop
try to update record following error facing, where is problem
[2015-03-25 14:15:04] production.ERROR: exception 'Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException' in E:\Web\xampp\htdocs\makeitsnappy\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php:210
where record is updated into the destination table but when redirect to the route
return Redirect::to('question', $id)
here it's not working
after try both ways Form::model(), and Form::open() but in vain?
Redirect::to() requires a full path. Change it to use the route() method, and provide the route parameters:
Redirect::route('question', array('id' => $id));
sorry if this is a very newbie Q..
but please help me to solve this problem. plus give me the reason about why this error happened..
this is my edit view
new.blade.php
#section('content')
#include('common.show_error')
{{Form::open(array('url'=>'author/update', 'method'=>'PUT'))}}
<p>
{{ Form::label('name', 'Name: ') }}</br>
{{ Form::text('name', $author->name) }}
</p>
<p>
{{ Form::label('bio', 'Biography: ') }}</br>
{{ Form::textarea('bio', $author->bio) }}
</p>
{{ Form::hidden('id', $author->id) }}
<p>{{ Form::submit('Edit Data') }}</p>
#stop
this is my show view
show.blade.php
#extends('layouts.default')
#section('content')
<h1>{{ $author->name }}</h1>
<p>{{ $author->bio }}</p>
<p>{{ $author->updated_at }}</p>
<span>
{{ HTML::linkRoute('authors', 'Home') }} |
{{ HTML::linkRoute('edit_author', 'Edit', array($author->id)) }} |
{{ Form::open(array('url'=>'author/destroy', 'method'=>'DELETE', 'style'=>'display: inline;')) }}
{{ Form::hidden('id', $author->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
</span>
#stop
this is my controller
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
Member::where('id','=',$id)->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated');
}
}
the case: when I try to edit data using edit button. it said:
"Trying to get property of non-object laravel"
and when I check at the error log. it refers to
<h1>{{ $author->name }}</h1>
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
$author = Member::find($id);
$author->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated')
->with('author', $author);
}
}
Little changes in your controller, try it :) In your code, you are not send variable "author" into your view.
I am in the progress of making a form to edit an existing entry in the database. I am using the Form::model approach to do this, however it doesn't seem to work. The fields just stay empty.
ServerController.php
/**
* Editing servers
*/
public function edit($name)
{
$server = Server::find($name);
$keywords = ($server->getKeywords()) ? $server->getKeywords() : array();
$countries = $this->getCountries();
return View::make('server/edit', array('server' => $server, 'countries' => $countries));
}
public function update($name)
{
$server = Server::find($name);
// Did it succeed?
if($server->save()) {
Session::flash('success', 'You server was edited!');
return Redirect::route('server.view', array($name));
}
// Did not validate
if(Input::get('keywords')) {
$keywords = Input::get('keywords');
Session::flash('keywords', $keywords);
}
Session::flash('danger', "<b>Oops! There were some problems processing your update</b><br/>" . implode("<br/>", $server->errors()->all()));
return Redirect::route('server.edit', array($name))->withInput()->withErrors($server->errors());
}
The Form
{{ Form::model($server, array('route' => array('server.update', $server->name), 'class' => 'form-horizontal', 'role' => 'form', 'files' => true)) }}
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{{ Form::label('email', 'Email', array('class' => 'control-label col-md-4')) }}
<div class="col-md-4">
{{ Form::text('email', '', array('class' => 'form-control')) }}
{{ $errors->first('email', '<br/><div class="alert alert-danger">:message</div>') }}
</div>
</div>
(some more fields)
{{ Form::close() }}
The problem here is that you're passing in an empty string as the default field value. As the documentation states here, any explicitly passed values will overrule the model attribute data. Try using null instead of '':
{{ Form::text('email', null, array('class' => 'form-control')) }}