Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alaa service crud #11

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions app/Http/Controllers/Admin/ServiceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Models\Service;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Stevebauman\Purify\Facades\Purify;


class ServiceController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$services = Service::paginate(10);
return view('admin.services.index', ['services' => $services]);
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('admin.services.create');
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{

$validation = $request->validate([
'name_en' => 'required|min:3',
'name_ar' => 'required|min:3',
'description_en' => 'required',
'description_ar' => 'required',
'featured_image' => 'required|file|image',
]);
$validation['featured_image'] = $request->featured_image->store('public/images');
// please add purification here like Admin/PostController@store
$service = Service::create($validation);


return redirect()->route('admin.services.index');
}

/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show(Service $service)
{
return view('admin.services.show', ['service' => $service]);
}

/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit(Service $service)
{
return view('admin.services.edit', ['service' => $service]);
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Service $service, Request $request)
{
$validation = $request->validate([
'name_en' => 'required|min:3',
'name_ar' => 'required|min:3',
// validations are missing descriptions
]);

$service->update($validation);

return redirect()->route('admin.services.index');
}

/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy(Service $service)
{
$service->delete();

return redirect()->route('admin.services.index');
}
}
26 changes: 26 additions & 0 deletions app/Models/Service.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;

class Service extends Model
{
use HasFactory, SoftDeletes;

protected $fillable = [
'name_ar',
'name_en',
'description_ar',
'description_en',
'featured_image'
];

public function getFeaturedImageAttribute($value)
{
return $value ? Storage::url($value) : 'https://bulma.io/images/placeholders/1280x960.png';
}
}
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function registerMediaCollections(): void
{
$this
->addMediaCollection('avatar')
->useFallbackUrl('https://ui-avatars.com/api/?background=random&size=128&color=fff&name=' . $this->name)
->useFallbackUrl('https://ui-avatars.com/api/?background=random&size=128&color=ffffff&name=' . $this->name)
->singleFile();
}
}
38 changes: 38 additions & 0 deletions database/migrations/2021_11_13_110059_create_services_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

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

class CreateServicesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('services', function (Blueprint $table) {
$table->id();
$table->string('name_ar');
$table->string('name_en');
$table->text('featured_image');
$table->text('description_ar');
$table->text('description_en');
$table->timestamps();
$table->softDeletes();
// you can add softdelete here there is no need for another migration
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('services');
}
}
129 changes: 129 additions & 0 deletions resources/views/admin/services/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
@extends('layouts.app', ['activePage' => 'services', 'titlePage' => __('New Service')])

@push('css')
<link href="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.css" rel="stylesheet">
@endpush

@push('js')
<script src="https://cdn.jsdelivr.net/npm/summernote@0.8.18/dist/summernote.min.js"></script>
<script>
$(document).ready(function() {
$('#summer-01').summernote();
$('#summer-02').summernote();
});
</script>
@endpush

@section('content')
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<form method="post" action="{{ route('admin.services.store') }}" autocomplete="off"
class="form-horizontal" enctype="multipart/form-data">
@csrf

<div class="card ">
<div class="card-header card-header-primary">
<h4 class="card-title">{{ __('Service information') }}</h4>
</div>
<div class="card-body ">
@if (session('status'))
<div class="row">
<div class="col-sm-12">
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert"
aria-label="Close">
<i class="material-icons">close</i>
</button>
<span>{{ session('status') }}</span>
</div>
</div>
</div>
@endif
<div class="row">
<label class="col-sm-2 col-form-label">{{ __('Arabic Name') }}</label>
<div class="col-sm-7">
<div class="form-group{{ $errors->has('name_ar') ? ' has-danger' : '' }}">
<input class="form-control{{ $errors->has('name_ar') ? ' is-invalid' : '' }}"
name="name_ar" id="input-name-ar" type="text"
placeholder="{{ __('Arabic Name') }}" value="{{ old('name_ar') }}"
required="true" aria-required="true" />
@if ($errors->has('name_ar'))
<span id="name-ar-error" class="error text-danger"
for="input-name-ar">{{ $errors->first('name_ar') }}</span>
@endif
</div>
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label">{{ __('English Name') }}</label>
<div class="col-sm-7">
<div class="form-group{{ $errors->has('name_en') ? ' has-danger' : '' }}">
<input class="form-control{{ $errors->has('name_en') ? ' is-invalid' : '' }}"
name="name_en" id="input-name-ar" type="text"
placeholder="{{ __('English Name') }}" value="{{ old('name_en') }}"
required="true" aria-required="true" />
@if ($errors->has('name_en'))
<span id="name-ar-error" class="error text-danger"
for="input-name-en">{{ $errors->first('name_en') }}</span>
@endif
</div>
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label">{{ __('Featured Image') }}</label>
<div class="col-sm-7">
<div
class="{{ $errors->has('featured_image') ? ' has-danger' : '' }} form-file-upload form-file-simple">
<input type="file" class="form-control" name="featured_image">
@if ($errors->has('featured_image'))
<span id="title-ar-error" class="error text-danger"
for="input-featured_image">{{ $errors->first('featured_image') }}</span>
@endif
</div>
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label">{{ __('Arabic description') }}</label>
<div class="col-sm-7">
<div class="form-group{{ $errors->has('description_ar') ? ' has-danger' : '' }}">
<textarea id="summer-01"
class="form-control{{ $errors->has('description_ar') ? ' is-invalid' : '' }}"
name="description_ar" id="input-description_ar"
placeholder="{{ __('Arabic description') }}"
required>{{ old('description_ar') }}</textarea>
@if ($errors->has('description_ar'))
<span id="description_ar-error" class="error text-danger"
for="input-description_ar">{{ $errors->first('description_ar') }}</span>
@endif
</div>
</div>
</div>
<div class="row">
<label class="col-sm-2 col-form-label">{{ __('English description') }}</label>
<div class="col-sm-7">
<div class="form-group{{ $errors->has('description_en') ? ' has-danger' : '' }}">
<textarea id="summer-02"
class="form-control{{ $errors->has('description_en') ? ' is-invalid' : '' }}"
name="description_en" id="input-description_en"
placeholder="{{ __('English description') }}"
required>{{ old('description_en') }}</textarea>
@if ($errors->has('description_en'))
<span id="description_en-error" class="error text-danger"
for="input-description_en">{{ $errors->first('description_en') }}</span>
@endif
</div>
</div>
</div>

<div class="card-footer ml-auto mr-auto">
<button type="submit" class="btn btn-primary">{{ __('Save') }}</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
@endsection
Loading