Closed jeffpaitchell closed 10 years ago
Thanks for using my package!
First, when you edit the package files you'll run into problems when you run composer update
since that will erase all your changes.
This might help you:
{{ Form::model($album, array('method' => 'PUT', 'route' => array('gallery.album.update', $album->album_id))) }}
so it routes to a new controller that you will create in your app, for example (docs).
JeroenG\LaravelPhotoGallery\Controllers\AlbumsController
and the construct() function should have: parent::__construct();
update()
(and maybe store()
too) from AlbumsController.php and edit them so they save your things too.use JeroenG\LaravelPhotoGallery\Models\Album;
And then in your controller you can use it sort of like this:
$album = Album::find($id);
$album->album_name = $albumName;
$album->album_description = $albumDescription;
$album->album_awesomeness = $albumAwesomenessLevel;
// other stuff
$album->touch();
return $album->save();
It's getting close to the end of the day for me but I will definitely try your steps in the morning and get back to you ASAP.
Thank you so much for the quick reply!
Ok so I tried using your suggestions by doing the following:
1) Defining a new controller for editing albums named 'EditAlbumsController.php' and the code is:
use JeroenG\Laravel\PhotoGallery\Models\Album; use JeroenG\LaravelPhotoGallery\Validators as Validators;
class EditAlbumsController extends AlbumsController {
public function __construct()
{
parent::__construct();
}
public function update($id)
{
$input = \Input::except('_method');
$validation = new Validators\Album($input);
if ($validation->passes())
{
$album = Album::find($id);
$album->album_name = $input['album_name'];
/* Additional Database Columns */
$album->touch();
return $album->save();
return \Redirect::route('gallery.album.show', array('id' => $id));
}
else
{
return \Redirect::route('gallery.album.edit', array('id' => $id))
->withInput()
->withErrors($validation->errors)
->with('message', \Lang::get('gallery::gallery.errors'));
}
}
2) Defining a new route in 'routes.php' as:
Route::put('gallery/album/{id}/edit', array('as'=>'edit_album', 'uses'=>'EditAlbumsController@update'));
3) Changing the 'Form' code in 'edit-album.blade.php' to ->
{{ Form::model($album, array('method' => 'PUT', 'route' => array('edit_album', $album->album_id))) }}
The problem now is when I get to the 'edit' page for editing albums, when I hit the 'update' button, I keep getting a '502 Bad Gateway' error. Could this be because a route was not defined properly is my 'EditAlbumsController' not defined correctly?
I did run 'composer dumpautoload -o' after making some changes in the code as per a suggestion I found online, not sure if this had an effect.
EDIT: I'm creating a new controller and NOT using 'php artisan dump-autoload' OR 'composer dumpautoload -0' since I think that may have created the problem described above. So my new question is, how can I extend my 'EditAlbumsController.php' class to your 'AlbumsController.php' class since when I try to update the form, the class 'AlbumsController' is not found?
Should I use a special 'namespace' line of code or a 'use' line of code at the top of the 'EditAlbumsController.php' file?
EDIT #2: The more I keep digging around this issue, I'm almost convinced that this is a namespacing issue with the controllers. How do I sync my new 'EditAlbumsController.php' to YOUR 'AlbumsController.php' file?
You have to include the whole namespace when extending the AlbumsController, as I mentioned in the third step. :wink:
I also see you have two returns in your validation->passes() statement. The second will never be run.
Ok I got it updating properly! I still need to make some changes and additions but here's what I did:
'EditAlbumsController.php':
Added this line to the top of the code -> 'use JeroenG\LaravelPhotoGallery\Controllers\AlbumsController;'
Removed the second 'return' statement that you mentioned.
'edit-album-blade.php':
Changed the form code: {{ Form::model($album, array('method' => 'PUT', 'route' => array('edit_album2', $album->album_id))) }}
'routes.php':
Route::put('gallery/album/{id}/edit', array('as'=>'edit_album2', 'uses'=>'EditAlbumsController2@update'));
Thank you so much and I will definitely keep in touch with you!
Now that my albums data is updating, the issue I'm having now is being able to edit and update checkbox data. The problem is when I edit and try to update the form with unchecked Checkboxes, I get an "undefined index" error. Your 'AlbumsController.php' file stores unchecked Checkbox data fine but this problem only occurs when I try to update the form with the unchecked Checkboxes.
EDIT: Actually got a solution on Stack Overflow by changing:
$album->album_application_kitchen = $input['album_application_kitchen'];
To:
$album->album_application_kitchen = Input::get('album_application_kitchen');
I am glad you got everything working. :smiley: What extra columns did you add? Maybe it could be valuable for others too.
Added the following columns:
3 Dropdown columns 2 Different Checkbox sections, both containing roughly 5 or 6 checkboxes 2 Additional sections for text input
I'm also thinking about doing the same process for editing Photos as well. Again its the end of the day for me so I won't know if I'm going to take that route until tomorrow.
If I do take that route, my questions would be the following:
If I define a new controller for editing photos, in the update() function I would still need to pass both the '$albumId' & '$photoId' parameters to this function. So I'm thinking the function would look something like this:
'EditPhotosController.php'
public function update($albumid, $photoid)
{
$input = \Input::except('_method');
$validation = new Validators\Photo($input);
if ($validation->passes())
{
$photo = Photo::find($photoid);
$photo->photo_name = $input['photo_name'];
$photo->photo_description = $input['photo_description'];
$photo->photo_path = $input['photo_path'];
$photo->album_id = $input['album_id'];
$photo->touch();
$photo->save();
return \Redirect::route("gallery.album.photo.show", array('albumid' => $albumId, 'photoid' => $photoId));
}
else
{
return \Redirect::route("gallery.album.photo.edit", array('albumid' => $albumId, 'photoid' => $photoId))
->withInput()
->withErrors($validation->errors)
->with('message', \Lang::get('gallery::gallery.errors'));
}
}
'routes.php'
Route::put('gallery/album/{albumid}/photo/{photoid}/edit', array('as'=>'edit_photo', 'uses'=>'EditPhotosController@update'));
I actually tried using the code above earlier but received an error in my 'EditPhotosController.php' controller where 'albumId' in the line below was not defined:
return \Redirect::route("gallery.album.photo.edit", array('albumid' => $albumId, 'photoid' => $photoId))
Once again, thank you so much for helping me out with this, you've done a lot! I hope I gave you enough information above regarding the database columns I added.
What I meant whas, for what functionality do you plan to use the columns? Just out of curiosity.
Regarding your error: in the update() statement you have $albumid
and in the return statement $albumId
(notice the capitalised i )
The functionality for the additional columns is to add more attributes to each photo album. Originally each photo album just had a name and a description, so I wanted to add more attributes to each photo album such as different applications, colors and other numeric values that each photo contains since the photos deal with technical specifications.
For instance, when a user adds a photo to a particular album, the photo should belong to the appropriate album, etc.
Thank you for noticing the case-sensitive issue I had with 'EditPhotosController.php'. My custom controller is now almost working except now I am trying to include new validation :)
Since I've included additional columns to your photo gallery, I am now trying to write a custom 'Validation' file to account for these new columns. The problem I'm having now is very similar to what I had yesterday where I'm trying to include a custom local 'Photo.php' Validation file but I keep getting a 'Class Not Found' error.
I've tried using 'php artisan dump-autoload' but that hasn't been working for me either. So I'm thinking this might be a simple namespacing issue.
EditPhotosController.php:
use JeroenG\LaravelPhotoGallery\Controllers\AlbumsController; /* From Package */
use JeroenG\LaravelPhotoGallery\Controllers\PhotosController; /* From Package */
use JeroenG\LaravelPhotoGallery\Models\Album; /* From Package */
use JeroenG\LaravelPhotoGallery\Models\Photo; /* From Package */
use Models\Validators as Validators; /* Custom local file */
class EditPhotosController extends PhotosController {
public function __construct()
{
parent::__construct();
}
public function update($albumId, $photoId)
{
$input = \Input::except('_method');
$validation = new Validators\Photo($input); // Here's where error occurs
/* Validation check and update code etc. */
}
}
}
Photo.php -> File path: Models\Validators\Photo.php
namespace Models\Validators; /* Local file path */
class Photo extends Validator {
public static $rules = array(
'album_id' => 'required',
'photo_name' => 'required',
'photo_description' => 'max:255',
);
}
Not sure if I missed a step in including my custom local files properly.
The validator class that your Photo class extends is not a Laravel class.
class Photo extends \JeroenG\LaravelPhotoGallery\Validators\Validator
Oh ok. I see that now :) So if I want to include this custom 'Photo.php' validation file, how do I include it properly so that the class is found?
You could create an app/validators folder and include it in the autload classmap (and maybe no namespacing).
I created a folder 'app/validators' and edited my 'composer.json' file to include in the classmap "app/validators".
I tried running the command 'php artisan dump-autoload' but it still didn't do anything since I keep getting the same error. Do I have to use the command 'composer dump-autoload -o' in order for the change in my 'composer.json' file to take effect?
Have a look at this: http://tntstudio.us/blog/laravel-form-validation-as-a-service
That documentation helped me out a lot! The issue was both a namespacing and class autoload issue.
Made the following changes:
Created a new local folder 'validators' in my 'app' directory
Edited my 'composer.json' file and put "app/validators" within my "autoload/classmap" block
In my validators files within the 'Validators' folder, 'Photo.php' and 'Validator.php' both have the line of code at the top 'namespace app\validators;'
In my 'EditPhotosController.php' file, I include at the top"use app\validators as Validators;"
After making the above changes, I ran the command 'php artisan dump-autoload' in Putty and now my custom validation file works fine! Thank you so much for all your help!
You're welcome. If everything works I will close this issue.
Yes everything is working for now. If I have any future issues, I'll just create a new one.
Once again, thank you so much!
Your program works great by the way so I'm fine with the latest version you have available for download on Github.
Here's what I'm trying to accomplish:
1) Add additional columns (database fields) to the 'Albums' table -> Done 2) Create albums with these new columns -> Done 3) Display album information with the new columns -> Done 4) Edit albums with the new columns -> This is where I'm having an issue since when I try to edit the albums with the new columns, only the 'album_name' & 'album_description' columns are being updated.
I tried editing the 'EloquentAlbumRepository' file by adding lines for the new database fields in the 'update' function but the fields are still not being updated.
How do I accomplish this task and is it even possible since I am using your code package ?