0

On my search having $categroy_id - $country_id - $city_id. there is a table activities with all that value.

I just implement a function in my controller but its return all data.

My controller function code:

public function PlanActivity(Request $request){
    $category_id = $request->category_id;
    $countryid = $request->country_id;
    $cityid = $request->city_id;
    $listactivity = Activity::all(); // get all activity
    if($category_id != '') {
        $listactivity->where('category_id', function ($query) use ($category_id) {
            $query->where('category_id', $category_id);
        });
    }

    return view('front.plan_activity',compact('listactivity'));
}

How can i do this?

4 Answers 4

5

Use multiple where clauses:

Query Builder

// match any one of the values
$activities = DB::table('activities')->where('category_id', $category_id)
                       ->orWhere('country_id', $country_id)
                       ->orWhere('city_id', $city_id)
                       ->get();

// match all of the values
$activities = DB::table('activities')->where('category_id', $category_id)
                       ->where('country_id', $country_id)
                       ->where('city_id', $city_id)
                       ->get();

Eloquent

// match any one of the values
$activities = Activity::where('category_id', $category_id)
                       ->orWhere('country_id', $country_id)
                       ->orWhere('city_id', $city_id)
                       ->get();

// match all of the values
$activities = Activity::where('category_id', $category_id)
                       ->where('country_id', $country_id)
                       ->where('city_id', $city_id)
                       ->get();

// this can be merged together in one array
$activities = Activity::where([
     'category_id' => $category_id,
     'country_id' => $country_id,
     'city_id' => $city_id
])->get();

If the request parameters are null

public function PlanActivity(Request $request){
    if (!$categroy_id && !$country_id && !$city_id) {
        $activities = Activity::all();
    } else {
        // do the above queries
    }

    return view('front.plan_activity',compact('activities'));
}
Sign up to request clarification or add additional context in comments.

3 Comments

i want to set this query on model ? how can i do this?
one more thng that if do not have $categroy_id - $country_id - $city_id is null return all activity..how can i set.
First you need to create a model for your activity table. class Activity extends Model { protected $table = 'activities'; //table name protected $guarded = []; } Then you can write queries like he said above $activities = Activity::where('category_id', $category_id) ->orWhere('country_id', $country_id) ->orWhere('city_id', $city_id) ->get();
0

Your mistake is in the use of the eloquent functions that Laravel's models provide. When you call the all() function, it basically does a SELECT * FROM your table and returns the results as an object. If you need to filter this query or add anything, you will no longer use all(), but instead use get() as the last function.

As for setting this query on the model, you are already inside the model! When you call Activity::whatever-function-next() you are inside the model, and Laravel already gives you all these functions to create you query with little effort. The first answer gave you all you need, just be sure to understand what those functions and classes are actually doing.

Comments

0

First you need to create a model for your activity table.

class Activity extends Model
{
    protected $table = 'activities'; //table name
    protected $guarded = [];
}

Then you can write queries like he said above

$activities = Activity::where('category_id', $category_id)
                           ->orWhere('country_id', $country_id)
                           ->orWhere('city_id', $city_id)
                           ->get();

Comments

0

You can use this query to perform search,

public function PlanActivity(Request $request)
{
    $category_id = $request->category_id;
    $country_id = $request->country_id;
    $city_id = $request->city_id;
    $activity = new Activity; // get all activity
    $search = $activity->search();
    if($category_id) {
       $search->where('category_id', $category_id);
    }
    if($country_id){
       $search->where('country_id', $country_id);
    }
    if($city_id){
      $search->where('city_id', $city_id);
    }


    $listactivity = $search->

    return view('front.plan_activity',compact('listactivity'));
}

Comments

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.