0

I am new to laravel and trying to have a result. My code is this:

class GegonosController extends Controller
{
    public function index($gid = null, $cid = null, $nid = null)
    {
        if (is_null($cid) and is_null($nid) and is_null($gid)) {
            $gegon = Gegono::orderBy('created_at','desc')->get(); 
        }
        else{
            if(!is_null($gid)) {
                if($gid == 0) {
                    $gegon = Gegono::where('gegtype_id','>',1);
                }
                else{
                    $gegon = Gegono::where('gegtype_id', '=', $gid);   
                }
            }
            else {
                $gegon = Gegono::where('gegtype_id','>',0);
            }
            if (!is_null($cid)) {
                $gegon->where('city_id', '=',$cid);
            }
            if (!is_null($nid)) {
                $gegon->where('nomos_id','=', $nid);
            }        
            $gegon->orderBy('created_at','desc')->get(); 
        }                        
        $nomoi = \App\Nomoi::orderby('name')->get();
        return view('front.gegonota', compact('gegon','gid','cid','nid','nomoi'));
    }

In the first, if when all variables are null the result is a collection of all the records of the table. In all other, if (other cases) the result is a builder and I get no results,

It doesn't show any error but gives no results.

Any help would save me a lot of time.

1 Answer 1

1

Query Builder's get() method returns the collection of records. In your code, you just call get() method in the air, so it does nothing. You should assign it to a variable or use it in an expression.

Source: https://laravel.com/docs/5.4/eloquent#collections

Sign up to request clarification or add additional context in comments.

3 Comments

Wait a second i assign it in the variable $gegon i think its obvious. And then the variable passes in view front.gegonota. This Is not the problem. Something else.
In your if, yes. But in your else it's not assigned. There is a Query Builder in your $gegon variable and you call get() on it, but it doesn't mean $gegon becomes a collection (like I explained in my answer). Try this: $gegon = $gegon->orderBy('created_at','desc')->get(); .
I already told you what is wrong here, now it's your turn to just apply it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.