Skip to content

Commit

Permalink
inited
Browse files Browse the repository at this point in the history
  • Loading branch information
zareismail committed Feb 15, 2020
1 parent 3eace54 commit cf94b16
Show file tree
Hide file tree
Showing 26 changed files with 1,839 additions and 24 deletions.
31 changes: 9 additions & 22 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,23 +1,10 @@
/vendor/
node_modules/
npm-debug.log
yarn-error.log

# Laravel 4 specific
bootstrap/compiled.php
app/storage/

# Laravel 5 & Lumen specific
public/storage
public/hot

# Laravel 5 & Lumen specific with changed public path
public_html/storage
public_html/hot

storage/*.key
.env
Homestead.yaml
Homestead.json
/.vagrant
/.idea
/vendor
/node_modules
package-lock.json
composer.phar
composer.lock
phpunit.xml
.phpunit.result.cache
.DS_Store
Thumbs.db
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
version 0.1.0 (2020-02-04)
-----------------------------
### INITED
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 armincms

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
118 changes: 116 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,116 @@
# belongs-to-many
The inline `BelongsToMany` and `MorphedByMany` field for the Laravel Nova
# BelongsToMany
A Laravel Nova field for `simple` and `polymorphic` `ManyToMany` relationships.

##### Table of Contents
* [Features](#features)
* [Install](#install)
* [Simple Usage](#simple-usage)
* [Pivots](#pivots)
* [Duplicate Attachment](#duplicate-attachment)
* [Polymorphic Relation](#polymorphic-relation)


## Features
- Attach polymorphic and non-polymorphic `ManyToMany` relationships in
the creation and update page
- Edit pivot columns when attaching relation
- Attach a source to another resource many times

## Install
```bash
composer require armincms/belongs-to-many
```

## Simple Usage

```
use Armincms\Fields\BelongsToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
BelongsToMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->pivots(),
];
}
```

## Pivots
For customizing the pivot columns when attaching a resource you can use the `pivots` method of the field. then define your custom pivot fields with the `fields` method. now, when attaching a resource; a Modal that contains the pivot fields will be displayed to you.


```
use Armincms\Fields\BelongsToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
BelongsToMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->pivots(),
];
}
```

## Duplicate Attachment
You can use the `duplicate` feature for repetitively attach a resource to another resource. follow the example:



```
use Armincms\Fields\BelongsToMany;
/**
* Get the fields displayed by the resource.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function fields(Request $request)
{
return [
BelongsToMany::make(__("Label"), 'relationName', RelatedResource::class)
->fields(function() {
return [
Text::make('Price')
->rules('required', 'numeric'),
];
})
->duplicate(),
];
}
```
## Polymorphic Relation
Using for the polymorphic relationships is like non-polymorphic.
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "armincms/belongs-to-many",
"description": "A Laravel Nova field.",
"keywords": [
"laravel",
"nova"
],
"license": "MIT",
"require": {
"php": ">=7.1.0"
},
"autoload": {
"psr-4": {
"Armincms\\Fields\\": "src/"
}
},
"extra": {
"laravel": {
"providers": [
"Armincms\\Fields\\FieldServiceProvider"
]
}
},
"config": {
"sort-packages": true
},
"minimum-stability": "dev",
"prefer-stable": true
}
Empty file added dist/css/field.css
Empty file.
1 change: 1 addition & 0 deletions dist/js/field.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions dist/mix-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"/js/field.js": "/js/field.js",
"/css/field.css": "/css/field.css"
}
21 changes: 21 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"private": true,
"scripts": {
"dev": "npm run development",
"development": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch": "cross-env NODE_ENV=development node_modules/webpack/bin/webpack.js --watch --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js",
"watch-poll": "npm run watch -- --watch-poll",
"hot": "cross-env NODE_ENV=development node_modules/webpack-dev-server/bin/webpack-dev-server.js --inline --hot --config=node_modules/laravel-mix/setup/webpack.config.js",
"prod": "npm run production",
"production": "cross-env NODE_ENV=production node_modules/webpack/bin/webpack.js --progress --hide-modules --config=node_modules/laravel-mix/setup/webpack.config.js"
},
"devDependencies": {
"cross-env": "^5.0.0",
"laravel-mix": "^1.0",
"laravel-nova": "^1.0",
"@johmun/vue-tags-input": "2.0.*"
},
"dependencies": {
"vue": "^2.5.0"
}
}
150 changes: 150 additions & 0 deletions resources/js/components/DetailField.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<template>
<panel-item :field="field">
<template slot="value" v-if="field.value && field.value.length">
<span v-if="field.pivots">
<span
class="no-underline font-bold dim text-primary cursor-pointer"
v-for="(resource, index) in field.value"
:key="resource.id"
@click="displayPivots(resource)"
:title="__(':resource Details', { resource: resource.text })"
>
{{ resource.text }} {{ field.value.length - index - 1 ? ' , ' : '' }}
</span>
</span>
<span v-else>
<router-link
v-for="(resource, index) in field.value"
:key="index"
:to="{
name: 'detail',
params: {
resourceName: field.resourceName,
resourceId: resource.id,
},
}"
class="no-underline font-bold dim text-primary"
:title="__('View')"
>
{{ resource.text }} {{ field.value.length - index - 1 ? ' , ' : '' }}
</router-link>
</span>
<modal v-if="field.pivots && display" role="dialog" @modal-close="handleClose">
<loading-view :loading="loading">
<card :class="'w-action-fields'">
<form-heading-field
:field="{
asHtml: true,
value: display.text
}"
class="mb-6 pt-6"
/>

<div class="mb-6 py-3 px-6">
<!-- Pivot Fields -->
<component
v-for="(field, index) in fields"
:key="index"
:is="'detail-' + field.component"
:resource-name="resourceName"
:field="field"
:via-resource="viaResource"
:via-resource-id="viaResourceId"
:via-relationship="viaRelationship"
/>
</div>

<div class="px-6 py-3 flex">
<div class="flex items-center ml-auto">
<button
class="btn btn-link dim cursor-pointer text-80 ml-auto mr-6"
type="button"
@click.prevent="handleClose">{{
__("Close")
}}</button>
<router-link
:to="{
name: 'detail',
params: {
resourceName: field.resourceName,
resourceId: display.id,
},
}"
class="no-underline font-bold dim text-primary"
:title="__('View')"
>
{{ __(':resource Details', { resource: display.text }) }}
</router-link>
</div>
</div>
</card>
</loading-view>
</modal>
</template>
<p v-else slot="value">&mdash;</p>
</panel-item>
</template>

<script>
export default {
props: ['resource', 'resourceName', 'resourceId', 'field'],
data() {
return {
display: false,
fields: [],
}
},
methods: {
displayPivots(resource) {
this.display = resource;
this.loading = false;
this.getPivotFields()
},
/**
* Get all of the available fields for an attachment.
*/
async getPivotFields(resource) {
await Nova.request()
.get(
`/nova-api/armincms/${this.resourceName}/pivot-fields/${this.field.resourceName}`,
{
params: {
resourceId: this.resourceId,
relatedId: this.display.id,
pivotId: this.display.pivotId,
},
}
)
.then(({ data }) => {
this.fields = data
_.each(this.fields, field => {
field.fill = () => ''
})
})
},
/**
* Resolve the component name.
*/
resolveComponentName(field) {
return field.prefixComponent
? 'detail-' + field.component
: field.component
},
handleClose() {
this.display = null
this.$emit('close')
},
},
computed: {
},
}
</script>
Loading

0 comments on commit cf94b16

Please sign in to comment.