Undefined variable: categories in Blade Laravel 6.4.0 - php

I am trying to show my categories on the site map page with the controller below.
CategoryController.php
class CategoriesController extends Controller
{
public function create()
{
$categories = Category
::orderBy('name','desc')
->where('parent_id', NULL)
->get();
return view('admin.category.create', compact('categories'));
}
}
The following is the part of my Blade file where I use the categories variable template.
create.blade.php
<div class="form-group">
<label for="exampleInputPassword1">Parent Category</label>
<select name="parent_id" class="form-control">
#foreach ($main_categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>
I use every way to get variable and passing but none the didn't work do you have any suggestion?

You've got two options here.
Option A:
You named the collection categories in your Controller and pass it to the view as such. To access it in your view, you need to reference it by the same name.
Change this:
// Your view is looking for a collection titled `main_categories`, which does not exist
#foreach ($main_categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
To this:
#foreach ($categories as $c)
<option value="{{ $c->id }}">{{ $c->name }}</option>
#endforeach
Option B:
Pass the data back using main_categories as the collection name
Change this:
return view('admin.category.create', compact('categories'));
To this:
return view('admin.category.create', [
'main_categories' => $categories
]);
You can read up more on passing data to views here.

To be more clear Ill break your code as below and add some details
class CategoriesController extends Controller
{
public function create()
{
$categories = Category
::orderBy('name','desc')
->where('parent_id', NULL)
->get();
// Your are passing variable but I change it as below to be more clear
// return view('admin.category.create', compact('categories'));
//now you are passing the value to the view
return view('admin.category.create', ['categories' => $categories]);
}
}
Now lets catch it in the view. Note that now $categories available in the view. If you pass ['A' => $categories] then view has $A variable so you should call for the relevant variable that you define in the controller.
<div class="form-group">
<label for="exampleInputPassword1">Parent Category</label>
<select name="parent_id" class="form-control">
**{{-- In here you should pass $categories --}}**
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->name }}</option>
#endforeach
</select>
</div>

Related

Determine selected options based on many-to-many relationship

I have three tables: products, categories and category_product.
In my edit form page I'm displaying the categories and subcategories values in a select box.
The chosen category options are saved in the pivot table category_product. This table has product_id and category_id.
Here's my code.
Product model
public function category() {
return $this->belongsToMany('App\Models\Category');
}
Category model:
public function products()
{
return $this->belongsToMany(Product::class);
}
edit product page view:
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}">{{ $parentCategory->name }}</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>
subcats-select view
#foreach($subcategories as $subcategory)
<option value="{{ $subcategory->id }}">---{{ $subcategory->name }}</option>
#if(count($subcategory->subcategory))
#include('includes.subcats-select',['subcategories' => $subcategory->subcategory])
#endif
</div>
#endforeach
How can I put "selected" attribute to the chosen category options, so when I edit the page to apply the changes properly?
You can get pivot table columns the following way:
Product model
public function category() {
return $this->belongsToMany('App\Models\Category')->withPivot('selected');
}
edit product page View:
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}" selected="{{ $parentCategory->pivot->selected }}">
{{ $parentCategory->name }}
</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>
EDIT: While the above answer might be helpful for others, it doesn't answer the question. So here's my second try after some clarification in the chat.
<select name="category_id[]" class="form-control" multiple size = 10>
#foreach ($parentCategories as $parentCategory)
<option value="{{ $parentCategory->id }}"{{ $product->category->contains($parentCategory->id) ? 'selected' : '' }}>
{{ $parentCategory->name }}
</option>
#if(count($parentCategory->subcategory))
#include('includes.subcats-select',['subcategories' => $parentCategory->subcategory])
#endif
#endforeach
</select>

How can I get selected value in select dom element [Laravel]

Can someone tell me how can I get selected value when I try to edit post in Laravel project.
So here are parts of code
Post.php:
public function category(){
return $this->belongsTo('App\Category');
}
Category.php:
public function posts(){
return $this->hasMany('App\Post');
}
PostController.php:
$post = Post::findOrFail($id);
$categories = Category::all();
$tags = Tag::all();
return view('admin.posts.edit', ['post' => $post, 'categories' => $categories, 'tags' => $tags]);
posts/edit.blade.php:
<div class="form-group">
<label for="category_id">Selecet category</label>
<select name="category_id" id="category_id" class="form-control">
<option disabled>List of available post categories</option>
#foreach($categories as $category)
<option value="{{ $category->id }}" {{$category->id == $category->id ? 'selected' : '' }}>{{ $category->name }}</option>
#endforeach
</select>
</div>
Like #Hardood mentioned. $category->id == $category->id will always return true. In fact, if you just want to get the value from form submission. You can just remove that. HTML will handle selected value itself.
You can simply use $request->category_name in your backend.
Problem solved with this part of code (for future users who will read this :) )
#foreach($categories as $category)
<option value="{{ $category->id }}"
#if($post->category->id == $category->id)
selected
#endif
>{{ $category->name }}</option>
#endforeach

