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.
Related
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));
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.
Earlier today I had the exact same problem with Auth::attempt always retuning false. I realized that Auth checks for a hashed password, so by doing so I was able to get it to return true, but now it always does. Even if I type asdadfasdfaf in my form, the if statement loads the page. Any suggestions?
Controller:
class userController extends \BaseController
{
public function login()
{
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::attempt($user))
{
return Redirect::route('home');
}
else
{
return View::make('login');
}
}
}
Form
{{ Form::open(array('url' => 'home' )) }}
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
The Routes file:
Route::post('home', 'userController#login');
No matter what I enter it always directs me to my "home" page?
The action url should be login
{{ Form::open(array('url' => 'login' )) }}
^^^^ as you used Auth::attempt to this URL
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
I'm stuck..
I want to add to give the possibity to add a new photo through a form.
I have the Photo resource properly set up.
The table photo with: id, title, caption, path:string.
This is the form that i made until now in the view photo.create:
{{ Form::open(array('route' => 'photos.store'),'image/save', array('files'=> true)) }}
{{ Form::label('title', 'Title: ') }}
{{ Form::text('title') }}
{{ Form::label('caption', 'Caption: ') }}
{{ Form::textarea('caption') }} <br>
{{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}
{{ Form::close() }}
How can i add a button thanks to which select a new file?
Thank you!!
EDIT:
This is the simple store method i made until now:
public function store()
{
$input = Input::all();
$rules = array('title' => 'required', 'path' => 'required');
$validation = Validator::make($input, $rules);
if ($validation->passes())
{
$photo = new Photo();
$photo->title = $input['title'];
$photo->caption = $input['caption'];
$photo->path = $input['path'];
$photo->save();
return Redirect::route('photos.index');
}
return Redirect::back()->withInput()->withErrors($validation)->with('message','There were validation message');
}
How can i put correctly there that implementation to retrieve the file and store in the folder located in public/img ?
And then how can i save that path and put into $photo->path ?
Thank you very much!!
Use Form::file('image')
Then, you can retrieve your uploaded file with Input::file('image') and move it to a destination with Input::file('file')->move(YOUR_DESTINATION_PATH);
References : http://laravel.com/docs/requests#files and http://laravel.com/docs/html#file-input
edit :
To store your uploaded file to public/img : Input::file('file')->move(base_path() . '/public/img');
Storing path in database :
$photo->path = base_path() . '/public/img' . Input::file('file')->getClientOriginalName(); // if you want your real path on harddrive
or
$photo->path = URL::to('img/' . Input::file('file')->getClientOriginalName()); // if you want an exploitable path for http render
second edit
See my correction on your form http://paste.laravel.com/KX9
#extends('master')
#section('blog')
<div class="span12 well">
{{ link_to_route('photos.index', 'Back to index') }}
</div>
<div class="span12 well">
{{ Form::open(array('route' => 'photos.store', 'files'=> true)) }}
{{ Form::label('title', 'Title: ') }}
{{ Form::text('title') }}
{{ Form::label('caption', 'Caption: ') }}
{{ Form::textarea('caption') }}
{{ Form::label('image', 'Image: ') }}
{{ Form::file('image') }}
<br>
{{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}
{{ Form::close() }}
<br><br>
#if($errors->any())
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
#endif
</div>
#stop
you can use Form::file('image');
see http://laravel.com/docs/html#file-input
{{ Form::open(array('action' => 'HomeController#upload', 'files'=>true)) }}
{{ Form::label('image', 'Upload Image')}}
{{ Form::file('image') }}
{{ Form::submit('Submit') }}
{{ Form::close() }}
Step2:
public function upload(){
$image = Input::file('image');
$savepath = 'public/uploads/';
$filename = $image->getClientOriginalName();
Input::file('image')->move($savepath, $filename);
}
Final Step:
Route::get('/upload' function()
{
return View::make('upload');
});
Route::post('/upload', 'HomeController#upload');