mkeremcansev
Repository
public function postsAdd($data)
{
try {
$general_key = Str::random(24);
if($data['post_type'] == '0' || $data['post_type'] == '2'){
$image = $this->imageAdd('posts',$data['image']);
}else{
if(isset($data['image'])){
$image = $this->imageAdd('posts',$data['image']);
}else{
$id = $this->getYoutubeVideoIdFromUrl($data['video_link']);
$image ='https://img.youtube.com/vi/'.$id.'/maxresdefault.jpg';
}
}
foreach($this->languages() as $language){
$keywordsData = $data['keywords_'.$language->short_name];
$keywords = [];
if (!empty($keywordsData)) {
$keywordsArray = explode(',', $keywordsData);
foreach ($keywordsArray as $keyword) {
$value = trim($keyword);
$keywords[] = ['value' => $value];
}
}
$posts = DB::table('posts')->insert([
'lang_id' => $language->id,
'general_key' => $general_key,
'category_general_key' => $data['category_az'],
'user_id' => Auth::id(),
'title' => $data['title_'.$language->short_name],
'content' => $data['content_'.$language->short_name],
'slug' =>Str::slug($data['title_'.$language->short_name]),
'image' => $image,
'seo_title' => $data['title_'.$language->short_name],
'seo_description' => $data['seo_description_'.$language->short_name],
'keywords' => json_encode($keywords),
'post_type' => $data['post_type'],
'video_link' => $data['video_link'],
'slider_content' => $data['slider_content_'.$language->short_name],
'publish_date' => $data['publish_date_'.$language->short_name],
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
]);
}
return true;
} catch (\Throwable $th) {
return false;
}
}
<form action="{{route('postsAdd')}}" method="POST" enctype="multipart/form-data">
@csrf
<div class="tab-content" id="tabs-tabContent">
<div class="col-lg-3">
<div class="">
<label for="name" class="form-label">Posts image</label>
<div class="upload-image">
<input type='file' name="image" class="imgInp" data-id='img'/>
</div>
<br>
<img id="img" class="imgInpVal" src="{{asset('assets/images/avatar.jpg')}}" alt="your image" height="150"/>
</div>
</div>
<div class="elvin grid grid-cols-2">
<div class="pt-4">
<label for="category_main"> Main Categories</label>
<select class="form-control w-full" onchange="SelectBox(this.value,'/posts/get-sub-catagories')" id="category_main" name="category_main">
<option selected="selected" disabled="disabled" value="none" class="py-1 inline-block font-Inter font-normal text-sm text-slate-600">Select category</option>
@foreach ($mainCategories['az'] as $mainCategory)
<option value="{{ $mainCategory->general_key }}" {{ old('category_main') == $mainCategory->general_key ? 'selected' : '' }}>{{ $mainCategory->title }}</option>
@endforeach
</select>
</div>
<div class="pt-4" id="subCatagoriesDiv" >
<label for="category_main"> Sub Categories</label>
<select class="form-control w-full" id="subCatagories" name="category_az">
<option disabled selected>Selecet sub catagory</option>
</select>
</div>
<div class="pt-4">
<label for="basicSelect" class="form-label">Post Type</label>
<select name="post_type" id="basicSelect" class="form-control w-full">
<option selected="selected" disabled="disabled" value="none" class="py-1 inline-block font-Inter font-normal text-sm text-slate-600">Select post type</option>
<option value="0" {{ old('post_type') == "0" ? 'selected' : '' }} class="py-1 inline-block font-Inter font-normal text-sm text-slate-600">Text Post</option>
<option value="1" {{ old('post_type') == "1" ? 'selected' : '' }} class="py-1 inline-block font-Inter font-normal text-sm text-slate-600">Video Post</option>
<option value="2" {{ old('post_type') == "2" ? 'selected' : '' }} class="py-1 inline-block font-Inter font-normal text-sm text-slate-600">Podcast</option>
</select>
</div>
<div class="input-area" id="videoLinkArea" style="display: none">
<label for="video_link" class="form-label">Video link</label>
<input id="video_link_{{$lang->short_name}}" type="url" name="video_link" value="{{old('video_link')}}" class="form-control" placeholder="Video link" />
</div>
</div>
@foreach($languages as $key => $lang)
<div class="tab-pane fade {{$key == 0 ? 'show active' : ''}}" id="tabs-{{$lang->short_name}}" role="tabpanel" aria-labelledby="tabs-{{$lang->short_name}}-tab">
<div class="card-text h-full my-6 space-y-4">
<div class="elvin grid grid-cols-2">
<div class="input-area">
<label for="title_{{$lang->short_name}}" class="form-label">Post title</label>
<input id="title_{{$lang->short_name}}" type="text" name="title_{{$lang->short_name}}" value="{{old('title_'.$lang->short_name)}}" class="form-control" placeholder="Post title" />
</div>
<div class="input-area">
<label for="publish_date_{{$lang->short_name}}" class="form-label">Post Publish Date</label>
<input id="publish_date_{{$lang->short_name}}" type="datetime-local" name="publish_date_{{$lang->short_name}}" class="form-control" placeholder="Post Publish Date" />
</div>
</div>
<div class="input-area">
<label for="content_{{$lang->short_name}}" class="form-label">Post content</label>
<textarea id="content_{{$lang->short_name}}" name="content_{{$lang->short_name}}" rows="5" class="form-control" placeholder="Post Content">{{old('content_'.$lang->short_name)}}</textarea>
</div>
<div class="elvin grid grid-cols-2">
<div class="input-area">
<label for="seo_description_{{$lang->short_name}}" class="form-label">Post Seo Decription</label>
<textarea id="seo_description_{{$lang->short_name}}" name="seo_description_{{$lang->short_name}}" rows="5" class="form-control" placeholder="Post seo_description">{{old('seo_description_'.$lang->short_name)}}</textarea>
</div>
<div class="input-area">
<label for="slider_content_{{$lang->short_name}}" class="form-label">Post Slider Content</label>
<textarea id="slider_content_{{$lang->short_name}}" name="slider_content_{{$lang->short_name}}" rows="5" class="form-control" placeholder="Post slider content">{{old('slider_content_'.$lang->short_name)}}</textarea>
</div>
</div>
<div class="input-area">
<label for="keywords_{{$lang->short_name}}" class="form-label">Post keywords</label>
<input type="text" name="keywords_{{ $lang->short_name }}" class="form-control" id="keywords_{{ $lang->short_name }}" value="{{ old('keywords_'.$lang->short_name) }}" data-role="tagsinput" placeholder="Enter keywords" multiple>
</div>
</div>
</div>
@endforeach
<button type="submit" class="btn inline-flex justify-center btn-outline-info">Add</button>
</div>
</form>