mardi 21 juillet 2020

Displaying records from the database based on selected values

I am beginner in Laravel and php. I write my project in Laravel 7. I have this code:

class Plate extends Model
{
    use ScopeActiveTrait;

    protected $fillable = ['name', 'enable', 'price', 'vat_id', 'type', 'description'];
    protected $quarded = ['id'];
    public $timestamps = false;

    public function frontImage()
    {
        return $this->hasMany('App\Models\UploadImage', 'file_id', 'id')->orderBy('order', 'ASC')->where('file_type', 'plates');
    }

    public function vat()
    {
        return $this->belongsTo('App\Models\Vat', 'vat_id');
    }
}

class SelectedPlates extends Model
{
    use ScopeActiveTrait;

    protected $quarded = ['id'];
    protected $fillable = ['plate_id', 'status', 'maxvalue', 'minvalue', 'product_id'];

}

Schema::create('selected_plates', function (Blueprint $table) {
            $table->id();
            $table->bigInteger('plate_id')->unsigned();
            $table->foreign('plate_id')->references('id')->on('plates')->onDelete('cascade');
            $table->bigInteger('product_id')->unsigned();
            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
            $table->char('status', 1)->default(0);
            $table->decimal('maxvalue', 9, 2)->default(0);
            $table->decimal('minvalue', 9, 2)->default(0);
            $table->timestamps();
        });

Schema::create('plates', function (Blueprint $table) {
            $table->id();
            $table->string('name', 155);
            $table->string('description', 155)->nullable();
            $table->char('enable', 1)->default(0);
            $table->decimal('price', 9, 2)->default(0);
            $table->smallInteger('vat_id')->unsigned()->default(1);
            $table->foreign('vat_id')->references('id')->on('vat');
            $table->char('type', 1)->default(0);
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });

and function to show result:

public function getMaterials(string $query, int $type, int $produktId)
    {
        return $this->model->active()->with(['vat', 'frontImage'])->where(function ($q) use ($query, $type) {
            if ($query != "") {
                $q->where('name', 'LIKE', '%' . $query . '%');
                $q->orWhere('description', 'LIKE', '%' . $query . '%');
            }
        })
            ->where(function ($q) use ($type) {
                $q->where('type', $type);
                $q->orWhere('type', 3);
            })
            ->orderBy('name', 'asc')->get();
    }

$this->model = Plate. It's work fine. Its show all my plates with parms etc.

I need to change this code so that this function returns not all plates - only the ones that were selected (selected_plates).

How can I make it?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/30ucsNT
via IFTTT

Aucun commentaire:

Enregistrer un commentaire