PHP code example of dlogon / quick-crud-for-laravel
1. Go to this page and download the library: Download dlogon/quick-crud-for-laravel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Dlogon\QuickCrudForLaravel\Traits\NavigationUtils;
class Blog extends Model
{
use HasFactory;
use NavigationUtils;
}
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
public function up(): void
{
Schema::create('blogs', function (Blueprint $table) {
$table->id();
$table->string("name");
$table->string("content");
$table->timestamps();
});
}
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Dlogon\TailwindAlerts\Facades\TailwindAlerts;
use Dlogon\QuickCrudForLaravel\Helpers\Search;
use App\Models\Blog;
class BlogController extends Controller
{
public $tableFields = array (
'id' => 'id',
'name' => 'name',
'content' => 'content',
'created_at' => 'created_at',
'updated_at' => 'updated_at',
);
/*
[
"label" => "tablefieldName",
'label' => ["type" => "related", "field" =>"relationName.fieldNameOfRelatedModel"],
'label' => ["type"=>"money", "field"=>'moneyOrDecimalField']
]
*/
public $searchFields = [
"created_at" => [
"type" => "singleDate", // search by date
"label" => "Creation date",
],
/*
"fieldName" =>[
"type" => "text" // search by text
"placeholder" => "my search"
]
"customer_id" => [
"type" => "related", // search by related model
"elements" => $allCustomers, //<- this will populate a select dropdown
"modelDisplay" => "name",
"label" => "Cliente", <--what show to
"value" => "id" <-- what find to
]
*/
];
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index($models = null)
{
$models = $models ?? Blog::orderBy("id")->paginate(10);
$fields = $this->tableFields;
$searchFields = $this->searchFields;
return view(
"crudable.blog.index",
[
"models" => $models,
"fields" => $fields,
"searchFields" => $searchFields
]
);
}
public function search(Request $request)
{
$models = Blog::query();
if ($request->has('q'))
$models = Search::searchByQueryParams(new Blog, $request)->get();
return $this->index($models);
}
/**
* Display the specified resource.
*
* @param \App\Models\Blog $model
* @return \Illuminate\Http\Response
*/
public function show(Blog $blog)
{
$fields = $this->tableFields;
return view(
"crudable.blog.show",
[
"model" => $blog,
"fields" => $fields
]
);
}
/**
* Remove the specified resource from storage.
*
* @param \App\Models\Customer $customer
* @return \Illuminate\Http\Response
*/
public function destroy(Blog $blog)
{
$blog->delete();
TailwindAlerts::addBottomToastMessage("Deleted", TailwindAlerts::ERROR);
return redirect()->route("blogs.index");
}
}