mardi 30 janvier 2018

Laravel search isset error

I try to get my search result, where my search form work with 2 conditions, search in products and search in posts.

If I search in products i will get results, but if search in posts I will get error Trying to get property of non-object which comes from products brand name in my products result part.

Here is my blade code summarized :

    @if(isset($posts))
      @forelse($posts as $post)
        
      @empty
        No Result! Try another keywords
      @endforelse
    @endif


    @if(isset($products))
      @forelse($products as $product)
        
      @empty
        No Result! Try another keywords
      @endforelse

    @endif

my controller:

<?php

namespace App\Http\Controllers\frontend;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Session;
use DB;
use Carbon\Carbon;
use App\Product;
use App\Post;
use App\Brand;

class SearchController extends Controller
{
    //header search (used in all pages header part)
    public function search(Request $request) {
        $search = request('search');
        $searchType = request('searchType');

        if(strcmp($searchType, "posts") == 0){
          $posts = Post::where('title', 'like', "%{$search}%")
          ->orWhere('description', 'like', "%{$search}%")
          ->get();
        }elseif(strcmp($searchType, "products") == 0){
          $products = Product::where('title', 'like', "%{$search}%")
          ->orWhere('description', 'like', "%{$search}%")
          ->get();
        }

        return view('front.search', compact('posts', 'products'));
    }
}

my route:

//Header Search
Route::any('/search', 'frontend\SearchController@search')->name('search');



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2DPrXHC
via IFTTT

Aucun commentaire:

Enregistrer un commentaire