Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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
Laravel 拉威尔未定义性质_Laravel_Search - Fatal编程技术网

Laravel 拉威尔未定义性质

Laravel 拉威尔未定义性质,laravel,search,Laravel,Search,几天来,我一直试图让我的搜索系统完全正常工作,但由于某种原因,我正在使用的一个变量不断出现错误,我不知道为什么,错误是“Undefined property:Illumb\Database\Eloquent\Collection::$firstName”,我的代码如下: userblock.blade.php <div class="media"> <a class="pull-left" href="{{ route('profile/index', ['firstName'

几天来,我一直试图让我的搜索系统完全正常工作,但由于某种原因,我正在使用的一个变量不断出现错误,我不知道为什么,错误是“Undefined property:Illumb\Database\Eloquent\Collection::$firstName”,我的代码如下:

userblock.blade.php

<div class="media">
<a class="pull-left" href="{{ route('profile/index', ['firstName' => $users->firstName]) }}">
    <img class="media-object" alt="{{ $users->getNameOrUsername() }}" src="{{ $users->getAvatarUrl() }}">
</a>
<div class="media-body">
    <h4 class="media-heading"><a href="{{ route('profile/index', ['firstName' => $users->firstName]) }}">{{ $users->getNameOrUsername() }}</a></h4>
</div>
@if ($users->currentLocation)
    <p>{{ $users->currentLocation }}</p>
@endif

@如果($users->currentLocation)
{{$users->currentLocation}

@恩迪夫

SearchController.php

<?php 

namespace App\Http\Controllers;

use DB;
use App\User;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Http\Request;

class SearchController extends BaseController
{
public function getResults(Request $request)
{   
    $query = $request->input('query');

    if (!$query) {
        return back();
    }

    $users = User::where(DB::raw("CONCAT(firstName, ' ', lastName)"), '
        LIKE', "%{$query}%")
        ->orWhere('username', 'LIKE', "%{$query}%")
        ->get();

    return view('search/results')->with('users', $users);
}
}

您现在获得了一个用户集合,您只需要一个,即与WHERE子句匹配的用户集合。试试这个:

$users = User::where(DB::raw("CONCAT(firstName, ' ', lastName)"), '
    LIKE', "%{$query}%")
    ->orWhere('username', 'LIKE', "%{$query}%")
    ->first();

这将不再返回集合,只返回单个用户。让我知道它是否有效

您现在获得了一个用户集合,您只需要一个,即与WHERE子句匹配的用户集合。试试这个:

$users = User::where(DB::raw("CONCAT(firstName, ' ', lastName)"), '
    LIKE', "%{$query}%")
    ->orWhere('username', 'LIKE', "%{$query}%")
    ->first();
这将不再返回集合,只返回单个用户。让我知道它是否有效