在Laravel表单上获取具有其他值的行的ID

时间:2017-08-04 11:43:29

标签: php mysql sql laravel

如何获取具有名称值的id值?

我有这样的表格:

<form  enctype="multipart/form-data" id="projectclientexist" name="projectclientexist">
    @foreach ($clients as $key => $client)
    <div class="radio">
        <div class="col-md-3">
            <label><input type="radio" name="client" value="{{$client->name}}">{{$client->name}}</label>
        </div>
    </div>
    @endforeach
    <input type="submit" value="Crear relacion Proyecto-Cliente" id="relationclientexist" name="relationclientexist" class="btn btn-success">
</form>

如何获取具有名称值的客户端的id值?

public function storeProjectsClientsIfExist (Request $request)
{
    $client_project->project = new Client_Project();

    $client_project->client_id = DB::table('clients')
                                ->where('id', DB::raw("(select max(id) "));

    $client_project->project_id = DB::table('projects')
                                ->where('id', DB::raw("(select max(`id`) from projects)"))
                                ->first()
                                ->id;
    $client_project->save();
}

非常感谢,如果对代码有任何疑问,可以提问。

4 个答案:

答案 0 :(得分:2)

奇怪的问道。如果我正确理解你的questino,你想获得所选客户的ID吗?

如果是这种情况,在视图中,字段的value属性必须存储ID:

<label><input type="radio" name="client" value="{{$client->id}}">{{$client->name}}</label>

然后,在控制器中,您将获得如下所示的已检查客户端:

$clientID = $request->input('client'); // or $request->client;
$clientObj = Client::find($clientID); // Your client model object, if needed. Equals to: DB::table('clients')->find($clientID);

编辑:我认为您发生了Creating default object from empty value错误,因为您尝试在$client_project->project = new Client_Project();中的非对象上添加属性。您必须将$client_project定义为具有stdClass的对象(它是一个默认类,没有属性,没有方法。有点像空的javascript对象。)

所以我们在这里:

public function storeProjectsClientsIfExist (Request $request)
{
    $clientID = $request->input('name'); // get the name field value.

    $client_project = new \stdClass(); // define the var as an object.

    $client_project->project = new Client_Project();
    $client_project->client_id = $clientID;
    // currently the next line get the last project added in DB
    $client_project->project_id = DB::table('projects')
        ->where('id', DB::raw("(select max(`id`) from projects)"))
        ->first()
        ->id;

    // I think "$client_project->save();" will not work
    $client_project->project->save();

    return view('my.view'); // or redirect()->route('my.route') or what you want
}

答案 1 :(得分:1)

如果您想要选择客户端,则必须以值而不是名称

传递客户端的ID
@foreach ($clients as $key => $client)
          <div class="radio">
                  <div class="col-md-3">
                    <label><input type="radio" name="client" value="{{$client->id}}">{{$client->name}}</label>
                  </div>
          </div>
      @endforeach

答案 2 :(得分:1)

我想你可以试试这个:

public function storeProjectsClientsIfExist(Request $request)
       {
            $client_project->project = new Client_Project();
            $client_project->client_id = DB::table('clients')->where('name', $request->client)->first()->id;
            $client_project->project_id = DB::table('projects')->where('id', DB::raw("(select max(`id`) from projects)"))->first()->id;
            $client_project->save();
        }

希望这对你有用!!!

答案 3 :(得分:0)

如果您只有与名称相关联的ID,请尝试以下操作:

$client_id = DB::table('clients')->where('name',$request->client))->first()->id;

如果许多id与给定名称相关联,那么您可以使用它:

$client_ids = DB::table('clients')->where('name',$request->client))->pluck('id');