wim91 started a new conversation+100 XP
2w ago
I created a category table recursively, meaning that in one table, subcategories reference the parent. This resulted in a high degree of nesting. A product belongs to a child category, and using a recursive method in the model, I retrieve all the nested relationships. I need to get a collection of the last relationship in the chain, but I'm getting "null." Could you tell me how to do this?
Here's the code.
/** class Product extends Model **/
public function category() {
return $this->belongsTo(Category::class, 'category_id');
}
/** class Category extends Model **/
public function parentCategories()
{
return $this->belongsTo(Category::class, 'category_id')->with('parentCategories');
}
/** Controller **/
$product = Product::with('category.parentCategories')->find(2);
$parent_cat = $product->category->parent_categories;
dd($parent_cat); // null
wim91 started a new conversation+100 XP
2w ago
wim91 wrote a reply+100 XP
2mos ago
wim91 wrote a reply+100 XP
2mos ago
wim91 started a new conversation+100 XP
2mos ago
Hello everyone. Here's the task. Four images are added through a form. Each image has a name that is saved in the database. When updating files, I need to delete the previously saved image, upload a new one, and overwrite its name. It's possible that the new file name will match the names of three other files. My question is, how can I check the name of the uploaded file against the names of the other files, each of which is stored in the database? I found an example that only checks one field, and I have four.
$myId = 1;
$validated = $request->validate([
'mydata' => 'unique:table,mydata,' . $myId,
]);
PS: The task requires that file names be changed, which could easily have been solved by assigning a unique name to each newly uploaded file, but alas...
wim91 started a new conversation+100 XP
5mos ago
wim91 started a new conversation+100 XP
5mos ago