Multiple parameters to crud

Published 3 months ago by dsmI

Hi, I'm having, probably a basic issue for some, problems with my edit crud.

I have this two routes in my web.php.

// Categories
Route::resource('docs', 'CategoryController');

// Articles
Route::resource('docs/{category}/', 'ArticleController');

The categories rout generates the url docs/category_name, by having it I can create categories in separate table.

The second route is for creating articles. I'd like to display it like docs/category_name/article_name.

But then I get the error "Missing required parameters for [Route: store] [URI: docs/{category}]"

This is my edit crud:


public function create($category)
{
    return view('articles.create')
    ->with('category', $category);
}

Still does it not work.

In my articles.edit file I have the following:

{!! Form::open(['route' => 'store']) !!}

I can't find where I'm wrong. Please provide me with some valuable information. Thank you!

arthurvillar

Probably you just need to specify the model like this

public function create(Category $category)
{
    return view('articles.create')
    ->with('category', $category);
}
dsmI
dsmI
3 months ago (7,350 XP)

I did specify the Model, but then "Sorry, the page you are looking for could not be found.". And I am still stuck.

arukomp

the first parameter of Route::resource is the name of the resource, not the url. Try this:

Route::resource('categories', 'CategoriesController');
Route::resource('articles', 'ArticlesController');

then you'll be able to access the routes using the names:

route('articles.show', $article);
...
route('categories.create');
dsmI
dsmI
3 months ago (7,350 XP)

Yes, but to get the url like I listed above, do I need to use a separate Route:get?

arthurvillar

The answer @arukomp gave you is perfect, I didn't pick up on that error. I have a feeling you should not be using resource at all, at least until you get used to how they work. You will have more control if you just write one get() method for each route you want.

Snapey
Snapey
3 months ago (865,465 XP)

You cannot have the two routes as you have shown

// Categories
Route::resource('docs', 'CategoryController');

// Articles
Route::resource('docs/{category}/', 'ArticleController');

because the router cannot differentiate between docs/23 and docs/puppies

All your requests will go to the Category controller and not the to Article.

If you want to use Route::resource then you have to recognise that Article and Category are different resources and should have their own URLs

dsmI
dsmI
3 months ago (7,350 XP)

Okay, I understand. So in short, Route::resource gives the curds it's own URLs. So If I want it to be like a did it above I'll be needing to use Route::get except for put and delete?

Drfraker

@dsml you can type

php artisan route:list 

in the console and see all of the routes in your app. This might help you understand what the resource routes are creating for you.

Snapey
Snapey
3 months ago (865,465 XP)

You would see the problem more clearly if you listed the routes out, but you would possibly still need to change the url schema

  • docs/23 to show document 23
  • docs/puppies to show category 'puppies'

cannot co-exist

  • docs/category/puppies
  • docs/23

can co-exist if they are in that order since category is matched before trying the variable route with the document ID

dsmI
dsmI
3 months ago (7,350 XP)

Yes, I see. So how can I sort my articles by category and link them like docs/main/some_article_name?

I already applied some hasToMany and BelongsTo in both Models.

Snapey
Snapey
3 months ago (865,465 XP)

What data you return has little to do with the URL

You can pretend to use the category

Route::get('/docs/{category/{slug}','[email protected]');

and then in the controller

public function show($category, $slug)
{

    $article = Article::where('slug',$slug)->first();

and then never use the category if you have unique slugs for the articles.

dsmI
dsmI
3 months ago (7,350 XP)

So this two can't co-exist. That's now pretty clear. But if I create a PagesController and make a page called docs, can I then somehow get the collection of all categories and articles there or do I need I helper function for that? Thank you!

Snapey
Snapey
3 months ago (865,465 XP)
  1. Your route calls a controller method.

  2. The method gets the data it needs and passes it to the view

  3. You return the view.

Your controller can get data from any of your models and pass as many variables to the view as required.

dsmI
dsmI
3 months ago (7,350 XP)

I did everything you suggested. I have now to separate controllers for the articles and categories. I'll make them only accessible for admin. Then I have the PageController for the other stuff, but because I use slug for both of the categories and the articles if I want to say docs/{slug}/{slug} I then get the error "Route pattern "/docs/{slug}/{slug}" cannot reference variable name "slug" more than once." Is there a way around? I'm using laravel-sluggable by spatie. Thank you!

Snapey
Snapey
3 months ago (865,465 XP)

use something other than slug

Please sign in or create an account to participate in this conversation.