Error Call to a member function count() on string Cara ngatasinnya gimana ea

Pas lagi ngebuat metode searching udh ngeliat tutorialnya masih kagak bisa udh gagal paham walau nyari di google http://imageshack.com/a/img922/4046/hCUZcg.jpg http://imageshack.com/a/img924/3006/dnL4p7.jpg http://imageshack.com/a/img922/2906/khnKZk.jpg

<?php

namespace App\Http\Controllers;
use DB;
use App\Models\User;
use Illuminate\Http\Request;

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

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

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

    return \View::make('search/result')->with('users', '$users');
  }
}

@extends('templates.default')

@section('content')

    <h3>Your Result is"{{ Request::input('query') }}"</h3>

    @if ( !$users->count() )
        <p>No Result Found</p>
    @else

    <div class="row">
      <div class="col-lg-12">
        @foreach ($users as $user)
            @include('user/partials/userblock')
        @endforeach

      </div>
    </div>
@endif
@stop

avatar mikuady
@mikuady

30 Kontribusi 4 Poin

Diperbarui 7 tahun yang lalu

7 Jawaban:

pastein kode agan dimari

avatar kartadinata
@kartadinata

121 Kontribusi 78 Poin

Dipost 7 tahun yang lalu

coba scriptnya di post di sini aja mas.. biar enak keliatannyaa.. :)

avatar gunalirezqimauludi
@gunalirezqimauludi

350 Kontribusi 243 Poin

Dipost 7 tahun yang lalu

<?php

namespace App\Http\Controllers;
use DB;
use App\Models\User;
use Illuminate\Http\Request;

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

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

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

    return \View::make('search/result')->with('users', '$users');
  }
}

avatar mikuady
@mikuady

30 Kontribusi 4 Poin

Dipost 7 tahun yang lalu

Jawaban Terpilih

coba bagian

....

    @if ( !$users->count() )
        <p>No Result Found</p>
....

ubah ke


    @if ( count($users) == 0 )
        <p>No Result Found</p>

avatar gunalirezqimauludi
@gunalirezqimauludi

350 Kontribusi 243 Poin

Dipost 7 tahun yang lalu

ErrorException in 02fc26e581969aaf89bc92cb727a0871 line 12: Invalid argument supplied for foreach() (View: C:\xampp\htdocs\potato\resources\views\search\result.blade.php)

<?php

namespace App\Http\Controllers;
use DB;
use App\Models\User;
use Illuminate\Http\Request;

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

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

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

    return \View::make('search/result')->with('users', '$users');
  }
}

avatar mikuady
@mikuady

30 Kontribusi 4 Poin

Dipost 7 tahun yang lalu

apa yang harus di ubah lagi

avatar mikuady
@mikuady

30 Kontribusi 4 Poin

Dipost 7 tahun yang lalu

$users itu datanya array apa string? Di errornya dia menghitung string soalnya, jadi nggak bisa. Pastikan dulu kalau datanya array.

die(var_dump($users));

// Dan coba ini diganti
return \View::make('search/result')->with('users', '$users');
// Jadi gini
return \View::make('search/result')->with('users', $users);

avatar dalikewara
@dalikewara

351 Kontribusi 260 Poin

Dipost 7 tahun yang lalu

Login untuk ikut Jawaban