douling8087 2016-01-15 08:30
浏览 34
已采纳

Laravel 5.2 getNameOrUsername方法不起作用

I am searching for data, as in usernames. I got it to work by using a static name like <p>Jordan1</p> but when I want to make it dynamic as in having my actual users' name reflect on the page it doesn't work when I used this method

{{ $user->getNameOrUsername() }}

In return I get this error:

BadMethodCallException in Builder.php line 2130:

Call to undefined method Illuminate\Database\Query\Builder::getNameOrUsername()


userblock.blade.php

    <div class="media">
        <a class="pull-left" href="#">
            <img class="media-object" alt="{{ $user->getNameOrUsername() }}" src="">
        </a>
        <div class="media-body">
            <h4 class="media-heading">
            <a href="#">{{ $user->getNameOrUsername() }}</a>
            </h4>
                @if ($user->location)
                    <p>{{ $user->location }}</p>
                @endif
        </div>
    </div>

results.blade.php

@include('layouts.--header')

<div class="container">
    <h4>Your search for "{{ Request::input('query') }}"</h4>

    @if (!$users->count())
        <p> No results found, sorry.</p>
    @else
    <div class="row">
        <div class="col-lg-12">
            @foreach ($users as $user)
                @include('search.userblock')
            @endforeach
        </div>
    </div>
    @endif
</div>

@include('layouts.--footer')

SearchController.php

<?php

namespace SCM\Http\Controllers;

use Illuminate\Http\Request;

use DB;
use SCM\User;
use Illuminate\Http\Requests;

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

        if (!$query) {
            return redirect ()->route('welcome');

        }

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

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

routes.php

<?php

/*
|--------------------------------------------------------------------------
| Routes File
|--------------------------------------------------------------------------
|
| Here is where you will register all of the routes in an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/


/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
*/

Route::group(['middleware' => ['web']], function () {
    Route::get('/login', function () {
    return view('auth/login');
});
    Route::get('/register', function () {
    return view('auth/login');
});

});

Route::group(['middleware' => 'web'], function () {
    Route::auth();

    Route::get('/', [
    'as' => 'welcome', 'uses' => 'WelcomeController@index'
]);

    Route::get('/profile', function () {
    return view('layouts/-profile');
});

    Route::get('profile/{username}', function () {
    return view('layouts/-profile');
});

    Route::get('settings/{username}', function () {
    return view('layouts/-settings');
});

    Route::get('/settings', function () {
    return view('layouts/-settings');
});


    Route::get('/home', 'HomeController@index');
});

/**
* Search
*/
Route::get('/search', [
    'as' => 'search.results', 'uses' => 'SearchController@getResults'
]);
  • 写回答

2条回答 默认 最新

  • duanji9378 2016-01-15 08:42
    关注

    It's looking for the getNameOrUsername() method on your User model. You need to define this method.

    FYI, if it doesn't find the method on your Model, it looks for it on the Illuminate\Database\Eloquent\Builder object. If it doesn't find it there, it looks for it on the Illuminate\Database\Query\Builder object. If it doesn't find it there, it throws the exception. That's why it is a "BadMethodCallException" on Illuminate\Database\Query\Builder.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大