【问题标题】:Laravel 5.2 getNameOrUsername method not workingLaravel 5.2 getNameOrUsername 方法不起作用
【发布时间】:2016-01-15 17:19:39
【问题描述】:

我正在搜索数据,例如用户名。我通过使用像<p>Jordan1</p> 这样的静态名称让它工作,但是当我想让它动态化时,让我的实际用户的名字反映在页面上,当我使用这种方法时它不起作用

{{ $user->getNameOrUsername() }}

作为回报,我得到了这个错误:

Builder.php 第 2130 行中的 BadMethodCallException:

调用未定义的方法 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'
]);

【问题讨论】:

  • 我没有看到你创建的方法getNameOrUsername()。不过,Blade 只会让你做{{ $user-&gt;name or $user-&gt;username }}。也许你的问题不清楚。
  • 忘记添加我在 Builder.php 第 2130 行收到此错误 BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::getNameOrUsername()
  • 我猜你的 User 模型没有 getNameOrUsername() 方法。你定义了吗?你能发布你的模型吗?
  • 没关系你的解决方案有效!谢谢!
  • @patricus 看起来就是这样。

标签: php laravel laravel-5.2


【解决方案1】:

它正在您的User 模型上寻找getNameOrUsername() 方法。你需要定义这个方法。

仅供参考,如果它在您的 Model 上找不到该方法,它会在 Illuminate\Database\Eloquent\Builder 对象上查找它。如果它在那里没有找到它,它会在 Illuminate\Database\Query\Builder 对象上查找它。如果它在那里没有找到它,它会抛出异常。这就是为什么它是 Illuminate\Database\Query\Builder 上的“BadMethodCallException”。

【讨论】:

    【解决方案2】:

    该方法不随 Laravel 提供。如果你想使用它,你需要创建它。我认为可以这样做:

    用户模型

    public function getNameOrUsername() {
    
        return $this->name ? $this->name : $this->username;
    
    }
    

    然后在你的刀片模板中使用它,比如

    Hi {{ $user-&gt;getNameOrUsername() }}

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 1970-01-01
      • 2016-04-01
      • 2017-01-26
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-02
      相关资源
      最近更新 更多