Support the ongoing development of Laravel.io →
Authorization Blade
Last updated by @ajax30 2 years ago.
0

The problem is after calling the parent index function the child index function is also invoked and the FeaturedPostsController index function does not return the view. If you call the parent::index($request); from the child controller index function, still you won't be able to render the view because after executing the parent::index($request); the control returns back to the caller function.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;

class PostsController extends Controller {
    public function index(Request $request){
        $viewData = $this->loadViewData();
        $viewData['page_title'] = "Posts";
        $viewData['posts'] = $this->posts($request);
        return view('posts', $viewData);
        dd('parent index function');
    }
}


namespace App\Http\Controllers;
use App\Http\Controllers\PostsController;
use Illuminate\Http\Request;

class FeaturedPostsController extends PostsController {
    public function __construct(Request $request) {
        parent::index($request);
    }
    
    public function index(Request $request){
        $viewData['page_title'] = "Featured posts";
        dd('child index function');
    }
}

Update your code with dd() statements and then

  1. run your code once
  2. remove the dd() in the parent controller to know the complete execution cycle.
0
Solution

You can achieve your goal by using a protected $page_title attribute and updating your controllers functions like :

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;

class PostsController extends Controller {

    protected $page_title = 'Posts';
    public function index(Request $request){
        $viewData = $this->loadViewData();
        $viewData['page_title'] = $this->page_title;
        $viewData['posts'] = $this->posts($request);
        return view('posts', $viewData);
    }
}


namespace App\Http\Controllers;
use App\Http\Controllers\PostsController;
use Illuminate\Http\Request;

class FeaturedPostsController extends PostsController {
    public function __construct(Request $request) {
        $this->page_title =  'Featured posts';
        parent::index($request);
    }
}

Hope this helps you!

0
Solution selected by @ajax30

@faisal Please help me with this question too. Thanks! :)

0

Sign in to participate in this thread!

Eventy

Your banner here too?

Razvan ajax30 Joined 2 Oct 2021

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.