banda
banda

Reputation: 402

why did not properly redirect if else in controller in Laravel?

I am working with laravel 5.6. and in my application I have table name vehicles as like this,

id  name   model    year  categoryname  user_id
1   toyota  121     2001      car           1
2   nissan  sunney  1998      car           2
3   toyota  liteace 2000      van           5
4   isuzu   elf     2005      truck         9
5   bmw     520d    2010      car           7

now I need redirect each vechicle edit form acording to categoeyname of the vechicle table. I have controller edit function like this?

public function edit($id)
{
    if (Vehicle::where('categoryname', 'Car')) {

        $vehicles = Vehicle::find($id);
        $town_list = Town::with('district.province')->groupBy('provincename')->get();
        $model_list = DB::table('modols')

            ->groupBy('categoryname')
            ->get();
        return view('vehicles.caredit')->withVehicles($vehicles)->withTown_list($town_list)->withModel_list($model_list);
    }
    else if (Vehicle::where('categoryname', 'Van')){
        $vehicles = Vehicle::find($id);


        $town_list = Town::with('district.province')->groupBy('provincename')->get();
        $model_list = DB::table('modols')

            ->groupBy('categoryname')
            ->get();
        returnview('vehicles.vanedit')->withVehicles($vehicles)->withTown_list($town_list)->withModel_list($model_list);
    }
}

but when I redirect diffrent categoryname values like car and van those are allways redirect to caredit.blade.php. then how can I fix this problem?

Upvotes: 1

Views: 36

Answers (2)

aynber
aynber

Reputation: 23010

You're just getting the query builder object instead of restricting the search. However, I'm going to guess that you want to switch the view based on the specific model's category name. In that case, you want to check the model's categoryname value:

public function edit($id)
{
    $vehicles = Vehicle::find($id);
    $town_list = Town::with('district.province')->groupBy('provincename')->get();
    $model_list = DB::table('modols')

        ->groupBy('categoryname')
        ->get();
    if ($vehicles->categoryname == 'Car') {
        return view('vehicles.caredit')->withVehicles($vehicles)->withTown_list($town_list)->withModel_list($model_list);
    }
    else if ($vehicles->categoryname == 'Van'){
        return view('vehicles.vanedit')->withVehicles($vehicles)->withTown_list($town_list)->withModel_list($model_list);
    }
}

If the formatting of your templates always uses the same format you could also use the following to reduce the amount of if-else statements:

public function edit($id)
{
    $vehicles = Vehicle::find($id);
    $town_list = Town::with('district.province')
                 ->groupBy('provincename')
                 ->get();
    $model_list = DB::table('modols')
                  ->groupBy('categoryname')
                  ->get();

    $categoryName = strtolower($vehicles->categoryname);
    return view('vehicles.'.$categoryName.'edit')
           ->withVehicles($vehicles)
           ->withTown_list($town_list)
           ->withModel_list($model_list);

Upvotes: 2

Danyal Sandeelo
Danyal Sandeelo

Reputation: 12391

change

Vehicle::where('categoryname', 'Car')

to

Vehicle::where('categoryname', 'Car')->get();

You are not accessing the actual result set in your code, you need to call ->get() function.

A good practice would be to get the result set and check if it's Van or Car and based on that redirect.

Upvotes: 0

Related Questions