如何在Laravel中存储和更新多个数据

时间:2017-10-19 06:51:54

标签: php laravel

我的数据库中有两个表:角色表和角色成员资格表

角色表

Schema::create('roles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('RoleName');
        $table->boolean('IsAllCategory')->nullable()->default(0);
        $table->boolean('IsUserCanLogin')->nullable()->default(1);
        $table->timestamps();
    });

角色会员资格表

Schema::create('role_memberships', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('role_id');
        $table->string('MembershipName');
        $table->text('MembershipValue');
        $table->timestamps();
    });

我的角色表中有一行行数据,其中RoleName =' Admin'然后我设置了成员资格..每次我将角色成员资格数据存储到数据库中时,它会存储为两行使用相同的role_id .. 这是我的角色成员资格控制器

public function show($id)
{
    $role = Role::findOrFail($id);
    $postAdMaxImage = DB::table('role_memberships')->where('role_id', $role->id)->where('MembershipName' , 'PostAdMaxImage')->first();
    $postAdExpiredDay = DB::table('role_memberships')->where('role_id', $id)->where('MembershipName' , 'PostAdExpiredDay')->first();
    return view('pages/back-end/forms/role/membership')->with('role', $role)
                                                       ->with('postAdMaxImage', $postAdMaxImage)
                                                       ->with('postAdExpiredDay', $postAdExpiredDay);
}
public function update(Request $request, $id)
{
    $role = Role::findOrFail($id);
    $membership = [
        ['role_id' => $id, 'MembershipName' => 'PostAdMaxImage', 'MembershipValue' => $request->PostAdMaxImage ? $request->PostAdMaxImage : ''],
        ['role_id' => $id, 'MembershipName' => 'PostAdExpiredDay', 'MembershipValue' => $request->PostAdExpiredDay ? $request->PostAdExpiredDay : '']
    ];
    DB::table('role_memberships')->insert($membership);

    return response()->json(array($role, $membership));
}

第一个问题是show函数..我不能使用first()..我想获取数据显示在我的视图中

<div class="form-group">
    <div class="col-md-6">
      <label>Membership</label><br>
      <label>Maximum Gambar untuk Iklan</label>
      <div class="row">
        <div class="col-md-10">
          <input type="text" name="PostAdMaxImage" class="form-control" value="{{$postAdMaxImage->MembershipValue}}">
        </div>
        <div class="col-md-2" style="padding: 0 !important">
          <h5>Gambar</h5>
        </div>
      </div>
    </div>
  </div>
  <div class="form-group">
    <div class="col-md-6">
      <label>Masa Berlaku Iklan</label>
      <div class="row">
        <div class="col-md-10">
          <input type="text" name="PostAdExpiredDay" class="form-control" value="{{$postAdExpiredDay->MembershipValue}}>
        </div>
        <div class="col-md-2" style="padding: 0 !important">
          <h5>Hari</h5>
        </div>
      </div>
    </div>
  </div>

第二个问题是,当我想编辑成员资格时..它将存储为新的两行,而不是更新最后两行

1 个答案:

答案 0 :(得分:1)

要获取值,请使用value函数

$postAdMaxImage = DB::table('role_memberships')->wh‌​ere('role_id', $role->id)->where('MembershipName' , 'PostAdMaxImage')->value('MembershipValue');
$postAdExpiredDay = DB::table('role_memberships')->where('role_id', $id)->where('MembershipName' , 'PostAdExpiredDay')->value('MembershipValue');

将您的观点更改为:

  value="{{$postAdMaxImage}}"
  value="{{$postAdExpiredDay}}"

在第二个问题中,您需要更改update函数以更新字段而不创建新字段

public function update(Request $request, $id)
{
    $PostAdMaxImage= $request['PostAdMaxImage'];
    $PostAdExpiredDay = $request['PostAdExpiredDay'];
    DB::table('role_memberships')->where('role_id',$id)->where('MembershipName','PostAdMaxImage')->update(['MembershipValue'=>$PostAdMaxImage]);
DB::table('role_memberships')->where('role_id',$id)->where('MembershipName','PostAdExpiredDay')->update(['MembershipValue'=>$PostAdExpiredDay]);


    return response()->json(array($role, $membership));
}