Laravel CRUD multi entities, routing - php

There are 3 entities:
Categories
Subcategories
Photos
(3 tables, 3 controllers, 3 models).
We need to implement CRUD operations for each entity. How to implement routing?
For example, to delete a subcategory, you need to make a DELETE request to:
categories / {category_id} / subcategories/{subcategory_id}
In the blade in href or in form action you need to write the following, for example:
{{ route('subcategories.destroy', [$category->id, $subcategory->id])}
But I don't need $category_id to delete a subcategory, just id subcategory is enough.
If then the same thing is implemented for photo, then you have to pass 3 parameters, 2 of which I do not need, but only the id of the photo.
I think I'm going some wrong way, can someone tell me how to implement routing and crud operations for categories and their subcategories?
routes/web.php:
// Category routes
Route::resource('categories', 'CategoryController');
// Subcategories routes
Route::prefix('categories/{category_id}')->group(function (){
Route::resource('subcategories', 'SubcategoryController');
});
SubcategoryController#destroy:
public function destroy($category_id, $subcategory_id)
{
$subcategory = Subcategory::findOrFail($subcategory_id);
$subcategory->delete();
return redirect('/categories/' . $subcategory->category_id);
}
Tables:
categories
subcategories
photos

Related

Add insert limit on many-to-many relation

how do we add a limit to many-to-many relation. Following the example in the documentation let's say we have 3 tables post, category and post_category. Let's say I have a list of posts and on every post a possibility to choose a category.
How can I 'limit' the relation so that there can't be more then 5 categories per post and 100 posts per category?
I have managed to do so using a checking if count(post->books) < 5 on the create action of post_category controller but am looking for a way to do it more Yii friendly, if that exists.
Working on Yii 1.1.14.
You can using custom validation rules. For example in your PostCategory model, add the following rule
public function rules()
{
return [
[['post'], 'validateSize'],
];
}
Then anywhere in the model add validateSize()
public function validateSize($attribute_name,$params){
if(sizeof($this->post->categories) > 4)
$this->addError($attribute_name, Yii::t('post', 'You cant have more than 5 categories per post'));
}
This will give an error when trying to create a new category belonging to a post that already has 4 categories

Laravel Voyager: Dropdown that shows conditional relations

I am using Laravel with Voyager for the back-end.
I made a relationship between Posts model and Categories model.
When adding a new Post, I can choose an according category using a dropdown.
How can I make this dropdown show Categories according to certain conditions? (Let's say, only subcategories)
You can easily filter the shown relationship options by defining a local scope in the foreign model. For example, if you want to only show active entries of categories in a relationship input, create a scope as given in your Category model,
public function scopeSubcategories($query){
return $query->where('parent_id', '!=' , null);
}
Now, go to the BREAD builder and add the following to the relationship options
{
"scope": "subcategories"
}
The value is the name of your scope-method without the word scope.
The value for scopeSubcategories() is subcategories.

How to set up database relation so that a category may belong to multiple parent category

I am currently using Laravel (PHP framework) in order to construct an ecommerce site.
The site will have a lot of categories(for products) of which there is ruffly 100-150 and will probably be more as there will be a backend site to add more.
Some times it will be necessary for a category to appear in more than 1 parent category on the site.
Category Relationships I am trying to achieve:
A category can have many child categories.
A category may have more than one parent category.
I am very confused as to how to set up the second of these two relationships correctly within Laravel.
So my question is:
How do I set up a database structure and Model relations so that a category can belong to many other category without any duplication in the categories table.
I would like to know what tables/columns I need and also what types of relationships need to be set in the models please.
This model seems to work:
I have a table called category_category and relation:
public function parentCategories()
{
return $this->belongsToMany('TottonTimber\Category', 'category_category', 'category_id', 'parent_id');
}
public function childCategories()
{
return $this->belongsToMany('TottonTimber\Category', 'category_category', 'parent_id', 'category_id');
}
Yet this doesn't seem like the correct way of doing it as both are "belongsTo"
In Many to Many to relations. Both models do have a belongs to relation with the other. For example in classic User & Roles Scenerio, User Belongs to Many Roles & Roles Belongs to Many Users.. So as you see 'Belongs to' relationship both sides. Here as you have same model for both ends of your relations, you have to put 'belongsto' for both your relation definations. That is perfectly ok it seems.

CakePHP: HABTM Multiple times

I have the current models:
Categories
SubCategories
Items
Each can have many of the others. For example, an item might belong to subcategory id 1, but the same item might also belong to subcategory id 2. Then subcategory id 1 might belong to category id 1, as well as subcategory id 2 belonging to category id 1.
Currently I have a HABTM relationship between each models, using a table called Categories_Sub_Categories or Items_Sub_Categories to link them. However I'd like to know is there a more efficient 'cake' way of doing this?
Categories and SubCategories could be combined into a single "Category" model and use the Tree Behavior to keep track of which is is the parent/child of each. That also allows you to keep more than 2 levels of Categories without need for changing your code.
Then, you can just do a HABTM between Category and Item.

How to the data available in a 4th column in cakephp?

Currently i'm having a problem. I want to access the data available in the 4th table of my DB.
Db image:
I have the tables in this way: Categories --> Categories_Companies --> Companies --> Affiliates
Like it shows in the image i'm on the categories and in the Categories view (views/categories/view.ctp) i want to show the fields title and url from the affiliates table.
There is another way of doing that without using the this->query?
Regards
You access a table through its model. The Category model is automatically included in the CategoriesController by naming convention. You can include other models by using $uses.
var $uses = array('Category', 'Affiliate');
function view() {
$this->Category->find(…);
$this->Affiliate->find(…);
}
Or, if your models are linked through associations, you can access them through an association:
$this->Category->Company->Affiliate->find(…);
Both examples are equivalent, the first is just more convenient.

Categories