Trying to get property 'service_type' of non-object in laravel it is not an array

I am getting error Trying to get property 'service_type' of non-object in laravel
controller
$services= Service::pluck('service_type', 'service_id');
return view('package', compact('services'));
View:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($services as $service )
<option value="{{ $service->service_id }}">{{ $service->service_type }}</option>
#endforeach
</select>
pluck is not working as expected. Tested with several process and it does not work. use select with get or simply get.
$services= Service::get(['service_type', 'service_id']);
$services= Service::select('service_type', 'service_id')->get();
//use anyone from the above
And in view:
#foreach ($services as $service )
<option value="{{ $service->service_id }}">{{ $service->service_type }}</option>
#endforeach
Your $services variable is not an object, hence you can access it's properties with object notation ->. To display them, alter your view to this:
<select class="form-control" name="service_type" id="service_type" data-parsley-required="true">
#foreach ($services as $service )
<option value="{{ $service['service_id'] }}">{{ $service['service_type'] }}</option>
#endforeach
</select>

laravel select box values showing

I made a admin panel with laravel 5.4 . I want to show my category list using category table in products items edit view page. This is my product items edit page controller.
public function edit($id)
{
$item = Item::findOrFail($id);
//$sub_cat = SubCat::all();
$sub_cat = SubCat::with('category')->get();
return view('admin.items.edit', compact(['item', 'sub_cat']));
}
And this is my product items edit page view selection box values show
<div class="form-group">
<label>Main Category</label>
<select class="form-control" id="main_category" name="main_category">
#if(!empty($sub_cat))
#foreach ($sub_cat as $pages)
<option value="{{ $item->id }}">{{ $pages->name }}</option>
#endforeach
#endif
</select>
</div>
I used a model relationship like this
public function category(){
return $this->belongsTo('App\SubCat');
}
How i show my saved category name with other category names into my selection box.
first of all get all the categories like this
public function edit($id)
{
$item = Item::findOrFail($id);
$sub_cat = SubCat::all(); //get all the categories
return view('admin.items.edit', compact(['item', 'sub_cat']));
}
in the blade file run loop on $sub_cat collection and replace $item->id to $pages->id
<div class="form-group">
<label>Main Category</label>
<select class="form-control" id="main_category" name="main_category">
#if(!empty($sub_cat))
#foreach ($sub_cat as $pages) //loop on $sub_cat
<option value="{{ $pages->id }}" {{ $item->category_id == $pages->id ? 'selected="selected"' : '' }}>{{ $pages->name }}</option>
#endforeach
#endif
</select>
</div>

Create select menu with nested categories in Laravel 5

Having gotten help from the this question I asked earlier
Create a nested lists of categories in Laravel 5
was able to loop through and display my nested categories in a tree-like structure.
Now I want to be able to edit each categories, so I created an edit link which I then pass category id to the link, which is in turn used in my controller to display that particular category from the categories table.
But I keep getting this error
ErrorException in 775837465f5ab64876c1dc1677878595 line 46: Trying to get property of non-object (View: /resources/views/backend/categories/edit.blade.php)
Here is my current controller:
public function edit($id)
{
$categories = Category::find($id);
//print_r($categories);
return view('backend.categories.edit')->with('categories', $categories);
}
And this is how I implemented it in my view file:
<select name="parent_id" class="form-control">
<option value="" selected disabled style="display:none">choose parent category</option>
#foreach ($categories as $category)
<option value="{{ $category->id }}" placeholder="choose parent category">{{ $child->name }}</option>
#endforeach
</select>
you are calling $child->name without child having been defined. you need to have a nested loop through $category->children if you want to access their properties. Where you have $child, you need to put $category
#foreach ($categories as $category)
<option value="{{ $category->id }}" placeholder="choose parent category">{{ $category->name }}</option>
#endforeach

Categories