Just as background information, i'm relatively new with PHP and Laravel. I started making a simple application to get some feelings with PHP and Laravel. In the application i want to create a object with some params and save this object in the database.
For this i created a migration:
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreatePlayersTable extends Migration
{
public function up()
{
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer("var1")->default(0);
$table->integer("var2")->default(0);
}
public function down()
{
Schema::dropIfExists('players');
}
}
I created a model (Player):
namespace App;
use Illuminate\Database\Eloquent\Model;
use DB;
use Log;
class Player extends Model
{
private $var1;
private $var2;
public function __construct($number)
{
$this->var1 = $number;
$this->var2 = $number;
}
}
And a controller off course (PlayerController):
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Player;
class PlayerController extends Controller
{
public function index()
{
$player = new Player(20);
// dd($player);
$player->save();
}
If i uncomment the dd($player) i get a json object in my browser what shows the player with var1 = 20 and var2 = 20 what is correct. If i check the player in the database, var1 and 2 are 0.
If i add $player->var1 = 10; above the $player->save(); the value in the database is 10.
Why is the value of the vars, defined by generating the Player object not stored and it is if i change the var values after the object exists?
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire