PHP code example of illuminatech / model-rules

1. Go to this page and download the library: Download illuminatech/model-rules 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/ */

    

illuminatech / model-rules example snippets




namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Item;
use Illuminate\Http\Request;

class ItemController extends Controller
{
    public function store(Request $request)
    {
        $validatedData = $this->validate($request, [
            'name' => [
                '   $item->name = $validatedData['name'];
        
        $category = Category::query()->find($validatedData['category_id']); // executes another SQL query 'SELECT ... FROM categories WHERE id =...'
        $item->category()->associate($category);
        
        $item->save();
        // ...
    }
}



namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Item;
use Illuminate\Http\Request;
use Illuminatech\ModelRules\Exists;

class ItemController extends Controller
{
    public function store(Request $request)
    {
        $validatedData = $this->validate($request, [
            'name' => [
                'item->name = $validatedData['name'];
        
        $category = $categoryRule->getModel(); // returns model fetched during validation without extra SQL query
        $item->category()->associate($category);
        
        $item->save();
        // ...
    }
}



namespace App\Http\Controllers;

use App\Models\Item;
use Illuminate\Http\Request;
use Illuminatech\ModelRules\Unique;

class ItemController extends Controller
{
    public function update(Request $request, Item $item)
    {
        $validatedData = $this->validate($request, [
            'name' => [
                ';
        
        $item = new Item();
        $item->name = $validatedData['name'];
        $item->slug = $validatedData['slug'];
        $item->save();
        // ...
    }
}



namespace App\Http\Controllers;

use App\Models\RefundRequest;
use App\Models\RefundReason;
use Illuminate\Http\Request;
use Illuminatech\ModelRules\Exists;

class RefundRequestController extends Controller
{
    public function store(Request $request)
    {
        $user = $request->user();
        
        $validatedData = $this->validate($request, [
            'reason_id' => [
                ' $refundRequest = new RefundRequest();
        $refundRequest->reason()->associate($reasonRule->getModel());
        $refundRequest->order()->associate($orderRule->getModel());
        $refundRequest->save();
        // ...
    }
}



namespace App\Http\Controllers;

use App\Models\Item;
use Illuminate\Http\Request;
use Illuminatech\ModelRules\Unique;

class ItemController extends Controller
{
    public function update(Request $request, Item $item)
    {
        $validatedData = $this->validate($request, [
            'name' => [
                ' slug is already in use at item ID=19"
            ],
        ]);
        // ...
    }
}



namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Item;
use Illuminate\Http\Request;

class ItemController extends Controller
{
    public function store(Request $request)
    {
        $validatedData = $this->validate($request, [
            'name' => [
                'es!
            ],
        ]);
        
        $item = new Item();
        $item->name = $validatedData['name'];
        
        $item->categories()->sync($validatedData['category_ids']);
        
        $item->save();
        // ...
    }
}



namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Item;
use Illuminate\Http\Request;
use Illuminatech\ModelRules\MultiExist;

class ItemController extends Controller
{
    public function store(Request $request)
    {
        $validatedData = $this->validate($request, [
            'name' => [
                'item->name = $validatedData['name'];
        
        $item->categories()->sync($categoryRule->getModels());
        
        $item->save();
        // ...
    }
}



namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Item;
use Illuminate\Http\Request;
use Illuminatech\ModelRules\Exists;

class ItemController extends Controller
{
    public function storeBatch(Request $request)
    {
        $firstRoundValidated = $this->validate($request, [
            'items' => ['   "items.{$key}.category_id" => $categoryRule, // validate single item at once
            ]);
            
            // create item draft:
            $item = new Item();
            $item->name = $item['name'];
            $item->category()->associatte($categoryRule->getModel());
            $items[] = $item;
        }
        
        foreach ($items as $item) {
            $item->save(); // save item drafts
        }
        // ...
    }
}



namespace App\Http\Controllers;

use App\Models\Category;
use App\Models\Item;
use Illuminate\Http\Request;
use Illuminate\Foundation\Http\FormRequest;
use Illuminatech\ModelRules\Exists;

class ItemStoreRequest extends FormRequest
{
    private $categoryRule;
    
    public function rules()
    {
        return [
            'name' => [
                'r extends Controller
{
    public function store(ItemStoreRequest $request)
    {
        $validatedData = $request->validated();
        
        $item = new Item();
        $item->name = $validatedData['name'];
        
        $item->category()->associate($request->validatedCategory());
        
        $item->save();
        // ...
    }
}