1

Laravel seems to be throwing an error at line 3 use App\List; but I can't seem to figure out the problem as I'm new to Laravel (perhaps it's a PHP version issue?).

Error: syntax error, unexpected 'List' (T_LIST), expecting identifier (T_STRING)

Here is my PageController Class:

<?php

use App\List;
namespace App\Http\Controllers;

class PageController extends Controller
{
    public function home(){
      $lists = List::all();
      return view('home', compact('lists'));
    }
}

and here is App\List

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class List extends Model
{
    public function items(){
      return $this->hasMany(ListItem::class);
    }
}
Polarize
  • 1,005
  • 2
  • 10
  • 27

2 Answers2

4

You cannot name a class List. See more: List of Reserved Words

Grzegorz Gajda
  • 2,424
  • 2
  • 15
  • 23
  • I was trying for hours to find the solution. it does work .thanks – Abdalla Arbab Mar 04 '17 at 13:30
  • 1
    The reserved word is `list`, and not `List`. I would consider this a language bug. – vhs May 22 '17 at 09:29
  • Came across this issue. That's pretty crappy. 'Public' and 'List' are perfectly usable words in modeling business logic. Why on earth does PHP care if they're used in the class namespace! – mangonights Feb 17 '19 at 13:49
0

You cannot name a class List. I have had this issue, it is a reserved word.

Alex Harris
  • 6,172
  • 2
  • 32
  • 57