serialization of 'closure not allowed' Error - laravel

Below is the code from my product view:
{{ Form::open(array('url'=>'admin/products/create' , 'files'=>true)) }}
<p>
{{ Form::label('category_id' , 'Category') }}
{{ Form::select('category_id' , $categories) }}
</p>
<p>
{{ Form::label('title') }}
{{ Form::text('title') }}
</p>
<p>
{{ Form::label('description') }}
{{ Form::textarea('description') }}
</p>
<p>
{{ Form::label('height') }}
{{ Form::text('height' , null , array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('width') }}
{{ Form::text('width' , null , array('class'=>'form-price')) }}
<!--{{ Form::label('image' , 'Choose an image') }}
{{ Form::file('image') }}-->
</p>
<p>
{{ Form::label('length') }}
{{ Form::text('length' , null , array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('color') }}
{{ Form::text('color' , null , array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('material') }}
{{ Form::text('material' , null , array('class'=>'form-price')) }}
</p>
{{ Form::submit('Create Product' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
and below is the code from my product controller:
<?php
/**
*
*/
class ProductsController extends BaseController {
public function __construct() {
$this->beforeFilter('csrf' , array('on'=>'post'));
}
public function getIndex() {
/* NOTE :: the categories array is being
created so that the products index page
can have access to all the availability
categories */
$categories = array();
foreach (Category::all() as $category) {
$categories[$category->id] = $category->name;
}
return View::make('products.index')
->with('products' , Product::all())
->with('categories' , $categories);
}
public function postCreate() {
$validator = Validator::make(Input::all() , Product::$rules);
if($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->height = Input::get('height');
$product->width = Input::get('width');
$product->length = Input::get('length');
$product->color = Input::get('color');
$product->material = Input::get('material');
/* code for saving the image */
/* $image = Input::file('image');
$filename = date('Y-m-d-H-i-s')."-".$image->getClientOriginalName();
$path = public_path('img/products/' .$filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
$product->image = 'img/products/'.$filename; */
/*end of code for saving the image */
$product->save();
return Redirect::to('admin/products/index')
->with('message' , 'Product created');
}
return Redirect::to('admin/products/index')
->with('message' , 'something went wrong')
->withError($validator)
->withInput();
}
public function postDestroy() {
$product = Product::find(Input::get('id'));
if ($product) {
$product->delete();
return Redirect::to('admin/products/index')
->with('message' , 'product has been deleted');
}
return Redirect::to('admin/products/index')
->with('message' , 'something went wrong , Please try again');
}
}
?>
Now when i fill the values in the form and click on submit i get a error as follows :
Error image
serialization of 'closure not allowed' , on googling a bit i found the following helpful thread:
Related thread,
the accepted solution is not applicable to me and i tried the solution in the second answer and i still have the same error.
I still don't understand what could be the cause of this error or what am i doing wrong ? can anybody explain ?

Unless it is a typo in the code you have posted, the accepted solution to the related question you posted does relate to you.
On your return statement, you have ->withError($validator), this should be ->withErrors($validator) (you're missing the 's').

Related

View is not getting anything from the controller

Can anyone tell me why this will not return show.blade.php data?
ROUTE
Route::resource('news', 'NewsController', ['except' => ['create', 'store', 'edit', 'update', 'destroy']]);
MODEL
public function categories()
{
return $this->belongsToMany(ContentCategory::class);
}
public function tags()
{
return $this->belongsToMany(ContentTag::class);
}
CONTROLLER
public function show(News $news)
{
$news->load('categories', 'tags', 'product_press_releases', 'section');
return view('site.news.show', compact('news'));
}
VIEW show.blade.php
#section('content')
{{ $news->title ?? '' }}
{{ $news->id ?? '' }}
#foreach($news->categories as $key => $category)
<span class="label label-info">{{ $category->name }}</span>
#endforeach
#endcontent
For the life of me I cannot get why no data is being returned. I do these all the time and never ran into this.
I believe that your are not extending it to app.blade.php.
Add - #extends('YOUR_APP_LINK').
In your case -
#extends('YOUR_APP_LINK')
#section('content')
{{ $news->title ?? '' }}
{{ $news->id ?? '' }}
#foreach($news->categories as $key => $category)
<span class="label label-info">{{ $category->name }}</span>
#endforeach
#endcontent
Hope this will help you.

Update Data in Laravel

This is my code :
Route:
Route::get('/editposts/{id}', function ($id) {
$showpost = Posts::where('id', $id)->get();
return view('editposts', compact('showpost'));
});
Route::post('/editposts', array('uses'=>'PostController#Update'));
Controller :
public function Update($id)
{
$Posts = Posts::find($id);
$Posts->Title = 10;
$Posts->Content = 10;
$Posts->save();
//return Redirect()->back(); Input::get('Title')
}
and View:
#foreach($showpost as $showpost)
<h1>Edit Posts :</h1>
{{ Form::open(array('url'=>'editposts', 'method'=>'post')) }}
Title : {{ Form::text('Title', $showpost->Title) }} <br> Content : {{ Form::text('Content', $showpost->Content ) }} <br> {{ Form::submit('Update') }}
{{ Form::close() }}
#endforeach
but when I want to Update my data i receive an error :
http://localhost:8000/editposts/1
Missing argument 1 for App\Http\Controllers\PostController::Update()
You need to change route:
Route::post('editposts/{id}', 'PostController#Update');
Then the form to:
{{ Form::open(['url' => 'editposts/' . $showpost->id, 'method'=>'post']) }}
Change your post route to:
Route::post('/editposts/{id}', 'PostController#Update');
Done!
Correct the route,specify a parameter
Route::post('editposts/{id}', 'PostController#Update');
Pass the post'id as paramater
{{ Form::open(array('url'=>'editposts/'.$post->id, 'method'=>'post')) }}
Title : {{ Form::text('Title', $showpost->Title) }} <br> Content : {{ Form::text('Content', $showpost->Content ) }} <br> {{
Form::submit('Update') }}
{{ Form::close() }}
Notice $post->id
First declare your route:
Route::post('/editposts/{id}', array('uses'=>'PostController#Update'));
Then update your form url:
{{ Form::open(['url' => url()->action('PostController#Update', [ "id" => $showpost->id ]), 'method'=>'post']) }}
This is assuming your model's id column is id
(Optional) You can also use implicit model binding :
public function Update(Posts $id) {
//No need to find it Laravel will do that
$id->Title = 10;
$id->Content = 10;
$id->save();
}

eloquent create not inserting everything

I want to insert a row in my database using the input from a form.
//PersonController
Public function store(){
$input = Input::all();
if(! $this->person->fill($input)->isValid()){
return Redirect::back()->withInput()->withErrors($this->person->messages);
}
$this->person->create($input);
}
//class Person
protected $fillable = ['name', 'group_id', 'email', 'phone', 'address', 'isresponsible'];
public $messages;
public function isValid(){
$validation = Validator::make($this->attributes, static::$rules);
if ($validation->passes()) return true;
$this->messages = $validation->messages();
return false;
}
//Form in View
{{ Form::open(array('action' => 'PersonController#store')) }}
{{ Form::text('name') }}
{{$errors->first('name', '<span class=error>:message</span>')}}
{{ Form::select('group', $groups, Input::old('id')) }}
{{ Form::email('email') }}
{{ Form::text('phone') }}
{{ Form::text('address') }}
{{ Form::checkbox('isResponsible') }}
{{ Form::submit('Create') }}
{{ Form::close() }}
The sql statement generated by
$this->person->create($input)
is missing the foreign key (group_id) and the boolean value (isresponsible)
That's because the names in your form don't match the names of the attributes in the database.
isResponsible ≠ isresponsible
group ≠ group_id
Simply change the names in your form:
{{ Form::select('group_id', $groups, Input::old('group_id')) }}
{{-- ... --}}
{{ Form::checkbox('isresponsible') }}

Laravel 4 :: custom validation error message is not displaying?

I'm trying to create an online form and to display my own error messages. But for some reason it's not working correctly. Here's my controller code, CategoryController.php:
class CategoryController extends BaseController
{
public function add()
{
return View::make('admin');
}
public function validate_add()
{
$rules = array('category_name' => 'Required|Alpha|Min:4');
$messages = array('category_name.Required' =>'Please enter the category name');
$input = Input::all();
$validator = Validator::make($input, $rules, $messages);
if ($validator->fails())
{
return Redirect::to('admin')->withErrors($validator)->withInput(Input::all());
}
else
{
echo '<h1>WOW! your are are awesome!!! <3<h1> ';
}
}
}
And the admin.blade.php is following:
#extends('common')
#section('body')
<h1>Add Category</h1>
{{ HTML ::ul($errors->all(), array('class'=>'errors')) }}
{{ Form::open(array( 'url'=>'admin', $title="Admin Control Panel")) }}
<p>
{{ Form::label('Category Name:') }}
{{ Form::text('category_name', Input::old('category_name')) }}
</p>
<p>
{{Form::label('Parent Category') }}
{{ Form::select('Network', array('0' => 'Maincategory')) }}
</p>
<p>
{{ Form::submit('Submit') }}
</p>
{{ Form::close() }}
#stop
Interesting - I tried this on my local Laravel project and it looks like although you can specify your rules in mixed case, you need to specify the custom messages in lower case.
So if you changed your rules and your message override to; 'required' instead of 'Required' etc., it should work.

Laravel 4 - no guessers available issue

I get this error: LogicException: Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?) while trying to upload an image.
I have enabled the php_fileinfo extension and also restarted the Wamp web server but I still unable to solve this. What am I missing? Thanks
Below are my codes:
Models: Product.php
class Product 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:jpeg,jpg,bmp,png,gif|max:3000',
);
public function category() {
return $this->belongsTo('Category');
}
}
Controllers: ProductsController.php
public function postCreate() {
$validator = Validator::make(Input::all(), Product::$rules);
if($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('image');
$filename = date('Y-m-d-H:i:s')."-".$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(468,249)->save('public/img/products/'.$filename);
$product->image = 'img/products/'.$filename;
$product->save();
return Redirect::to('admin/products/index')
->with('message', 'Product Created');
}
return Redirect::to('admin/products/index')
->with('message', 'Something went wrong')
->withErrors($validator)
->WithInput();
}
Views: Index.blade.php
{{ Form::open(array('url'=>'admin/products/create', 'files'=>true)) }}
<p>
{{ Form::label('category_id', 'Category') }}
{{ Form::select('category_id', $categories) }}
</p>
<p>
{{ Form::label('title') }}
{{ Form::text('title') }}
</p>
<p>
{{ Form::label('description') }}
{{ Form::textarea('description') }}
</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 Product', array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
uncomment this line in php.ini into php folder.
extension=php_fileinfo.dll
and restart the server(enter 'php artisan serve' again). This way will works!
Open php.ini file and you can find ;extension=php_fileinfo.dll remove the semi-coloumn to extension=php_fileinfo.dll will work perfectly then restart the your apache server or xampp ,wampp whatever environment you are using
I think is the WAMP Web Server's Bug. I switched to XAMPP Web Server and it works fine.
Thanks alot by the way.

Resources