Skip to content

Commit

Permalink
added many to many relation ArticlesWithRelationship <-> Tag
Browse files Browse the repository at this point in the history
  • Loading branch information
RichardDominik committed Jul 9, 2020
1 parent e811233 commit ce2f584
Show file tree
Hide file tree
Showing 15 changed files with 167 additions and 19 deletions.
22 changes: 18 additions & 4 deletions app/Http/Controllers/Admin/ArticlesWithRelationshipController.php
Expand Up @@ -9,9 +9,11 @@
use App\Http\Requests\Admin\ArticlesWithRelationship\UpdateArticlesWithRelationship;
use App\Models\ArticlesWithRelationship;
use App\Models\Author;
use App\Models\Tag;
use Brackets\AdminListing\Facades\AdminListing;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\DB;

class ArticlesWithRelationshipController extends Controller
{
Expand Down Expand Up @@ -66,6 +68,7 @@ public function create()

return view('admin.articles-with-relationship.create', [
'authors' => Author::all(),
'tags' => Tag::all(),
]);
}

Expand All @@ -81,9 +84,13 @@ public function store(StoreArticlesWithRelationship $request)
$sanitized = $request->validated();

$sanitized['author_id'] = $request->getAuthorId();
$sanitized['tags'] = $request->getTags();

// Store the ArticlesWithRelationship
$articlesWithRelationship = ArticlesWithRelationship::create($sanitized);
DB::transaction(function () use ($sanitized) {
// Store the ArticlesWithRelationship
$articlesWithRelationship = ArticlesWithRelationship::create($sanitized);
$articlesWithRelationship->tags()->sync($sanitized['tags']);
});

if ($request->ajax()) {
return ['redirect' => url('admin/articles-with-relationships'), 'message' => trans('brackets/admin-ui::admin.operation.succeeded')];
Expand Down Expand Up @@ -117,9 +124,12 @@ public function edit(ArticlesWithRelationship $articlesWithRelationship)
{
$this->authorize('admin.articles-with-relationship.edit', $articlesWithRelationship);

$articlesWithRelationship->load('tags');

return view('admin.articles-with-relationship.edit', [
'articlesWithRelationship' => $articlesWithRelationship,
'authors' => Author::all(),
'tags' => Tag::all(),
]);
}

Expand All @@ -136,9 +146,13 @@ public function update(UpdateArticlesWithRelationship $request, ArticlesWithRela
$sanitized = $request->validated();

$sanitized['author_id'] = $request->getAuthorId();
$sanitized['tags'] = $request->getTags();

// Update changed values ArticlesWithRelationship
$articlesWithRelationship->update($sanitized);
DB::transaction(function () use ($articlesWithRelationship, $sanitized) {
// Update changed values ArticlesWithRelationship
$articlesWithRelationship->update($sanitized);
$articlesWithRelationship->tags()->sync($sanitized['tags']);
});

if ($request->ajax()) {
return ['redirect' => url('admin/articles-with-relationships'), 'message' => trans('brackets/admin-ui::admin.operation.succeeded')];
Expand Down
Expand Up @@ -30,7 +30,8 @@ public function rules()
'published_at' => ['nullable', 'date'],
'enabled' => ['required', 'boolean'],
'author' => ['required'],

'tags' => ['required']

];
}

Expand All @@ -41,4 +42,13 @@ public function getAuthorId()
}
return null;
}

public function getTags(): array
{
if ($this->has('tags')) {
$tags = $this->get('tags');
return array_column($tags, 'id');
}
return [];
}
}
Expand Up @@ -30,6 +30,7 @@ public function rules()
'published_at' => ['nullable', 'date'],
'enabled' => ['sometimes', 'boolean'],
'author' => ['required'],
'tags' => ['required']

];
}
Expand All @@ -41,4 +42,13 @@ public function getAuthorId()
}
return null;
}

public function getTags(): array
{
if ($this->has('tags')) {
$tags = $this->get('tags');
return array_column($tags, 'id');
}
return [];
}
}
17 changes: 11 additions & 6 deletions app/Models/ArticlesWithRelationship.php
Expand Up @@ -14,27 +14,27 @@ class ArticlesWithRelationship extends Model
"author_id",

];

protected $hidden = [

];

protected $dates = [
"published_at",
"created_at",
"updated_at",

];

protected $with = ['author'];

protected $appends = ['resource_url'];

/* ************************ ACCESSOR ************************* */

public function getResourceUrlAttribute()
{
return url('/admin/articles-with-relationships/'.$this->getKey());
return url('/admin/articles-with-relationships/' . $this->getKey());
}

/* ************************ RELATIONS ************************* */
Expand All @@ -43,4 +43,9 @@ public function author()
{
return $this->belongsTo(Author::class);
}

public function tags()
{
return $this->belongsToMany(Tag::class);
}
}
11 changes: 8 additions & 3 deletions app/Models/Tag.php
Expand Up @@ -20,9 +20,7 @@ class Tag extends Model
"updated_at",

];




protected $appends = ['resource_url'];

/* ************************ ACCESSOR ************************* */
Expand All @@ -31,4 +29,11 @@ public function getResourceUrlAttribute()
{
return url('/admin/tags/'.$this->getKey());
}

/* ************************ RELATIONS ************************* */

