2017-08-04 92 views
1

如何获取具有名称值的id值?获取表单上具有其他值的行的ID Laravel

我有这样的形式:

<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(); 
} 

非常感谢,如果有关于代码的任何问题,你可以问它。

+0

你能详细阐述您的问题吗? –

+0

当然;)如何获得客户端的ID,当你在表单上传递名称值; –

+0

试试这个'$ client_project-> client_id = DB :: table('clients') - > where('name' ,$ request('client')) - > first() - > id;':) ' – Maraboc

回答

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(); 
     } 

希望为你工作!!!

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 
} 
+0

嗨,这是一个好主意。无论如何,给我一个错误:'从空值创建默认对象' –

+0

如果已经定义了'Client'模型,则我的答案中的$ clientObj部分有效。在你的情况下,我认为你可以使用'$ clientID'作为'$ client_project-> client_id'。 – iArcadia

+0

我有一个模型定义,不工作。:l –

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 
+0

我尝试像你说的,然后在控制器做到这一点?:'$ client_project-> client_id = $ request-> input(“client”);'给我一个错误:'从空值创建默认对象。我喜欢将价值传递给客户端的想法,这是一种简单的方法来获取控制器的价值。知道如何解决这个错误? –

+0

你会尝试'$ client_project-> client_id = $ request-> client;'而不是 –

+0

同样的错误伴侣。 –

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'); 
相关问题