访问多对多关系的集合,并将集合的特定部分作为old old()

问题描述

预先感谢您的帮助。

目前正在学习Laravel,而我正在苦苦挣扎。

背景 我正在尝试构建报价/发票应用程序。我可以创建产品报价。信息存储在pivot tableproduct_quote中。请在下面查看迁移。

Schema::create('product_quote',function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('product_id');
        $table->unsignedBigInteger('quote_id');
        $table->integer('qty');
        $table->timestamps();
    });

我的恋爱关系为many to many

请参阅我的Quote Model

public function products(){
    return $this->belongsToMany(Product::class)->withPivot('qty');
}

请参阅我的Product Model

public function quotes(){
    return $this->belongsToMany(Quote::class)->withPivot('qty');
}

我有一个form,其中的引号由products的{​​{1}}填充。请查看产品grouped

migration

在表格内的组中显示的产品由示例确定:

Schema::create('products',function (Blueprint $table) {
        $table->id();
        $table->integer('group');
        $table->string('code');
        $table->string('name');
        $table->double('price');
        $table->timestamps();
    });

请参阅示例如何在我的$features = DB::table('products')->whereIn('group',array(32))->get(); 中设置报价输入。

view

正如您在上面看到的,我在<div class="tab-pane fade" id="pills-system_features" role="tabpanel" aria-labelledby="pills-system_features-tab"> <p class="p-2 text-center alert-info">Please select the required system features.</p> @foreach($features as $feature) <div class="form-group row"> <label for="{{$feature->id}}" class="col-md-4 col-form-label text-md-right">{{$feature->code. ' ' .$feature->name}}</label> <div class="col-md-6"> <div > <input name="{{$feature->id}}" id="{{$feature->id}}" type="number" min="0" max="1" class="form-control @error('${{$feature->id}}') is-invalid @enderror" value="{{old($feature->id) ?? 0}}" autocomplete="{{$feature->id}}" required autofocus> @error('{{$feature->id}}') <span class="invalid-Feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div> </div> </div> @endforeach {{old() ?? 0}}中使用input。我这样做是因为在执行逻辑并且用户犯了错误之后,我使用了value = ""

创建,配置报价,执行逻辑并将数据存储在相应的Redirect::back->withinput()->with('error','msg')中。 我有一个选项,您可以在其中编辑报价。我转到tables,可以在其中编辑客户详细信息。然后,我将数据发送到保存客户端数据的view。然后,我进入Controller,然后再次有机会编辑报价单。

我的问题 返回quote view以编辑报价。 所有旧细节都不会显示在输入字段中。

我已经取得了以下进展:

quote view

要查找要编辑的报价。

$quote = Quote::query()->findOrFail($id);

查找此报价的产品。

执行$products = $quote->products; 后,我得到的产品如下。

dd($products);

例如,当我访问集合时。在Illuminate\Database\Eloquent\Collection {#1295 ▼ #items: array:5 [▼ 0 => App\Product {#1303 ▶} 1 => App\Product {#1304 ▶} 2 => App\Product {#1305 ▶} 3 => App\Product {#1306 ▶} 4 => App\Product {#1307 ▶} ] } 中,我得到:

attributes

在同一个 #attributes: array:7 [▼ "id" => 34 "group" => 10 "code" => "KX-TES824SA" "name" => "Main Unit" "price" => 3651.4243455746 "created_at" => "" "updated_at" => "" ] 中,我得到:

relations

因此我可以访问所需的信息,但是如何从那里获取使用我的 #relations: array:1 [▼ "pivot" => Illuminate\Database\Eloquent\Relations\Pivot {#1300 ▼ +incrementing: false #guarded: [] #connection: "MysqL" #table: "product_quote" #primaryKey: "id" #keyType: "int" #with: [] #withCount: [] #perPage: 15 +exists: true +wasRecentlyCreated: false #attributes: array:3 [▼ "quote_id" => 7 "product_id" => 34 "qty" => 1 ] 的表单。

是否应该为input value = old() ?? 0创建一个不同的view,如果可以,我仍然使用edit,否则,如何在执行逻辑后返回input value = old() ?? 0。 / p>

很抱歉,这个问题很长,我试图将其尽量缩短,但仍提供了所有需要的信息。

解决方法

old()助手的工作方式如下:

old()

旧功能检索刷新到输入中的旧输入值 会话:

$value = old('value');

$value = old('value','default');

如您所见,该值是从会话中检索的。

对于您而言,当您尝试更新项目时,不会从会话中提取信息。您正在从数据库中提取它。这就是old()不返回任何内容的原因。

如果您再次查看old()函数定义,它将接受第二个可用作默认值的参数。我想您可以参考其中的值,如下所示:

// ...
value="{{ old($feature->id,$feature->id ?? 0) }}"
// ...                      ^^^^^^^^^^^^^^^^^

尝试一下,它可能需要稍作调整,但是您已经了解了总体思路以及为什么它一开始就不起作用。