FrozenNode / Laravel-Administrator

An administrative interface package for Laravel
http://administrator.frozennode.com/
MIT License
1.94k stars 502 forks source link

Laravel Newbie hitting the wall #404

Closed billyzduke closed 10 years ago

billyzduke commented 10 years ago

Hello

I've been trying to get your administrator up and running in my local build. At the moment, my demo app is very simple: there are only two tables/models, games and users (who can approve games), and I've set games to be the home_page menu item, and created /config/administrator/games.php in addition to my original /models/Game.php.

However, I'm getting a 500 error when I hit /public/admin and it redirects to /public/admin/games. I have been going in circles with the documentation for a few hours now and I can't figure out what I've left out or entered incorrectly.

Sorry to trouble you, but could you please direct me to a working demo of the administrator online? Or to a thread that addresses my issue? Perhaps I just need a second set of eyes here, still getting used to a lot of new coding conventions. Thanks in advance for whatever help you can offer...

Here's the code for my games model config:

return array
(   'title' => 'Games'
,   'single' => 'game'
,   'model' => 'Game'
,   'columns' => array
    (   'title'
    ,   'publisher'
    ,   'complete' => array
        (   'select' => "IF((:table).complete, 'Yes', 'No')"
        )
    ,   'approved_by' => array
        (   'relationship' => 'user'
        ,   'select' => "(:table).user_id"
        )
    ,   'created_at'
    ,   'updated_at'
    )
,   'edit_fields' => array
    (   'title'
    ,   'publisher'
    ,   'complete' => array
        (   'type' =>   'bool'
        )
    ,   'approved_by' => array 
        (   'type' => 'relationship'
        ,   'name_field' => 'username'
        )
    ,   'created_at' => array 
        (   'type' => 'date'
        )
    ,   'updated_at' => array 
        (   'type' => 'date'
        )
    )
);
janhartigan commented 10 years ago

Can you check your server error log and tell me what the 500 error is?

billyzduke commented 10 years ago

This seems to be it:

PHP Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 78 bytes) in /Users/billyzduke/Sites/games-app/vendor/frozennode/administrator/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsTo.php on line 74

Perhaps I've incorrectly defined my relationship between the users and games tables?

//app/models/Game.php

class Game extends Eloquent{

    public function approved_by()
    {
        return $this->belongsTo('User');
    }

}
janhartigan commented 10 years ago

Looks like php is getting angry about the memory allocation. Since Laravel (and Administrator) uses PSR-0 autoloading, what's likely happening is that Administrator's loaded files are just pushing your request over the memory limit. You should try increasing the memory limit (php.ini memory_limit option) to what you need. Let me know if you have any questions.

billyzduke commented 10 years ago

I've been continuing to plumb the depths of my system in search of this error... I've deleted all PHP instances that I'm not using, leaving 5.5.3 running under MAMP Apache. I've upped the memory limit to 128M, and now I get this error:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 72 bytes) in /Users/billyzduke/Sites/games-app/vendor/laravel/framework/src/Illuminate/Validation/Validator.php on line 166

janhartigan commented 10 years ago

Ah ha...ok so 134mb is a much more unreasonable size. I think I may know what's going on here. Can you show me your eloquent model?

billyzduke commented 10 years ago

Further investigation seems to indicate that it has to do with the rules array, which was missing from my Eloquent Model. I've included a rule to test, and am now getting this error:

PHP Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 78 bytes) in /Users/billyzduke/Sites/games-app/vendor/frozennode/administrator/src/Frozennode/Administrator/DataTable/Columns/Column.php on line 260

Here's my game model:

class Game extends Eloquent{

    public static $rules = array
    (   'title' => 'required'
    );

    public function approved_by()
    {
        return $this->belongsTo('User');
    }

}
janhartigan commented 10 years ago

Your method name is incorrect. In Laravel 4 it needs to be approvedBy.

billyzduke commented 10 years ago

Ok, sorry if I seem dense, but it seems like we're circling ever closer to the solution... Having changed that method name, and updated references to it elsewhere, I'm now seeing this:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 77 bytes) in /Users/billyzduke/Sites/games-app/vendor/frozennode/administrator/src/Frozennode/Administrator/DataTable/Columns/Relationships/BelongsTo.php on line 74

Must I include a rule for each column, including relationships? This seems to be the sticking point...

janhartigan commented 10 years ago

The $rules array doesn't actually need to be there. Try setting up the administrator field name as approvedBy instead of approved_by.

billyzduke commented 10 years ago

Ok, it is now "approvedBy" in both columns and edit_fields arrays.

It looks like a column key name was wrong in the actors.php example. "relation" works, "relationship" does not.

janhartigan commented 10 years ago

Did that sort it out? Could you point me to the line that is wrong?

billyzduke commented 10 years ago

Line 26 in the following file:

https://github.com/FrozenNode/Laravel-Administrator/blob/master/examples/app/config/administrator/actors.php

It looks like the "relationship" key is also present in the rest of the example models, apart from directors.php, which has the working "relation." I built my games.php working from your actors.php, so I didn't see this difference until today.

However, using the "relation" key to avoid the previous error trail, I still don't seem to have access to the foreign key data I'm trying to get. My games model config columns array now has the following:

,   'approvedBy' => array
    (   'relation' => 'user'
    ,   'select' => "(:table).username"
    )

...yet the code still seems to be looking for games.username, as I'm getting this error in response:

Column not found: 1054 Unknown column 'games.username'

Thanks for your help! You've addressed a great deal of the concerns I've been attempting to conquer with my own PHP codebase, the administrator is an incredibly potentially useful package.

billyzduke commented 10 years ago

Also wondering how you would recommend I solve the following issue... For a column of type image, an empty value will still attempt to access the output template, and thus produce an erroneous 403 request of the directory location... I tried including a closure that returns a string to "output" based on the presence of a value, but that breaks type constraints and produces an "unexpected function" error...

'image' => array 
(   'output' => '<img src="../images/generated/(:value)"/>'
,   'sortable' => false
)
janhartigan commented 10 years ago

The correct key for this option is relationship not relation. The directors.php file should have relationship. The error you were running into is being caused by something else entirely, and the best way to get to the root of that problem is for you to post your entire eloquent model including the one for the relationship, and the schema for each table.

The output option currently only supports a text string like the one you've supplied, but I really should make it optionally an anonymous function (which would let you work around this problem.

billyzduke commented 10 years ago

I got it... The relationship needed to match the Eloquent model method, not the table.

billyzduke commented 10 years ago

I greatly appreciate your bearing with me... I will continue experimenting with administrator in the coming days, as it is quite promising...

To that end, it seems that the edit_fields boolean "editable" option setting currently has no effect on image type fields. If I set it to false I am still shown the "upload image" and delete image link options in the edit pop-up.