从laravel到刀片检索数据

时间:2018-08-09 01:40:02

标签: php database laravel laravel-blade

我是laravel的初学者,在uni,我们的任务是从名为pms.php的文件中检索数据,该文件包含主要部长的所有结果,例如姓名,年份和州。不管我做什么,我都无法正常工作。

这是我的web.php文件:

require(app_path().'/pms.php'); 

// To do: Display search form
Route::get('/', function()
{
    return view('search_form');
});

// To do: Perform search and display results
Route::get('search', function()
{
    //$name = request('name');    
    //$year = request('from', 'to');
    //$state = request('state');
    $pms = search('name','from','to','state');
    return view('search')->with('name', $pms)
           ->with('from', $pms)->with('to', $pms)->with('state', $pms);
});


/* Functions for PM database example. */

/* Search sample data for $name or $year or $state from form. */
function search($name, $year, $state) {
  $pms = getPms();

// Filter $pms by $name
if (!empty($name)) {
  $results = array();
  foreach ($pms as $pm) {
    if (stripos($pm['name'], $name) !== FALSE) {
      $results[] = $pm;
    }
  }
  $pms = $results;
}

// Filter $pms by $year
if (!empty($year)) {
  $results = array();
  foreach ($pms as $pm) {
    if (strpos($pm['from'], $year) !== FALSE || 
        strpos($pm['to'], $year) !== FALSE) {
      $results[] = $pm;
    }
  }   
  $pms = $results;
}

// Filter $pms by $state
if (!empty($state)) {
    $results = array();
    foreach ($pms as $pm) {
      if (stripos($pm['state'], $state) !== FALSE) {
        $results[] = $pm;
      }
    }
    $pms = $results;
}

  return $pms;
}

这是我的search.blade.php文件

@extends('layouts.master')

@section('title')
Query Search Result 
@endsection
@section('content')
Search result for {{ $name }}  {{ $year }}  {{ $state }}
@endsection

这是我的search_form.blade.php

@extends('layouts.master')

@section('title')
Search Form
@endsection

@section('content')
<p>
 <h2>Australian Prime Ministers</h2>
 <h3>Query</h3>

 <form method="get" action="search">
{{csrf_field()}}
 <table>
 <tr><td>Name: </td><td><input type="text" name="name"></td></tr>
 <tr><td>Year: </td><td><input type="text" name="year"></td></tr>
 <tr><td>State: </td><td><input type="text" name="state"></td></tr>
 <tr><td colspan=2><input type="submit" value="Search">
                  <input type="reset" value="Reset"></td></tr>
<table>
</form>
</p>
@endsection

你们知道为什么它不起作用吗?我现在面临的问题是:

  

htmlspecialchars()期望参数1为字符串,为给定的数组(查看:/home/ubuntu/workspace/Week4/assoc-laravel/resources/views/search.blade.php)

1 个答案:

答案 0 :(得分:0)

以compact()发送数据, 返回View :: make('foo.bar')

-> with(compact('test'));

或 推荐-> 返回视图('foo.bar',紧凑型('your_php_variable'))