尝试读取 null 属性?

问题描述 投票:0回答:1

嗨,我正在开发一个 Laravel 项目,我正在数据库上存储订单,并且我的一些值没有被存储并在数据库上显示

NULL
this is my orders table

这就是我通过控制器将数据存储到数据库中的方式:

public function store(Request $request)
    {
        //dd($request->all());
        $order = new Order;
        //$order->product_name = request('product_name');
        $order->prenom = request('prenom');
        $order->nom = request('nom');
        $order->nom_entreprise = request('nom_entreprise');
        $order->commune_id   = request('commune_id  ');
        $order->product_id = request('product_id');
        $order->telephone = request('telephone');
        $order->email = request('email');
        $order->wilaya_id  = request('wilaya_id ');
        $order->subtotal = request('subtotal');
        $order->tax = request('tax');
        $order->status = request('status');
        $order->qty = request('qty');
        $order->total = request('total');

        $order->save();
    }

这是迁移代码:

public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->id();
            $table->integer('product_id')->unsigned();
            $table->integer('commune_id')->unsigned();
            $table->integer('wilaya_id')->unsigned();
            $table->string('product_name');
            $table->string('prenom');
            $table->string('nom');
            $table->string('nom_nom_entreprise')->nullable();
            $table->integer('telephone')->nullable();
            $table->string('email')->nullable();
            $table->string('livraison')->nullable();
            $table->double('subtotal');
            $table->double('tax')->nullable();
            $table->integer('qty');
            $table->double('total');
            $table->boolean('status');
            $table->timestamps();
        });
    }

注意 当我 dd($request->commune_id ,$request->wilaya_id);它检索它们的 ID 就很好,但是当存储在数据库上时,值是

NULL 

这是型号代码:

Order Model

class Order extends Model
{
    use HasFactory;
    protected $fillable = ['nom' ,'wilaya_id', 'commune_id'];
    public function produits()
    {
        return $this->hasMany(Produit::class);
    }

    public function wilaya()
    {
        return $this->belongsTo(Wilaya::class, 'wilaya_id');
    }

    public function commune()
    {
        return $this->belongsTo(Commune::class, 'commune_id');
    }
    public function setFilenamesAttribute($value)
    {
        $this->attributes['filenames'] = json_encode($value);
        //$this->attributes['properties'] = json_encode($value);
    }
}

Wilaya Model

class Wilaya extends Model
{

    protected $fillable = ['nom'];
    use HasFactory;
    public function orders()
    {
        return $this->hasMany(Order::class);
    }
    public function communes()
    {
        return $this->hasMany(Commune::class);
    }
}

Commune Model

class Commune extends Model
{
    protected $fillable = ['nom', 'wilaya_id'];
    use HasFactory;

    public function orders()
    {
        return $this->hasMany(Order::class);
    }

    public function wilaya()
    {
        return $this->belongsTo(Wilaya::class);
    }
}

请随时告诉我我可以做些什么来解决这个问题。

谢谢

laravel database properties
1个回答
0
投票

问题似乎在于

request()
函数中
commune_id
wilaya_id
方法中的
store
的额外空格。请尝试以下方法。

public function store(Request $request)
{
    $order = new Order;
    $order->prenom = request('prenom');
    $order->nom = request('nom');
    $order->nom_entreprise = request('nom_entreprise');
    $order->commune_id = request('commune_id'); // Removed extra spaces
    $order->product_id = request('product_id');
    $order->telephone = request('telephone');
    $order->email = request('email');
    $order->wilaya_id = request('wilaya_id'); // Removed extra spaces
    $order->subtotal = request('subtotal');
    $order->tax = request('tax');
    $order->status = request('status');
    $order->qty = request('qty');
    $order->total = request('total');

    $order->save();
}

在您的代码中,

request('commune_id  ')
request('wilaya_id ')
末尾有额外的空格,这意味着它们正在使用这些确切的键(包括空格)查找请求数据。由于您的请求数据可能不包含键中的这些空格,因此它返回
null

© www.soinside.com 2019 - 2024. All rights reserved.