In my Laravel 8 project, there is a page called Tag List. I want to add two new tags to Create Tag and Edit pages in this Tag List page. As an example, I leave the edit.blade.php below. Lastly, I added the Collection Tag and Brand Tag in the edit.blade.php file. I added these tags here and uploaded them to the page, but when I select one of these new tags from the page and update it, it is not considered valid and still appears in the old tag. I don't know if I need to do something from the controller part for these tags to be valid or if I need to do something else from somewhere else.
edit.blade.php
@extends('admin.layouts.app')
@section('head_scripts')
<script src="{{ asset('js/jscolor/jscolor.min.js') }}"></script>
@endsection
@section('content')
<div class="p-5">
<div class="row justify-content-center">
<div class="col-lg-10">
{{ Form::open(array('url' => 'tags/'.$tag->id,'method' => 'PUT','files' => true)) }}
{{ Form::token() }}
<div class="card">
<div class="card-header row mx-0">
<div class="mr-auto">
Tag
</div>
<div class="ml-auto">
{{ Form::submit('Update',['class' => 'btn btn-success btn-sm py-2 px-4 rounded']) }}
</div>
</div>
<div class="card-body">
<div class="row">
<div class="col-lg-12">
<div class="row">
<div class="col-lg-3">
{{ Form::label('name','Tag Name') }}
{{ Form::text('tags[name]',$tag->name,['required',"class"=>"form-control form-control-sm","placeholder"=>"Name"]) }}
</div>
<div class="col-lg-1">
{{ Form::label('order','Tag Order') }}
{{ Form::text('tags[order]',$tag->order,['required',"class"=>"form-control form-control-sm","placeholder"=>"Name"]) }}
</div>
<div class="col-lg-3">
{{ Form::label('isMainTag','Is Main Tag', 'Collection Tag', 'Brand Tag') }}
<select name="tags[isMainPage]" class="form-control form-control-sm">
<option @if($tag->isMainPage==0) selected @endif value="0">Normal Tag</option>
<option @if($tag->isMainPage==1) selected @endif value="1">Main Feed Tag</option>
<option @if($tag->isMainPage==2) selected @endif value="2">Collection Tag</option>
<option @if($tag->isMainPage==3) selected @endif value="3">Brand Tag</option>
</select>
</div>
<div class="col-lg-2">
{{ Form::label('color','Course Background Color') }}
<input type="text" data-jscolor="{}" value="{{ $tag->color }}" class="color-picker form-control form-control-sm {{$errors->has('color') ? ' border-danger' : ''}}" id="color" name="tags[color]" value="{{old('color')}}">
<small class="form-text text-danger">{!! $errors->first('color') !!}</small>
</div>
<div class="col-lg-1">
@if($tag->color!=null)
<div class="border" style='background:{{$tag->color}};width: 100px;height: 100px' > </div>
@endif
</div>
<div class="col-lg-5">
<div class="col-lg-12">
{{ Form::label('categories','Categories') }}
{{ Form::select('categories[]',$categories->pluck('name','id') ,$tag->categories->pluck('id') , ['id'=>'tags','style'=>'width:100%','class'=>'form-control form-control-sm searchable_dropdown_multiple','multiple' => 'multiple']) }}
</div>
</div>
</div>
</div>
<div class="col-lg-12 my-5">
<div class="row">
<div class="col-lg-3">
{{ Form::label('type','Tag Type') }}
{{ Form::select('fields[type]',['Collection' => 'Collection Tag','Normal' => 'Normal Tag','NonPayout' => 'Non-Payout Tag'],$tag->getField('type') ?? 'Normal',["class"=>"form-control form-control-sm"]) }}
</div>
<div class="col-lg-3">
{{ Form::label('name','Brand Name') }}
{{ Form::text('fields[brand_name]',$tag->getField('brand_name'),["class"=>"form-control form-control-sm","placeholder"=>"Brand Name"]) }}
</div>
<div class="col-lg-3">
{{ Form::label('description','Brand Description') }}
{{ Form::text('fields[brand_description]',$tag->getField('brand_description'),["class"=>"form-control form-control-sm","placeholder"=>"Brand Description"]) }}
</div>
<div class="col-lg-3">
{{ Form::label('logo','Brand Logo') }}
{{ Form::file('file_fields[brand_logo]',["class"=>"form-control form-control-sm",'accept'=>'.png,.jpg,.jpeg']) }}
@if($tag->getField('brand_logo')!=null)
<div class="mt-2 text-center">
<p>Current Logo</p>
<img src="{{ $tag->getField('brand_logo')}}" height="100" width="100">
</div>
@endif
</div>
</div>
</div>
</div>
</div>
</div>
{{ Form::close() }}
</div>
</div>
</div>
@endsection
@section('scripts')
<script>
$(document).ready(function() {
$('body').on('click','.delete-lecture-btn',function () {
var result = confirm('Are You Sure Delete Lecture?');
if(result){
$(this).parents('.lecture_card').remove();
}
})
$('.add_lecture').click(function () {
var timestamp = new Date().getTime();
console.log(timestamp);
html = '';
html += '<div class="row my-2 border p-3 lecture_card">';
html += '<div class="col-lg-12 row mx-0">';
html += '<div class="mr-auto">';
html += '<div class="badge badge-info text-white">New</div>';
html += '</div>';
html += '<div class="ml-auto">';
html += '<button class="delete-lecture-btn btn btn-danger btn-sm">x</button>';
html += '</div>';
html += '</div>';
html += '<div class="col-lg-3">';
html += '<div class="row">';
html += '<div class="col-lg-12">';
html += '<small>Title</small>';
html += '<input type="text" name="lectures_new['+timestamp+'][title]" value="" class="form-control form-control-sm" placeholder="Title" required>';
html += '</div>';
html += '<div class="col-lg-6">';
html += '<small>Order</small>';
html += '<input type="text" name="lectures_new['+timestamp+'][order]" value="" class="form-control form-control-sm" placeholder="Order" required>';
html += '</div>';
html += '<div class="col-lg-6">';
html += '<small>Length (Second)</small>';
html += '<input type="text" name="lectures_new['+timestamp+'][length]" readonly value="" class="form-control form-control-sm" placeholder="Length" required>';
html += '</div>';
html += '</div>';
html += '</div>';
html += '<div class="col-lg-3">';
html += '<small>Description</small>';
html += '<textarea type="text" name="lectures_new['+timestamp+'][description]" rows="8" class="form-control form-control-sm" placeholder="Description" required></textarea>';
html += '</div>';
html += '<div class="col-lg-6">';
html += '<div class="row">';
html += '<div class="col-lg-12">';
html += '<small>New Audio</small><br>';
html += '<input type="file" name="lectures_new['+timestamp+'][new_audio_file]" accept=".mp3,audio/*" class="form-control form-control-sm">';
html += '</div>';
html += '</div>';
html += '</div>';
html += '</div>';
$('.lectures_place').append(html);
});
});
</script>
@endsection
TagController.php
<?php
namespace App\Http\Controllers;
use App\Course;
use App\CourseCategory;
use App\Tag;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Yajra\DataTables\DataTables;
class TagController extends Controller
{
public function index(){
//$courses = Course::all();
return view('admin.tags.index');
}
public function show($tag_id){
$tag = Tag::findOrFail($tag_id);
return view('admin.tags.show',['tag' => $tag]);
}
public function create(){
$categories = CourseCategory::all();
return view('admin.tags.create',['categories' => $categories]);
}
public function store(Request $request){
$tag = Tag::create($request->tags);
$this->setTagCategories($request->categories,$tag->id);
return redirect('/tags');
}
public function update(Request $request,$tag_id){
$tag = Tag::findOrFail($tag_id);
$tag->update($request->tags);
$this->setTagCategories($request->categories,$tag_id);
if(isset($request->fields)){
foreach ($request->fields as $key => $value){
$tag->setField($key,$value);
}
}
if(isset($request->file_fields)){
foreach ($request->file_fields as $key => $value){
$tag->setFileField($key,$value);
}
}
return redirect('/tags');
}
public function edit($tag_id){
$tag = Tag::findOrFail($tag_id);
$categories = CourseCategory::all();
return view('admin.tags.edit',['tag' => $tag,'categories' => $categories]);
}
private function setTagCategories($categories,$tag_id){
$db_data = [];
if($categories!=null)
foreach ($categories as $key => $category){
if($category!=null && $category!='')
$db_data[] = [
'tag_id'=> $tag_id,
'category_id'=> $category,
];
}
if(!empty($db_data)){
DB::table('category_tag_pivots')->where('tag_id','=',$tag_id)->delete();
DB::table('category_tag_pivots')->insert($db_data);
}
}
public function destroy($category_id){
DB::table('course_category_pivots')->where('category_id','=',$category_id)->delete();
DB::table('course_categories')->where('id','=',$category_id)->delete();
}
public function getDataTable()
{
return DataTables::of(Tag::select(['id','name','color','order','isMainPage']))
->addColumn('course_number', function (Tag $tag) {
return count($tag->courses);
})->addColumn('categories', function (Tag $tag) {
return $tag->categories->implode('name',',');
})
->addColumn('action', function($row){
$btn = '<a href="/tags/'.$row['id'].'" class="edit btn btn-info btn-sm">View</a>';
$btn = $btn.'<a href="/tags/'.$row['id'].'/edit" class="edit btn btn-primary btn-sm">Edit</a>';
$btn = $btn.'<a href="javascript:void(0)" data-remote="/tags/'.$row['id'].'" class="edit btn btn-danger btn-sm btn-delete">Del</a>';
return $btn;
})->make(true);
}
}
category_tag_pivots_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoryTagPivotsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('category_tag_pivots', function (Blueprint $table) {
$table->unsignedBigInteger('tag_id');
$table->unsignedBigInteger('category_id');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade')->onUpdate('cascade');
$table->foreign('category_id')->references('id')->on('course_categories')->onDelete('cascade')->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('category_tag_pivots');
}
}