I have a little problem. I have a table where field owntype if it has a value of 0, the field owner attribute to the ID of the table players , if it has a value of 1 should assign the owner at a value of 0 if the field owntype is 2 , then assigns the owner ID from the table vehicles , someone I know how to do to make it work ? Below I include tables, call models work without a problem, because I can do all supported migration tables.
Thing table with conditional statement:
<?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');
}
}
Thing model:
<?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();
}
}
Player Model:
<?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');
}
}
Vehicle Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Vehicle extends Model
{
protected $table = 'vehicles';
public function thing()
{
return $this->hasMany('App\Thing', 'owner');
}
}
When I create an object and assign it to field owner 1
and leave the field owntype
blank, the player can see all the items.
But when I create a new item with a value of 1
for the field owner and value 2
for owntype
, this subject to the player with ID 1
remains, but it should not.
How should I create a conditional statement to make it work ?
I did it, like this:
public function queryThing($query)
{
return $query->where('owntype', 0);
}