I have an edit page where the data of a product is passed, on this page I have a select box for the category change, in it I need to insert all the categories registered in my database but only shows the category related to the product of the page.
Select Box of Edit view
<select class="selectpicker" data-live-search="true">
#foreach($produtos->categoria as $categoria)
<option data-tokens="{{$categoria->erp_name}}" value="{{$categoria->erp_categoryid}}">{{$categoria->erp_name}}</option>
#endforeach
</select>
ProdutosController
public function edit($id)
{
$produtos = Produtos::find($id);
return view('functions.edit')->with('produtos', $produtos);
}
Routes
Route::get('/product/update/{id}', 'ProdutosController#edit')->name("product::updateGet");
Route::post('/product/update/{id}', 'ProdutosController#update')->name("product::updatePost");
Any suggestions?
The problem is you are looping through the categories of that product, not all categories. Here is a solution:
public function edit($id)
{
$produtos = Produtos::find($id);
$categories = Category::all();
return view('functions.edit', compact('produtos', 'categories'));
}
then in your view:
<select class="selectpicker" data-live-search="true">
#foreach($categories as $categoria)
<option data-tokens="{{$categoria->erp_name}}" value="{{$categoria->erp_categoryid}}">{{$categoria->erp_name}}</option>
#endforeach
</select>
Related
This is Ahmad Raza.
I'm working on E-commerce Project. I am trying to get product attributes on Product details page where user can select attributes before adding to cart.
I have two color attributes of single product in my database table. But I want to show only one color in my select box.
Product Attributes Table
Schema::create('product_attributes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('product_id');
$table->string('sku');
$table->string('size');
$table->string('color');
$table->string('price');
$table->string('stock');
$table->timestamps();
});
Relation
public function attributes()
{
return $this->hasmany('App\Models\ProductAttributes', 'product_id');
}
Route
Route::get('/view-product-details/{id}', [ShopController::class, 'view_product_details']);
Function - Sending Attributes to blade file
$product_attributes = ProductAttributes::where(['product_id' => $id])->get();
Receiving Color Attributes in select box
<select class="custom-select" selected id="inputGroupSelect01" name="color">
#foreach ($product_attributes as $color)
<option value="{{$color->color}}"name="color">
{{$color->color}}
</option>
#endforeach
</select>
My Output
I know this is not looking fine. I want to show only one black color here, but I can't.
Please help me to figure out the problem and guide me how can I resolve this.
You have two entries for black color. so show options with more detail like small-002 Black and medium-002 Black
Try with this code
<select class="custom-select" selected id="inputGroupSelect01" name="color">
#foreach ($product_attributes as $color)
<option value="{{ $color->id }}" name="color">
{{$color->sku .' '.$color->color}}
</option>
#endforeach
</select>
If you want to make the select consists of only the color options you can use the groupBy() while retrieving the product attributes see the code below:
$product_attributes = ProductAttributes::select("color")->where(['product_id' => $id])->groupBy("color")->get();
And then get the data the same as you did.
<select class="custom-select" selected id="inputGroupSelect01" name="color">
#foreach ($product_attributes as $color)
<option value="{{$color->color}}"name="color">
{{$color->color}}
</option>
#endforeach
</select>
I need help with my dropdown. Currently the dropdown display all options from database. However, what I want it to display option based on the user id only. For example, this form is for Urban Seafood, suppose the dropdown will only display Urban Seafood option instead of showing all options. Below is the controller and view blade. Thank you.
Adjustment Controller:
public function create() {
$restorants = Restorant::where(['active' => 1])->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
View:
<select class="form-control select2" name="restorant_id">
<option disabled selected value> -- {{ __('Select an option') }} -- </option>
#foreach ($restorants as $restorant)
<option <?php if(isset($_GET['restorant_id'])&&$_GET['restorant_id'].""==$restorant->id.""){echo "selected";} ?> value="{{ $restorant->id }}">{{$restorant->name}}</option>
#endforeach
</select>
You need the Auth facade
use Illuminate\Support\Facades\Auth;
// ...
public function create() {
$restorants = Restorant::where('active', 1)->where('user_id', Auth::id())->get();
return view('ingredient::adjustments.create', compact('restorants'));
}
I tried but it duplicated products, I wish to save one product under many category for one product_id.
Here my View Create (Product)
<div class="form-group row" id="category">
<label class="col-md-3 col-from-label">{{translate('Category')}} <span class="text-danger">*</span></label>
<div class="col-md-8">
<select class="form-control ml-selectpicker" name="category_id" id="category_id" data-live-search="true" required>
#foreach ($categories as $category)
<option value="{{ $category->id }}">{{ $category->getTranslation('name') }}</option>
#foreach ($category->childrenCategories as $childCategory)
#include('categories.child_category', ['child_category' => $childCategory])
#endforeach
#endforeach
</select>
</div>
</div>
And Here My control ( Create product )
/**
* Show the form for creating a new resource.
*
* #return \Illuminate\Http\Response
*/
public function create()
{
$categories = Category::where('parent_id', 0)
->where('digital', 0)
->with('childrenCategories')
->get();
return view('backend.product.products.create', compact('categories'));
}
And for save
$product->category_id = $request->category_id;
Here My model:
public function category()
{
return $this->belongsTo(Category::class);
}
I believe that there function like
$product->Categories()->sync([1, 3, 4]);
if You want to store a product under many categories then. You have to change Your Relationship to Many-to-many(a products can have many categories and a categories have many products).
So You have to create Three Tables
Products- to store products
categories- to store categories
category_products- pivot table
Then Your Models Will be Like below
Products Model
public function categories(){
return $this->belongsToMany(Category::class);
}
Category Model
public function products(){
return $this->belongsToMany(Product::class);
}
Then You can Simply use the function sync() to store a products under many categories
Many to many relationships in laravel
I have a user table and position table.
Position Table contains
Schema::create('positions', function (Blueprint $table) {
$table->id();
$table->string('name_position');
$table->boolean('is_head');
$table->string('name_department');
$table->boolean('is_top');
$table->string('top_name_department');
$table->string('position_report');
$table->integer('assigned_emp_id');
$table->timestamps();
Positions Controller contains
public function createorg()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
return view('positions.createorg')->with('positions',$user->positions);
}
Blade contains
<select id="name_department" name="name_department">
<option value="0">Please select a department</option>
#foreach($positions as $row)
{
<option value="name_department">{{$row->name_department}}</option>
}
#endforeach
</select>
My FE is showing
<select id="name_department" name="name_department">
<option value="0">Please select a department</option>
{
<option value="name_department">Company</option>
}
{
<option value="name_department">Finance</option>
}
{
<option value="name_department">Finance</option>
}
</select>
So my problem here is I do not want to show duplicate departments since the department is only one. But in my position table, I have multiple data entry with the same department. How can I achieve it?
You can create another variable with the department names from the $user->positions and pass it along to the view
public function createorg()
{
$user_id = auth()->user()->id;
$user = User::find($user_id);
$departments = $user->positions->pluck('name_department')->unique();
return view('positions.createorg', [
'positions' => $user->positions,
'departments' => $departments]
);
}
Then use $departments in blade view to populate select options
<select id="name_department" name="name_department">
<option value="0">Please select a department</option>
#foreach($departments as $department)
<option value="name_department">{{$department}}</option>
#endforeach
</select>
Or you can do it in blade view directly without passing $departments from controller
<select id="name_department" name="name_department">
<option value="0">Please select a department</option>
#foreach($positions->pluck('name_department')->unique() as $department)
<option value="name_department">{{$department}}</option>
#endforeach
</select>
Right so on my website, registered users can create posts and like other peoples posts. When displaying all the posts i have options where the users can sort by title, date and like count. I have done the title and date but im having trouble with sorting by like count. What would be the best way to implement this feature into my system.
This is my post controller method for retrieving and ordering the posts:
public function getEvents(Request $request){
if($request['sort'] == "created_at"){
$posts = Post::orderBy('created_at', 'desc')->get();
}
elseif($request['sort'] == "title"){
$posts = Post::orderBy('title', 'desc')->get();
}
elseif($request['sort'] == "like"){
//how would I go about ordering by like count here
}
return view ('eventspage', ['posts' => $posts]);
}
Here is my view where users select what to order by:
<form action="{{ route('events') }}">
<select name="sort" onchange="this.form.submit()" class="form-control">
<option value="">Sort By</option>
<option value="created_at">Date</option>
<option value="title">Title</option>
<option value="like">Likes</option>
<input type="hidden" name="formName" value="sort">
</select>
</form>
Use this:
$posts = Post::withCount('likes')->orderByDesc('likes_count')->get();
This requires a likes relationship in the Post model:
public function likes() {
return $this->hasMany(Like::class);
}