【问题标题】:How do I get info from a table with data from another table?如何从一个表中获取包含另一个表中的数据的信息?
【发布时间】:2020-09-10 06:13:15
【问题描述】:

我有一个来自组的行,其中包含一个下拉列表,该下拉列表会自动填充“客户”表中的数据。

<label for="client" class="text-muted font-weight-light text-center">{{ __('Client') }}</label>
<div class="form-group row">
    <div class="col-md-12">
       <select class="form-control" id="user" name="user">
       @foreach($clients as $client)
         <option value="{{$client->id}}">{{$client->firstname}} {{$client->lastname}} - {{$client->residence}} ({{$client->housing}} {{$client->housenr}})</option>
       @endforeach
       </select>
    </div>
</div>

这个 POST 表单将整个表单推送到名为“tasks”的表中。现在的问题是我在这个刀片中使用(例如){{$client-&gt;residence}},但我需要能够在我的控制器中的插入函数中执行此操作才能将其推送到数据库。

/**
 * Store a newly created resource in storage.
 *
 * @param  \Illuminate\Http\Request  $request
 * @return \Illuminate\Http\Response
 */
public function insert(Request $request)
{
    $clients = Clients::All();

    $id = auth()->user()->id;
    $title = $request->input('title');
    $startdate = $request->input('startdate');
    $enddate = $request->input('enddate');
    $starttime = $request->input('starttime');
    $endtime = $request->input('endtime');
    $description = $request->input('description');

    $data=array(
        "uuid"=>$id,
        "title"=>$title,
        "client"=>$client,
        "startdate"=>$startdate,
        "enddate"=>$enddate,
        "starttime"=>$starttime,
        "endtime"=>$endtime,
        "description"=>$description);
    DB::table('tasks')->insert($data);
    return redirect('/todo');
}

所以我想做的是使用选定的{{$client-&gt;id}} 获取信息以将其推送到单独列中的任务表。

希望我的意思很清楚。

【问题讨论】:

  • 有什么问题?你有client_id,你可以简单地通过它的id搜索客户端然后找到客户端。
  • 我已经修复了它,但我正在为转换而苦苦挣扎。
  • 您收到的请求中包含哪些内容?插入 dd($request);在插入并向我们展示输出

标签: database laravel controller laravel-blade laravel-controller


【解决方案1】:

根据你上面的查询

$firsname 不应该是一个单独的值。它必须是数组或对象。所以 请尝试使用以下代码访问名字。

将对象作为字符串值访问时也会出错。

  "firstname"=>$firstname->firstname,  //If firstname is object

"firstname"=>$firstname['firstname'],  //If firstname is array

【讨论】:

    【解决方案2】:

    所以我已经找到了解决方案,只是使用查询解决了我的问题。现在我现在面临的唯一问题是$firstname = DB::select('select firstname from clients where id='.$clientid); 我得到了一个数组,但为了导出到表,我也使用了一个数组,我无法在 $data 数组中传递 $firstname 数组

    $data=array(
            "uuid"=>$id,
            "title"=>$title,
            "residence"=>$residence[0],
            "startdate"=>$startdate,
            "enddate"=>$enddate,
            "starttime"=>$starttime,
            "endtime"=>$endtime,
            "description"=>$description,
            "firstname"=>$firstname,
            "lastname"=>$lastname,
            "housing"=>$housing,
            "housenr"=>$housenr
    
        );
        //dd($data);
        DB::table('tasks')->insert($data);
        return redirect('/todo');
    

    errorcode picture

    【讨论】:

    • @mohammad.kaab 我现在正在努力解决这个问题
    猜你喜欢
    • 2021-09-26
    • 2011-10-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多