Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fmahadybd/xecom
https://github.com/fmahadybd/xecom
Last synced: 2 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/fmahadybd/xecom
- Owner: fmahadyBD
- Created: 2024-05-04T13:17:43.000Z (9 months ago)
- Default Branch: main
- Last Pushed: 2024-05-11T09:19:27.000Z (9 months ago)
- Last Synced: 2024-05-11T20:56:11.993Z (9 months ago)
- Language: CSS
- Size: 24 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# What I learn from this project
- The Topic's name: \*
* [One-to-One Table Relation ](#one-to-one-table-relation)
* [Subcategory by the Category](#Subcategory-by-the-Category)
* [Globar data by AppProvider](#Globar-data-by-AppProvider)## One-to-One Table Relation
### Overview
In this README, we will discuss the one-to-one table relation between two tables: `Subcategory` and `Category`. Specifically, we will focus on how the `category` function within the `Subcategory` model establishes this relation.
## Relation Description
The relation between the `Subcategory` and `Category` tables is established as a one-to-one relationship. Each `Subcategory` belongs to one `Category`, and each `Category` can have only one associated `Subcategory`.
## Implementation Details
### Relation Setup
To establish the one-to-one relationship, we utilize Laravel's Eloquent ORM. Within the `Subcategory` model, the `category` function is defined to specify the relation.
```php
public function category()
{
return $this->belongsTo('App\Models\Category');
}
```### Usage
When accessing a `Subcategory` instance, you can retrieve its associated `Category` using the `category` function. For example:
```php
$subCategory = Subcategory::find($id);
$category = $subCategory->category;
```### Blade file:
```
@foreach($subcategories as $subCategory)
Subcategory Name: {{ $subCategory->name }}
Category Name: {{ $subCategory->category->name }}
@endforeach
```
This retrieves the `Category` object related to the `$subCategory`.
### Context
- **$subCategory**: This variable represents an instance of the `Subcategory` class, typically used within a loop or retrieved from the database.
- **category**: Within the `Subcategory` model, `category` refers to the function that establishes the relation with the `Category` table.
- **belongsTo()**: This method specifies that the `Subcategory` table has a foreign key referencing the `Category` table.
## Conclusion
In summary, the `category` function within the `Subcategory` model enables the establishment of a one-to-one table relation between the `Subcategory` and `Category` tables in Laravel. By utilizing Eloquent's built-in methods, developers can easily navigate and work with related data in their applications.
## Subcategory by the Category
### Loading Data in Controller
```php
// Load the blade file to load the data
function addProduct()
{
$categories = Category::all();
$SubCategories = SubCategory::all();
return view('admin.product.add-sub-product', [
'SubCategories' => $SubCategories,
'categories' => $categories,
]);
}
```
### Dropdown Menu
```html
Select Category :
Select One
@foreach ($categories as $category)
{{ $category->name }}
@endforeach
Sub Category :
Select One
@foreach ($SubCategories as $SubCategory)
{{ $SubCategory->name }}
@endforeach
```
### JavaScript (yield() in master)
```javascript
@section('admin-js')
function toggleSubCategoryDropdown(enable) {
if (enable) {
$('#subCategoryId').prop('disabled', false);
} else {
$('#subCategoryId').prop('disabled', true);
$('#subCategoryId').val('').change();
}
}
$(document).on("change", '#categoryId', function() {
var categoryId = $(this).val();
if (categoryId) {
toggleSubCategoryDropdown(true);
$.ajax({
url: '/getSubCategory/' + categoryId,
method: 'GET',
dataType: 'json',
success: function(res) {
var options = '<option value="" disabled="" selected>Select One</option>';
$.each(res, function(key, value) {
options += '<option value="' + value.id + '">' + value.name +
'</option>';
});
$('#subCategoryId').html(options);
},
error: function(e) {
console.log(e);
}
});
} else {
toggleSubCategoryDropdown(false);
}
});
$(document).ready(function() {
toggleSubCategoryDropdown(false);
});
@endsection
```
### Route for Asynchronous Call
```php
Route::get('/getSubCategory/{id}',[SubCategoryController::class,'getSubCategory'])->name('getSubCategory');
```
### Controller Function
```php
public function getSubCategory($id) {
$this->subCategory = SubCategory::where('category_id', $id)->get();
return response()->json($this->subCategory);
}
```
## Globar data by AppProvider
### AppProvider Code
```php
public function boot(): void
{
View::composer(['front.includes.header'], function ($view) {
$view->with('categoriesH', Category::where('status', 1)->get());
});
}
```
### Blade File Changes
```html
Category