我有一个小问题。我有一个表,其中字段 owntype 如果其值为 0,则字段所有者属性为表中玩家的 ID,如果其值为 1,则如果字段 owntype 为 2,则应将所有者分配为 0 值,然后从车辆表中分配车主 ID,我知道该怎么做才能使其工作吗?下面我包括表格,调用模型工作没有问题,因为我可以做所有支持的迁移表格。
带有条件语句的事物表:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateThingsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('things', function (Blueprint $table) {
$table->increments('id');
$table->integer('object')->default(0);
$table->tinyInteger('category')->default(0);
$table->string('name');
$table->integer('owner')->unsigned()->nullable();
$table->integer('owntype')->default(0);
$table->integer('number')->default(0);
$table->integer('value1')->default(0);
$table->integer('value2')->default(0);
$table->integer('damage')->default(1);
$table->float('pos_x');
$table->float('pos_y');
$table->float('pos_z');
$table->tinyInteger('pos_vw');
$table->integer('use')->default(0);
$table->timestamp('date');
$table->timestamps();
if(Schema::hasColumn('things', 'owntype') == 0)
{
$table->foreign('owner')->references('id')->on('players')->onDelete('cascade');
}
elseif(Schema::hasColumn('things', 'owntype') == 1)
{
Schema::table('things', function ($table) {
return $table->integer('owner')->unsigned()->nullable()->change();
});
}
elseif(Schema::hasColumn('things', 'owntype') == 2)
{
$table->foreign('owner')->references('id')->on('vehicles')->onDelete('cascade');
}
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('things');
}
}
事物模型:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Thing extends Model
{
protected $table = 'things';
protected $fillable = ['name', 'owner', 'owntype'];
public function player()
{
return $this->belongsTo('App\Player', 'owner')->withTimestamps();
}
public function vehicle()
{
return $this->belongsTo('App\Vehicle', 'owner')->withTimestamps();
}
}
玩家型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Player extends Model
{
protected $table = 'players';
protected $fillable =
[
'name',
'age',
'gid',
'sex',
'skin',
'from',
'history',
'online'
];
public function user()
{
return $this->belongsTo('App\User');
}
public function formatName($name)
{
$nick = str_replace("_", " ", $name);
return $nick;
}
public function checkActive($active)
{
if($active == 0)
{
$showActive = '<span class="label label-error">Zablokowana</span>';
return $showActive;
}
if($active == -999)
{
$showActive = '<span class="label label-default"><s>Zbanowana</s></span>';
return $showActive;
}
if($active == 1)
{
$showActive = '<span class="label label-success">Aktywna</span>';
return $showActive;
}
}
public function formatTime($time)
{
$hours = floor($time / 60);
$minutes = $time - ($hours * 60);
return $hours.'h '.$minutes.'min.';
}
public function formatSex($sex)
{
if($sex == 0)
{
$man = 'Mężczyzna';
return $man;
}
else
{
$girl = 'Kobieta';
return $girl;
}
}
public function formatKP($kp)
{
if($kp == 1)
{
$showKP = '<span class="label label-warning">Gracz premium!</span>';
return $showKP;
}
}
public function ban()
{
return $this->hasOne('App\Ban', 'player_name', 'name');
}
public function busines()
{
return $this->belongsTo('App\Busines', 'bmember');
return $this->belongsTo('App\Busines', 'bleader');
}
public function organization()
{
return $this->belongsTo('App\Organization', 'member');
return $this->belongsTo('App\Organization', 'leader');
}
public function house()
{
return $this->hasMany('App\House', 'owner');
}
public function thing()
{
return $this->hasMany('App\Thing', 'owner');
}
public function login()
{
return $this->hasMany('App\Login', 'nickname', 'name');
}
}
车辆型号:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Vehicle extends Model
{
protected $table = 'vehicles';
public function thing()
{
return $this->hasMany('App\Thing', 'owner');
}
}
当我创建一个对象并将其分配给字段所有者
1
并将字段owntype
留空时,玩家可以看到所有项目。
但是,当我为字段所有者创建一个值为
1
且为 2
为 owntype
的新项目时,ID 为 1
的玩家的这个主题仍然存在,但不应该。
我应该如何创建条件语句才能使其发挥作用?
我做到了,就像这样:
public function queryThing($query)
{
return $query->where('owntype', 0);
}