Php 获取在窗体Laravel上具有其他值的行的ID

Php 获取在窗体Laravel上具有其他值的行的ID,php,mysql,sql,laravel,Php,Mysql,Sql,Laravel,如何获取具有名称值的id值 public function storeProjectsClientsIfExist (Request $request) { $client_project->project = new Client_Project(); $client_project->client_id = DB::table('clients') ->where('id', DB::raw(

如何获取具有名称值的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();
}
我有以下表格:

<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吗

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

<label><input type="radio" name="client" value="{{$client->id}}">{{$client->name}}</label>
编辑:我认为您的
从空值创建默认对象时发生了错误,因为您试图在
$client\u project->project=new client\u project()中的非对象上添加属性。您必须将
$client\u 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
}

奇怪地问。如果我正确理解你的问题,你想得到所选客户的ID吗

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

<label><input type="radio" name="client" value="{{$client->id}}">{{$client->name}}</label>
编辑:我认为您的
从空值创建默认对象时发生了错误,因为您试图在
$client\u project->project=new client\u project()中的非对象上添加属性。您必须将
$client\u 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
}

如果要选择客户端,则必须在值中传递客户端id,而不是在名称中传递客户端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
@foreach($clients as$key=>$client)
{{$client->name}
@endforeach

如果要选择客户端,则必须在值中传递客户端id,而不是在名称中传递客户端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
@foreach($clients as$key=>$client)
{{$client->name}
@endforeach

我想你可以试试这个:

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

希望这为你工作

我想你可以试试这个:

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

希望这为你工作

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

$client_id = DB::table('clients')->where('name',$request->client))->first()->id;
如果有多个id与给定的名称关联,则可以使用:

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

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

$client_id = DB::table('clients')->where('name',$request->client))->first()->id;
如果有多个id与给定的名称关联,则可以使用:

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


你能详细说明你的问题吗?当然可以;)在表单上传递name值时,如何获取客户端的id;)试试这个
$client\u project->client\u id=DB::table('clients')->where('name',$request('client'))->first()->id
:)`知道我为什么提交go to:?也许有些id是错的,我想是因为你在表单或方法中提交后没有添加要命中的路线。你能详细说明你的问题吗?当然;)在表单上传递name值时,如何获取客户端的id;)试试这个
$client\u project->client\u id=DB::table('clients')->where('name',$request('client'))->first()->id
:)`知道我为什么提交go to:?可能是某个id出错了,我想是因为你在表单或方法中提交后没有添加要命中的路由。我按照你说的那样尝试,然后在控制器中执行以下操作:
$client\u project->client\u id=$request->input(“客户”)给我一个错误:
从空值创建默认对象
。我喜欢将值传递给客户机id,这是在控制器上获取值的简单方法。知道如何修复错误吗?请尝试
$client\u project->client\u id=$request->client
insteadI按照您所说的那样尝试,然后在controller中执行以下操作:
client\u project->client\u id=$request->input(“客户”)给我一个错误:
从空值创建默认对象
。我喜欢将值传递给客户机id,这是在控制器上获取值的简单方法。知道如何修复错误吗?请尝试
$client\u project->client\u id=$request->client从空值创建默认对象
如果定义了
客户机
模型,我的回答中的
$clientObj
部分会起作用。在您的情况下,我认为您可以使用
$clientID
来执行
$client\u项目->client\u id
。您是否使用
use App\client在您的控制器类定义之前?是@iArcadia,其声明为
使用App\Models\Client我有一个语法错误,让我重新编写代码,如果行得通或不行,我会发布:DHi,这是一个好主意,而且更简单。无论如何,给我一个错误:
从空值创建默认对象
如果定义了
客户机
模型,我的回答中的
$clientObj
部分会起作用。在您的情况下,我认为您可以使用
$clientID
来执行
$client\u项目->client\u id
。您是否使用
use App\client在您的控制器类定义之前?是@iArcadia,其声明为
使用App\Models\Client我有一个语法错误,让我重新编写代码,如果行得通或不行,我会发布:D