Ad

How Can I Make 4 Links To A Controller?

- 1 answer

i'm setting up a new project in my laravel

The stuffs i have :

1- PostsController

2- Post model

3- Routes:resource('posts','PostsController')

4- after i login i have a create button for post create

after click on it i have below image :

enter image description here

My desire:

i want without changing whole project or whole routes resources have these :

after i login i want to have 4 boxes like this( i made it )

enter image description here

with this property :

after i click any of them i can create post according to their type

for example if i click on video content i enter to a page with 2 form: like title video and upload video

after i click on text content i enter to page like the image i showed you at the first

it means if i click on video content, resource route take me to create method in postscontroller and create method check if i came from content video link return view (posts.create_video) or if i came from sound content link box return view(posts.content_sound) and etc boxes

how can i do all of that please help me thanks

Ad

Answer

You could structure it as following:

  • [routes/web.php]

    Route::get('posts/create/{type}', '[email protected]')->name('posts.create');
    Route::resource('posts', 'PostController')->except(['create']);
    
  • [PostController]

    class PostController extends Controller
    {
        public function create($type)
        {
            if (in_array($type, ['sound', 'video', 'image', 'text'])) {
                return view("posts.content_{$type}");
            }
            abort(404);
        }
    }
    
Ad
source: stackoverflow.com
Ad