-1

Hello I'm trying to create own laravel package it has two Controller resource, which have single controller and model Post

    Route::resource('posts', \vendor\package\Controllers\PostsController::class);
    Route::resource('categories', \vendor\package\Controllers\PostsController::class);

My method in the PostsController is show(Post $post)

If I open link http://localhost/posts/1, attributes element of $post is not empty in the show method.

But when I open link http://localhost/categories/1, attributes element of $post is empty.

How can I get Post data for resource categories?

P.S. difference between posts and categories is value of column post_type in the DB.

Andrii
  • 324
  • 3
  • 16

2 Answers2

1

You can define what the route parameter will be named for the generated routes when using resource routing.

This should be the only change you need to make:

Route::resource(
    'categories', 
    \vendor\package\Controllers\PostsController::class,
    ['parameters' => ['categories' => 'post']]
);

Now the route parameter is post:

GET categories/{post}
GET categories/{post}/edit
...

Laravel 5.6 Docs - Controllers - Naming Resource Route Parameters

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • Looks like works :) . Thanks. I have tried before set this parameter via maethod parameters() and it hasn't worked – Andrii Jul 25 '18 at 10:44
0

Add Route::model('categories', App\Post::class); to the boot() method in your

./app/Providers/RouteServiceProvider.php

source

Quezler
  • 2,376
  • 14
  • 29
  • First I need to make it in custom package. But even I'm inserting to `RouteServiceProvider` not working – Andrii Jul 25 '18 at 08:24
  • can you share the contents of how your `RouteServiceProvider` looks right now? – Quezler Jul 25 '18 at 08:26
  • can you in your `PostsController` add a `dd($post)` inside the `show()` method? then open `http://localhost/posts/1` and `http://localhost/categories/1`, can you screenshot both? – Quezler Jul 25 '18 at 08:34
  • categories - http://prntscr.com/kaqi2p, posts - http://prntscr.com/kaqiil – Andrii Jul 25 '18 at 08:38
  • I have remarked that `connection: null` for categories – Andrii Jul 25 '18 at 08:43
  • hmm, can you share the entire `show` method of your post controller to? – Quezler Jul 25 '18 at 08:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/176697/discussion-between-quezler-and-andrii). – Quezler Jul 25 '18 at 08:46