Added ability to seed manufacturers

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2025-04-22 14:13:46 +01:00
parent 2a2d118973
commit e5dc13e48c
10 changed files with 134 additions and 29 deletions

View file

@ -6,6 +6,7 @@ use App\Http\Requests\ImageUploadRequest;
use App\Models\Actionlog; use App\Models\Actionlog;
use App\Models\Manufacturer; use App\Models\Manufacturer;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Log;
@ -31,7 +32,28 @@ class ManufacturersController extends Controller
public function index() : View public function index() : View
{ {
$this->authorize('index', Manufacturer::class); $this->authorize('index', Manufacturer::class);
return view('manufacturers/index'); $manufacturer_count = Manufacturer::withTrashed()->count();
return view('manufacturers/index')->with('manufacturer_count', $manufacturer_count);
}
/**
* Returns a view that invokes the ajax tables which actually contains
* the content for the manufacturers listing, which is generated in getDatatable.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @see Api\ManufacturersController::index() method that generates the JSON response
* @since [v1.0]
*/
public function seed() : RedirectResponse
{
$this->authorize('index', Manufacturer::class);
if (Manufacturer::withTrashed()->count() == 0) {
Artisan::call('db:seed', ['--class' => 'ManufacturerSeeder']);
return redirect()->route('manufacturers.index')->with('success', trans('general.seeding.manufacturers.success'));
}
return redirect()->route('manufacturers.index')->with('error', 'could not seed - manufacturers already exist');
} }
/** /**

View file

@ -166,4 +166,49 @@ class ManufacturerFactory extends Factory
]; ];
}); });
} }
public function samsung()
{
return $this->state(function () {
return [
'name' => 'Samsung',
'url' => 'https://www.samsung.com',
'support_url' => 'https://www.samsung.com/support/',
'image' => 'samsung.png',
];
});
}
public function google()
{
return $this->state(function () {
return [
'name' => 'Google',
'url' => 'https://www.google.com',
'image' => 'google.webp',
];
});
}
public function huawei()
{
return $this->state(function () {
return [
'name' => 'Huawei',
'url' => 'https://consumer.huawei.com/',
'image' => 'huawei.webp',
];
});
}
public function sony()
{
return $this->state(function () {
return [
'name' => 'Sony',
'url' => 'https://electronics.sony.com',
'image' => 'sony.png',
];
});
}
} }

View file

@ -27,6 +27,10 @@ class ManufacturerSeeder extends Seeder
Manufacturer::factory()->count(1)->adobe()->create(['created_by' => $admin->id]); Manufacturer::factory()->count(1)->adobe()->create(['created_by' => $admin->id]);
Manufacturer::factory()->count(1)->avery()->create(['created_by' => $admin->id]); Manufacturer::factory()->count(1)->avery()->create(['created_by' => $admin->id]);
Manufacturer::factory()->count(1)->crucial()->create(['created_by' => $admin->id]); Manufacturer::factory()->count(1)->crucial()->create(['created_by' => $admin->id]);
Manufacturer::factory()->count(1)->samsung()->create(['created_by' => $admin->id]);
Manufacturer::factory()->count(1)->google()->create(['created_by' => $admin->id]);
Manufacturer::factory()->count(1)->huawei()->create(['created_by' => $admin->id]);
Manufacturer::factory()->count(1)->sony()->create(['created_by' => $admin->id]);
$src = public_path('/img/demo/manufacturers/'); $src = public_path('/img/demo/manufacturers/');
$dst = 'manufacturers'.'/'; $dst = 'manufacturers'.'/';

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.8 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 31 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 22 KiB

View file

@ -615,4 +615,12 @@ return [
'cost_each' => ':amount each', 'cost_each' => ':amount each',
'remove_current_image' => 'Remove current :type image', 'remove_current_image' => 'Remove current :type image',
'seeding' => [
'manufacturers' => [
'button' => 'Create Manufacturers',
'prompt' => 'You do not have any manufacturers yet. Would you like to seed a list of common manufacturers? (THIS WILL OVERWRITE EXISTING MANUFACTURERS, including those that have been soft-deleted.)',
'success' => 'Manufacturers seeded successfully',
],
],
]; ];

View file

@ -25,10 +25,29 @@
{{-- Page content --}} {{-- Page content --}}
@section('content') @section('content')
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<div class="box box-default"> <div class="box box-default">
<div class="box-body"> <div class="box-body">
@if ($manufacturer_count == 0)
<form action="{{ route('manufacturers.seed') }}" method="POST">
{{ csrf_field() }}
<div class="callout callout-info">
<p>
{{ trans('general.seeding.manufacturers.prompt') }}
<button class="btn btn-sm btn-primary hidden-print" rel="noopener">
{{ trans('general.seeding.manufacturers.button') }}
</button>
</p>
</div>
</form>
@else
<table <table
data-columns="{{ \App\Presenters\ManufacturerPresenter::dataTableLayout() }}" data-columns="{{ \App\Presenters\ManufacturerPresenter::dataTableLayout() }}"
data-cookie-id-table="manufacturersTable" data-cookie-id-table="manufacturersTable"
@ -49,13 +68,17 @@
"ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"] "ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"]
}'> }'>
</table> </table>
@endif
</div><!-- /.box-body --> </div><!-- /.box-body -->
</div><!-- /.box --> </div><!-- /.box -->
</div> </div>
</div> </div>
@stop @stop
@section('moar_scripts') @section('moar_scripts')
@include ('partials.bootstrap-table') @include ('partials.bootstrap-table')
@stop @stop

View file

@ -59,6 +59,9 @@ Route::group(['middleware' => 'auth'], function () {
Route::group(['prefix' => 'manufacturers', 'middleware' => ['auth']], function () { Route::group(['prefix' => 'manufacturers', 'middleware' => ['auth']], function () {
Route::post('{manufacturers_id}/restore', [ManufacturersController::class, 'restore'] )->name('restore/manufacturer'); Route::post('{manufacturers_id}/restore', [ManufacturersController::class, 'restore'] )->name('restore/manufacturer');
Route::post('seed', [ManufacturersController::class, 'seed'] )->name('manufacturers.seed');
}); });
Route::resource('manufacturers', ManufacturersController::class); Route::resource('manufacturers', ManufacturersController::class);