Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用Laravel查询将两列值合并为下拉列表的选项值_Php_Laravel - Fatal编程技术网

Php 如何使用Laravel查询将两列值合并为下拉列表的选项值

Php 如何使用Laravel查询将两列值合并为下拉列表的选项值,php,laravel,Php,Laravel,例如,我有两张桌子 表A、表B table_A id username 5 kumar 6 pavan table_B id userid rname 1 testing 2 5 -- 3 newtest 4 6 -- 我想要的是 <select> <option value='1'>testing</option> <option value='2'>kumar</op

例如,我有两张桌子

表A、表B

table_A
id username
5  kumar
6  pavan

table_B
id userid rname
1         testing
2    5    --
3         newtest
4    6    --
我想要的是

<select>
  <option value='1'>testing</option>
  <option value='2'>kumar</option>
  <option value='3'>newtest</option>
  <option value='4'>pavan</option>
</select>

测试
库玛
纽特
帕万

如果rname为空意味着(--),那么根据表_B中的用户ID,从表_A中获取用户名,那么我们如何使用带有Pulk的laravel查询来显示(

使用laravel
belongsTo()
关系。制作两个模型,分别为
表A
表B
。在
TableB
模型中,建立与
TableA
类似的关系-

public function tableA()
{
    return $this->belongsTo(TableA::class,'userid');
}
然后在你的控制器里-

$tableb_datas = TableB::with('tableA')->get();

return view('your_view')->with('tableb_datas', $tableb_datas);
最后在你看来-

<select>
    @foreach($tableb_datas as $b)
          @if($b->rname!="--")
             <option value='{{$b->id}}'>{{$b->rname}}</option>
          @else
             <option value='{{$b->id}}'>{{$b->tableA->username}}</option>
          @endif
    @endforeach
</select>

@foreach(表b_数据为$b)
@如果($b->rname!=“--”)
{{$b->rname}
@否则
{{$b->tableA->username}
@恩迪夫
@endforeach

使用Laravel
belongsTo()
关系。制作两个模型,分别为
表A
表B
。在
TableB
模型中,建立与
TableA
类似的关系-

public function tableA()
{
    return $this->belongsTo(TableA::class,'userid');
}
然后在你的控制器里-

$tableb_datas = TableB::with('tableA')->get();

return view('your_view')->with('tableb_datas', $tableb_datas);
最后在你看来-

<select>
    @foreach($tableb_datas as $b)
          @if($b->rname!="--")
             <option value='{{$b->id}}'>{{$b->rname}}</option>
          @else
             <option value='{{$b->id}}'>{{$b->tableA->username}}</option>
          @endif
    @endforeach
</select>

@foreach(表b_数据为$b)
@如果($b->rname!=“--”)
{{$b->rname}
@否则
{{$b->tableA->username}
@恩迪夫
@endforeach
您可以使用函数。
merge()
方法将给定数组或集合与原始集合合并

$first = table_A::all();
$second = table_B::all();

$finalResult = $first->merge($second);
$result = $finalResult->all();


$finalResult->each(function($record) {
    echo $record-><fieldName>.'<br />';
});
$first=table_A::all();
$second=table_B::all();
$finalResult=$first->merge($second);
$result=$finalResult->all();
$finalResult->each(函数($record){
echo$record->.
; });
希望这对你有帮助

您可以使用函数。
merge()
方法将给定数组或集合与原始集合合并

$first = table_A::all();
$second = table_B::all();

$finalResult = $first->merge($second);
$result = $finalResult->all();


$finalResult->each(function($record) {
    echo $record-><fieldName>.'<br />';
});
$first=table_A::all();
$second=table_B::all();
$finalResult=$first->merge($second);
$result=$finalResult->all();
$finalResult->each(函数($record){
echo$record->.
; });

希望这对你有帮助

Try:
$first=ModelName::where(“”,)->get()$second=Modelname::where(“”,)->get()$finalResult=$first->merge($second)$finalResult->each(函数($record){echo$record->.
;})尝试:
$first=ModelName::where(“”,)->get()$second=Modelname::where(“”,)->get()$finalResult=$first->merge($second)$finalResult->each(函数($record){echo$record->.
;})