Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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 5在JSON文件中搜索并显示数据_Php_Json_Laravel_Search_Collections - Fatal编程技术网

Php Laravel 5在JSON文件中搜索并显示数据

Php Laravel 5在JSON文件中搜索并显示数据,php,json,laravel,search,collections,Php,Json,Laravel,Search,Collections,我有一个带typeahead的搜索引擎。我想要的是,在进行搜索并提交后,显示结果。这会产生两个问题:第一,它返回一个空数组,第二,它不允许我访问属性,告诉我它不是对象 在controller中,我使用collect()来访问属性,但它不起作用,在哪里也不起作用 public function store(Request $request) { $url = 'storage/json/es/noticia.json'; $datos = file_get_contents($ur

我有一个带typeahead的搜索引擎。我想要的是,在进行搜索并提交后,显示结果。这会产生两个问题:第一,它返回一个空数组,第二,它不允许我访问属性,告诉我它不是对象

在controller中,我使用collect()来访问属性,但它不起作用,在哪里也不起作用

public function store(Request $request)
{
    $url = 'storage/json/es/noticia.json';
    $datos = file_get_contents($url);
    $data = json_decode($datos, true);
    $data = collect($data)->where("title","LIKE","%{$request->texto}%")->all();

    return view('web.buscar.index', compact('data'));
}
如果我使用
$data=collect($data)->all()我可以看到集合:

array:8 [▼
  0 => []
  1 => array:4 [▼
    "id" => 2
    "title" => "There is a title"
    "lead" => "There is a lead"
    "slug" => "there-is-a-title"
  ]
  2 => array:4 [▶]
  3 => array:4 [▶]
  4 => array:4 [▶]
  5 => array:4 [▶]
  6 => array:4 [▶]
  7 => array:4 [▶]

]
然后,如果我在视图中尝试:
$value->title
,我会出现错误:尝试获取非对象的属性“title”。在视图中我有:

{!! Form::open([
    'route' => 'buscar',
    'id' => 'buscar',
    'name' => 'buscar',
    'class' => 'buscador col-xs-12',
    'method' => 'POST',
    'accept-charset' => 'utf-8'
    ]) !!}

    <input id="texto" name="texto" class="input_buscador typetitulo" autocomplete="off" type="text"/>

    {!! HTML::image('images/web/icons/lupa.svg', '',  array('height' => '30', 'class' => 'boton_buscador', 'onclick' => 'document.buscar.submit()') ) !!}

{!! Form::close() !!}

@if(isset($data))
    @foreach($data as $value)
        <span>{{$value->title}}</span><br>
    @endforeach
@endif
{!!表单::打开([
“路线”=>“客车”,
'id'=>'buscar',
'name'=>'buscar',
'class'=>'buscador col-xs-12',
'方法'=>'发布',
“接受字符集”=>“utf-8”
]) !!}
{!!HTML::image('images/web/icons/lupa.svg','',数组('height'=>'30','class'=>'boton\u buscador','onclick'=>'document.buscar.submit()'))
{!!Form::close()!!}
@if(isset($data))
@foreach(数据为$value)
{{$value->title}}
@endforeach @恩迪夫
如果我使用
$data=collect($data)->pull('title')'title'
,但这不是我想要的,因为我还需要访问其他属性。
有什么想法吗?提前谢谢

这是失败的,因为数组中的第一个数组没有任何值,因此您将获得未定义的索引,通过执行以下操作删除所有空数组

    public function store(Request $request)
{
    $url = 'storage/json/es/noticia.json';
    $datos = file_get_contents($url);
    $data = json_decode($datos, true);

    $data = array_filter($data);

    $data = collect($data)->where("title","LIKE","%{$request->texto}%")->all();
    return view('web.buscar.index', compact('data'));
}
或者,您可以测试它是否在您的foreach中

 @foreach($data as $value)
    <span>{{$value->title ?? ''}}</span><br>
@endforeach

您可以使用stristr etc编辑更精细的返回值

您的集合是一个数组,而不是一个对象,请尝试
$value['title']
而不是
$value->title
我真的不明白您从哪里得到的
where
集合与SQL
where
相同。另外,只使用一种语言编写代码,看看当您将两种语言混合在一起(
$request->texto
)时它是什么样子。您的第一个值没有任何内容,因此将失败哦!我忘记了,如果我使用
$value['title']
我有一个错误:未定义的索引:title谢谢你@rchatburn现在开始工作了。我只需要解决如何对json文件执行where操作,直到集合为空为止。似乎
$data=collect($data)->where('title','LIKE',“%{$request->texto}%”->all()它不起作用。请尝试删除{并将所有更改为get?如果使用get(),则会出现以下错误:类型错误:如果仅删除{},则函数illumb\Support\Collection::get()的参数太少这是同样的结果:一个空的集合可能我不能在集合中使用LIKE,但必须有一种方法来做到这一点。Laravel不支持在集合中使用where LIKE。
    collect($data)->filter(function ($item) use ($request) {
        return $item->title == $request->texto;
    )