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
Related
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>
simply in my controller i have this collections:
$category = Category::with('users')->find($id);
$users = User::with('roles')->get();
and in front end i try to check which users in $users is into $category->users, like with:
in Category model which that belong to User i have one or multiple stored users and i would like checked html option if each $category has user which that is into $user
<select class="form-control multiselect-filtering" multiple="multiple" name="users[]">
#foreach ($category->users as $guser)
#foreach ($users as $user)
<option value="{{$guser->id}}"
#if ($guser->id == $user->id)
selected="selected"
#else
''
#endif
>
{{$user->username}}
</option>
#endforeach
#endforeach
</select>
this code and compare works, but i have multiple <option> which they are maybe selected or not. how can i solve this issue?
You can loop over the collection which is in the $users variable, and use the contains method to check if the current user id in the loop is contained in the $category->users collection.
#foreach ($users as $user)
<option value="{{$user->id}}"
#if ( $category->users ->contains('id', $user->id))
selected="selected"
#endif
>
{{$user->username}}
</option>
#endforeach
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>
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>
I'm using shipping system where will calculate shipping price.
this is what i get in my blade currently:
As you see I get id of state and then name of state.
here is my function:
public function index() {
$data = RajaOngkir::Provinsi()->all();
return view('welcome', compact('data'));
}
and this is my blade code:
#foreach($data as $sss)
#foreach($sss as $numbers)
{{ $numbers }} <br>
#endforeach
#endforeach
here is {{$data}} dump info:
what I want is to getting id and name separately so I can use it
in input.
The foreach will loop through anyhow and pull the relevant info. You just then show it within your blade like so:
#foreach ($data as $info)
ID: {{ $info->province_id }} <br />
Name: {{ $info->province }}
#endforeach
Updated:
From my re read you want it as a form drop down select so you can calculate shipping fee's therefore you'd simply do:
<select name="province" id="">
<option value="">Select</option> -> This will be the default select option
#foreach ($data as $info)
<option value="{{ $info->province_id }}">{{ $info->province }}</option>
#endforeach
</select>