laravel 4.2 update result no working - php

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));

Related

Getting a NotFoundHttpException() Error in laravel

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'));

Laravel 4 creating form open with database

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.

viewing my database in the view at laravel

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.

laravel 4 Auth::attempt allways returns true?

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() }}

Can't prevent users going back to the admin page after logging out - Laravel 4

I'm facing a weird issue while developing a very basic application using Laravel 4. I have created the functionality following the documentation of laravel 4's user authenticating docs where users can sign up to ask different questions and only the registered users can access to the secret/admin page. Everything works well so far except that I'm getting this weird issue where even after logging out users can still access to the precious page or in other word the admin page. I'm not sure it has something to do with Laravel but still I couldn't figure out what's the issue and how to prevent or force the browser to reload or something so that they can't see the admin page even if they click on the back arrow in browser.
Although it may not be relavent but I'm still attaching the codes I have. In routes.php I have this
<?php
Route::get('/', array('as'=>'home', 'uses'=>'QuestionController#getindex'));
Route::get('register', array('as'=>'register', 'uses'=>'UserController#getnew'));
Route::get('login', array('as'=>'login', 'uses'=>'UserController#getlogin'));
Route::get('logout', array('as'=>'logout', 'uses'=>'UserController#getlogout'));
Route::post('register', array('before'=>'csrf', 'uses'=>'UserController#postcreate'));
Route::post('login', array('before'=>'csrf', 'uses'=>'UserController#postlogin'));
And in userController I have this
<?php
class UserController extends BaseController {
public function getNew() {
return View::make('users.new')
->with('title', 'Snappy Q&A - Register');
}
public function postCreate() {
$validator = Member::validate(Input::all());
if ( $validator->passes() ) {
$user = User::create( array (
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password'))
));
Auth::login($user);
return Redirect::route('home')->with('message', 'Thanks for registering!');
} else {
return Redirect::route('register')->withErrors($validator)->withInput();
}
}
public function getLogin() {
return View::make('users.login')
->with('title', 'Snappy Q&A - Login');
}
public function postLogin() {
$user_creds = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if( Auth::attempt($user_creds) ) {
return Redirect::route('home')
->with('message', 'Yep, you are now logged in');
} else {
return Redirect::route('login')
->with('message', 'Shit man! The creds are not authorised!')
->withInput();
}
}
public function getLogout() {
if( Auth::check() ) {
Auth::logout();
return Redirect::route('login')
->with('message', 'You are now logged out!');
} else {
return Redirect::route('home');
}
}
}
This new.blade.php is responsible for creating a new user
#extends('master.master')
#section('content')
#if( $errors->has() )
<p>The following erros has occured: </p>
<ul class="form-errors">
{{ $errors->first('username', '<li>:message</li>') }}
{{ $errors->first('password', '<li>:message</li>') }}
{{ $errors->first('password_confirmation', '<li>:message</li>') }}
</ul>
#endif
{{ Form::open( array('route'=>'register', 'method'=>'post')) }}
{{ Form::token() }}
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username')) }}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{ Form::label('password_confirmation', 'Confirm Password') }}
{{ Form::password('password_confirmation') }}
{{ Form::submit('Register', array('class'=>'btn btn-success')) }}
{{ Form::close() }}
#stop
This one is for logging in a user:
#extends('master.master')
#section('content')
{{ Form::open( array('route'=>'login', 'method'=>'post') ) }}
{{ Form::token() }}
{{ Form::label('username', 'Username') }}
{{ Form::text('username', Input::old('username')) }}
{{ Form::label('password', 'Password') }}
{{ Form::password('password') }}
{{ Form::submit('Login', array('class' => 'btn btn-success')) }}
{{ Form::close() }}
#stop
TO make it clear, I don't have anything special yet for the admin page except the condition in the navigation with the method Auth::check(); to make sure that only logged in user can see the logout navigation. I will create the functionality later after I'm done with this problem. The admin page view will be in a folder called questions. I hope this makes sense now.
How do I take care of that? Please ask if you still need any other instance of my code. And I believe many of the newbies in Laravel world face more or less the same issue while developing functionality like that. I'm hoping this is a good question and will help others as well as me.
Try to use filters in routes
Route::get('/', array('before' => 'auth', function()
{
}));
More:
http://laravel.com/docs/routing#route-filters

Categories