public function articlesWithRelationships()
{
$this->belongsToMany(ArticlesWithRelationship::class);
}
}
3 changes: 1 addition & 2 deletions database/factories/ModelFactory.php
Expand Up @@ -158,10 +158,9 @@
/** @var \Illuminate\Database\Eloquent\Factory $factory */
$factory->define(App\Models\Tag::class, function (Faker\Generator $faker) {
return [
'name' => $faker->firstName,
'name' => $faker->word,
'created_at' => $faker->dateTime,
'updated_at' => $faker->dateTime,


];
});
@@ -0,0 +1,39 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateArticlesWithRelationShipTagTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('articles_with_relationship_tag', function (Blueprint $table) {
$table->unsignedInteger('articles_with_relationship_id');
$table->foreign('articles_with_relationship_id')
->references('id')
->on('articles_with_relationships')
->onDelete('cascade');
$table->unsignedInteger('tag_id');
$table->foreign('tag_id')
->references('id')
->on('tags')
->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('articles_with_relationship_tag');
}
}
25 changes: 25 additions & 0 deletions database/seeds/ArticlesWithRelationshipSeeder.php
@@ -0,0 +1,25 @@
<?php

use App\Models\ArticlesWithRelationship;
use App\Models\Tag;
use Illuminate\Database\Seeder;

class ArticlesWithRelationshipSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run() : void
{
$tags = Tag::all();
$articlesWithRelationship = ArticlesWithRelationship::all();

$articlesWithRelationship->each(function ($articleWithRelationShip) use ($tags) {
$articleWithRelationShip->tags()->sync(
$tags->random(rand(1, 3))->pluck('id')->toArray()
);
});
}
}
1 change: 1 addition & 0 deletions database/seeds/DatabaseSeeder.php
Expand Up @@ -20,5 +20,6 @@ public function run()
factory(\App\Models\ArticlesWithRelationship::class, 20)->create();
factory(\App\Models\BulkAction::class, 20)->create();
factory(\App\Models\Tag::class, 10)->create();
$this->call(ArticlesWithRelationshipSeeder::class);
}
}
5 changes: 3 additions & 2 deletions public/js/admin.js
Expand Up @@ -118451,15 +118451,16 @@ __webpack_require__.r(__webpack_exports__);

Vue.component('articles-with-relationship-form', {
mixins: [_app_components_Form_AppForm__WEBPACK_IMPORTED_MODULE_0__["default"]],
props: ['authors'],
props: ['authors', 'availableTags'],
data: function data() {
return {
form: {
title: '',
perex: '',
published_at: '',
enabled: false,
author: ''
author: '',
tags: ''
}
};
}
Expand Down
6 changes: 5 additions & 1 deletion resources/js/admin/articles-with-relationship/Form.js
Expand Up @@ -2,7 +2,10 @@ import AppForm from '../app-components/Form/AppForm';

Vue.component('articles-with-relationship-form', {
mixins: [AppForm],
props: ['authors'],
props: [
'authors',
'availableTags'
],
data: function() {
return {
form: {
Expand All @@ -11,6 +14,7 @@ Vue.component('articles-with-relationship-form', {
published_at: '' ,
enabled: false ,
author: '' ,
tags: '',
}
}
}
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en/admin.php
Expand Up @@ -124,6 +124,7 @@
'published_at' => "Published at",
'enabled' => "Enabled",
'author_id' => "Author",
'tags' => "Tags",

],
],
Expand Down
Expand Up @@ -56,3 +56,35 @@ class="col-form-label text-center col-md-4 col-lg-3">{{ trans('admin.post.column

</div>
</div>


<div class="card">
<div class="card-header">
<span><i class="fa fa-tags"></i> {{ trans('admin.articles-with-relationship.columns.tags') }} </span>
</div>

<div class="card-block">
<div class="form-group row align-items-center"
:class="{'has-danger': errors.has('tags'), 'has-success': this.fields.tags && this.fields.tags.valid }">
<label for="author_id"
class="col-form-label text-center col-md-4 col-lg-3">{{ trans('admin.articles-with-relationship.columns.tags') }}</label>
<div class="col-md-8 col-lg-9">

<multiselect
v-model="form.tags"
:options="availableTags"
:multiple="true"
track-by="id"
label="name"
tag-placeholder="{{ __('Select Tags') }}"
placeholder="{{ __('Tags') }}">
</multiselect>

<div v-if="errors.has('tags')" class="form-control-feedback form-text" v-cloak>@{{
errors.first('tags') }}
</div>
</div>
</div>

</div>
</div>
Expand Up @@ -9,6 +9,7 @@
<articles-with-relationship-form
:action="'{{ url('admin/articles-with-relationships') }}'"
:authors="{{$authors->toJson()}}"
:available-tags="{{ $tags->toJson() }}"
v-cloak
inline-template>

Expand Down
Expand Up @@ -10,6 +10,7 @@
:action="'{{ $articlesWithRelationship->resource_url }}'"
:data="{{ $articlesWithRelationship->toJson() }}"
:authors="{{$authors->toJson()}}"
:available-tags="{{ $tags->toJson() }}"
v-cloak
inline-template>

Expand Down

0 comments on commit ce2f584

Please sign in to comment.