2

I get an error on line 11 of this code in Laravel saying:

syntax error, unexpected '->' (T_OBJECT_OPERATOR)

Here is the code of ClientController:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Title as Title;

class ClientController extends Controller
{
    public function __construct( Title $titles) {
        $this->titles = titles->all();
    }

    public function di() {
        dd($this->titles);
    }
}

The Title:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Title extends ReadOnlyBase
{
    protected $titles_array = ['Mr', 'Mrs', 'Ms', 'Dr', 'Mx'];
}

The ReadOnlyBase:

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class ReadOnlyBase
{
    //
    protected $titles_array = [];

    public function all()
    {
        return $this->titles_array;
    }

    public function get( $id )
    {
        return $this->titles_array[$id];
    }
}

And the web.php:

Route::get('/di', 'ClientController@di');

I wrote the code exactly as a lynda lesson on Laravel, and yet I get an error.

Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • `titles->all();` should be `$titles->all();` – brombeer Jul 24 '18 at 06:17
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Nigel Ren Jul 24 '18 at 06:29

2 Answers2

1

you should use

public function __construct( Title $titles) {
    $this->titles = $titles->all();
}

instead of

public function __construct( Title $titles) {
    $this->titles = titles->all();
}
M Maavia
  • 340
  • 1
  • 9
0

You forgot a $ here:

$this->titles = titles->all();

Change it to:

$this->titles = $titles->all();
SystemGlitch
  • 2,150
  • 12
  • 27