在雄辩的orm中从数组输入中获取相同的数据

时间:2017-04-08 16:53:15

标签: php laravel orm eloquent

移植

<?php

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

class CreateErgebnisseTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('ergebnisse', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('katogorie_id')->unsigned()->index();
            $table->foreign('katogorie_id')->references('id')->on('katogorie')->onDelete('cascade');
            $table->integer('name_id')->unsigned()->index();
            $table->foreign('name_id')->references('id')->on('name')->onDelete('cascade');
            $table->integer('genauer_id')->unsigned()->index();
            $table->foreign('genauer_id')->references('id')->on('genauer')->onDelete('cascade');
            $table->integer('geometrie_id')->unsigned()->index();
            $table->foreign('geometrie_id')->references('id')->on('geometrie')->onDelete('cascade');
            $table->string('probe');
            $table->string('schnittgrosse');
            $table->integer('temperatur');
            $table->integer('zeit');
            $table->integer('aussehen');
            $table->integer('farbe');
            $table->integer('deformation');
            $table->integer('geruch');
            $table->integer('texture');

            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('ergebnisse');
    }
}

模型

 <?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Model;
use App\Models\Ergebnisse;

class Ergebnisse extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table='ergebnisse';
    protected $fillable = [
        'name',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [

    ];

    public function name()
    {
        return $this->belongsTo('App\Models\Name');
    }

    public function geometrie()
    {
        return $this->belongsTo('App\Models\Geometrie');
    }
    public function katogorie()
    {
        return $this->belongsTo('App\Models\Katogorie');
    }
    public function genauer()
    {
        return $this->belongsTo('App\Models\Genauer');
    }

}

方法

     $result1=Ergebnisse::where('name_id', $nam)->where('geometrie_id', $geo)->get();

        //Getting the same rows which equals temperatur and zeit

                $mix1 =Ergebnisse::where('name_id', $nam2)->where('geometrie_id', $geo2)
                ->where(function($q) use($result1) {               
                foreach ($result1 as $item) {
                     $q->orWhere(function($q) use($item) {
                         $q->where('temperatur', $item->temperatur)
                           ->where('zeit', $item->zeit);
                    });
                }
     })->orderBy( DB::raw('(temperatur) + (zeit)'))->get();

//getting the best result with respect to minimum sum...
     $result = Ergebnisse::select(DB::raw('*, (aussehen) + (farbe)+(deformation)+(geruch)+(texture) as sum'))->where(function($q) use($mix1) {               
            foreach ($mix1 as $item) {
                 $q->orWhere(function($q) use($item) {
                     $q->where('temperatur', $item->temperatur)
                       ->where('zeit', $item->zeit);
                });
            }
        }) 
         ->orderBy('sum', 'asc')->first();

我通过url param来获取名称和几何id。在上面的查询中,我得到了两个名称和几何id的相同数据,即nam,nam1和geo,geo2 .. 通过这个id我匹配温度和zeit。 上面的查询适用于两个名称和几何id。我需要使它像通用或至少五个名称和几何id。例如,如果我得到三个名称和几何id或四个?我怎样才能做到这一点? 我希望我已正确解释了我的问题。

0 个答案:

没有答案