Pregunta

I'm trying to pass an array of database entries to a view using Eloquent. I'm new to MVC development. The code is as follows:

School.php (model)

class School extends Eloquent 
{
 protected $table = 'Schools';
 public $timestamps = false; 
 protected $softDelete = false;

 public function school()   
 {
    return $this->hasOne('School');
 }

}

SchoolController.php (controller)

class SchoolController extends BaseController 
{
    public $restful = true;

    public function getIndex()
    {
        return View::make('schools')
            ->with('schools', School::all()); 
    }
}

schools.blade.php (View)

@extends('layouts.schools')

@section('school-search')

<ul>
@foreach($schools as $school)
    <li>{{ $school->name }}</li>
@endforeach
</ul>
@stop

However, I'm getting a white screen of death when I load the page. The view works fine when I comment out the "->with('schools', School::all())" part, so I think it has to do with Laravel failing to access the School object. I've googled/stackoverflowed a bit and can't find anyone with a similar problem.

Thanks!

edit: Some relevant info: I have had this problem before, where it would whitescreen anything I routed to, however I corrected that by changing app/storage permissions. Changing permissions does not seem to help this whitescreen problem.

Laravel seems to be error reporting just fine. When there is something that doesn't make sense in my code, I get an error.

We are in development environment.

edit:

when I run php artisan serve, it gives an error "This php binary is not version 5.4 or greater" Could this be related?

edit:

I've updated to PHP 5.4. Still whitescreening. Here is the latest in my error log.

#0 /var/www/playground/vendor/laravel/framework/src/Illuminate/Foundation/Console/ServeCommand.php(29): Illuminate\Foundation\Console\ServeCommand->checkPhpVersion()
#1 /var/www/playground/vendor/laravel/framework/src/Illuminate/Console/Command.php(108): Illuminate\Foundation\Console\ServeCommand->fire()
#2 /var/www/playground/vendor/symfony/console/Symfony/Component/Console/Command/Command.php(241): Illuminate\Console\Command->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#3 /var/www/playground/vendor/laravel/framework/src/Illuminate/Console/Command.php(96): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#4 /var/www/playground/vendor/symfony/console/Symfony/Component/Console/Application.php(892): Illuminate\Console\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#5 /var/www/playground/vendor/symfony/console/Symfony/Component/Console/Application.php(191): Symfony\Component\Console\Application->doRunCommand(Object(Illuminate\Foundation\Console\ServeCommand), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#6 /var/www/playground/vendor/symfony/console/Symfony/Component/Console/Application.php(121): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
#7 /var/www/playground/artisan(59): Symfony\Component\Console\Application->run()
#8 {main} [] []
¿Fue útil?

Solución

class School extends Eloquent 
{
 protected $table = 'Schools';
 public $timestamps = false; 
 protected $softDelete = false;

 public function school()   
 {
    return $this->hasOne('School');
 }

}

You have a School object which has a one to one relationship with another school object? You also haven't defined the foreign key to the relationship

I think that laravel is just recursively loading school object after school object and then running out of memory before it can build an error. You can test this by commenting out the entire school() function and seeing what happens.

Another question can you access you apache error log?

Otros consejos

The problem is with your School.php's relationship defination.

public function school()
{
   return $this->hasOne('School');
}

By this you are telling laravel that this model has a one-to-one / one-to-many relationship with itself and this causes a recursive loop when School::all() is called. Once the loop exceeds the max allowed memory size as set in php.ini, it will return an empty response. Thats why you are getting a blank screen.

This relationship does not seem logical to me but if the schools are hierarchical, you should change the method name school() to something like parent() to avoid confusing laravel. Eg..

public function parent()
{
   return $this->hasOne('School', 'parent_id');
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top