Ad

Laravel 5.2 GetNameOrUsername Method Not Working

- 1 answer

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' => '[email protected]'
]);

    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', '[email protected]');
});

/**
* Search
*/
Route::get('/search', [
    'as' => 'search.results', 'uses' => '[email protected]'
]);
Ad

Answer

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.

Ad
source: stackoverflow.com
Ad