1

I am working on a Laravel 5.4 Project. I am facing an issue. I would like to know more about the issue.

Below code is working.

PostController.php

<?php

namespace App\Http\Controllers;

use DB;
use View;
use App\Model\Book;
use App\Model\Chapter;
use App\Model\Post;

class PostController extends Controller
{
      public function index(Book $book,Chapter $chapter)
      {
            $books = Book::all();
            $posts = Post::get_post($book,$chapter);

            $data = array(
                'posts'     => $posts,
                'books'       => $books,
                'section_id'  => ''
            );

            return View::make('posts')->with($data);
      }
}  

Below code is creating error " Parse error: syntax error, unexpected '$book' (T_VARIABLE), expecting ',' or ')' "

PostController.php

<?php

namespace App\Http\Controllers;

use DB;
use View;
use App\Model\Book;
use App\Model\Chapter;
use App\Model\Post;

class PostController extends Controller
{
      public function index(Book $book,Chapter $chapter)
      {
            $books = Book::all();
            $posts = Post::get_post(Book $book,Chapter $chapter); //issue is in this line

            $data = array(
                'posts'     => $posts,
                'books'       => $books,
                'section_id'  => ''
            );

            return View::make('posts')->with($data);
      }
}

Why this error is coming ?

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
abu abu
  • 6,599
  • 19
  • 74
  • 131

1 Answers1

0

The problem with this line:

$posts = Post::get_post(Book $book,Chapter $chapter); //issue is in this line

is that when you call function you don't type hint types. You should run function like this:

$posts = Post::get_post($book, $chapter);

Yes, you declare get_post function like this:

public static function get_post(Book $book,Chapter $chapter) 
{
  // do whatever you want here
}

but now when you call you don't use Book or Chapter anymore

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291