0

I want to make a filter by supervisors, but when passing the name, it is passed through the underscore _ . Ok, I have replaced this when verify has() in $request. Example of the passing name into request

I'm not sure if this is the right way, but it works.

My Controller:

public function index(Request $request) {
$supervisors = User::where('role','supervisor')->get();
$filteredSupervisors = null;

foreach ($supervisors as $supervisor) {
            if ($request->has(str_replace(" ","_",$supervisor->name)))
                $filteredSupervisors[] = $supervisor->id;
}

if ($filteredSupervisors != null)
            $projectsQuery->whereIn('user_id', $filteredSupervisors);
$projects = $projectsQuery->paginate(4);

My View:

@foreach($supervisors as $supervisor)
    <label>{{ preg_replace('~^(\S++)\s++(\S)\S++\s++(\S)\S++$~u', '$1 $2.$3.', $supervisor->name) }} 
        <input name='{{ $supervisor->name }}' type='checkbox'>
    </label>
@endforeach
JaNix
  • 9
  • 5
  • rather than passing names as parameter. Use ids instead and also make it some parameter like `supervisor = [1,2,3]` which can be used as direct where clause – Dark Knight Oct 18 '20 at 10:10
  • @JitendraYadav Thank you for your comment. I thought about it, but in the search bar something like this will be: supervisor%b%d=1supervisor%b%d=2 . It looks awful to me, because i have other filters besides this – JaNix Oct 18 '20 at 10:19
  • That is actually standard way, but if you want you can do `supervisor=1,2,3` and then explode it in php to use in where clause. – Dark Knight Oct 18 '20 at 10:21
  • @JitendraYadav really? Could you give me some article/your code experience for making that. While i'm just beginner in laravel and php and can't do this on fly – JaNix Oct 18 '20 at 10:25
  • https://stackoverflow.com/questions/3061273/send-an-array-with-an-http-get check for weakly typed language like php – Dark Knight Oct 18 '20 at 10:27
  • @JitendraYadav Thank you very much, I will definitely read it! I am Russian and do not often use the English-language Internet. While you are here, you can give an article (including from the documentation, I didn't find it there) on how to filter if you have a many-to-many relationship (my project has tags that are connected by a pivot table with the project_id and tag_id fields). I searched all day today, then decided to postpone it for later. – JaNix Oct 18 '20 at 10:33

1 Answers1

0

I would recommend a bit of a different approach to the current method you are requesting the supervisor's names. Your current script is using the key of your request array as the data you are passing to your server. The problem with this is that finding the data you are looking for is far less intuitive and will slow down your script once more keys are added to those requests, as well as require far more work.

Change your post data to:

Array(3) {
  users[string:] => 'Sam_Xeros_Stack',
  date_start[string] => null,
  date_end[string] => null
}

Then you can, in your Controller, call the data in a much simpler way:

function index(Request $request)
{
  // Only call where role === 'supervisor'
  return User::where('role', 'supervisor')
              // get the names based on the `users` param
              ->whereIn('name', explode('_', $request->users))
              // Get the projects table relationship to that user
              ->with('projects')
              // fetch the data
              ->paginate(4);
}

I am assuming you already have a relationship in the users and projects table since you mentioned that in your original post.

However, you can also define the relationships pretty simply if you haven't already:

Users.php

public function projects() {
  return $this->hasMany('App\Project');
}

Projects.php

public function user() {
  return $this->belongsTo('App\User');
}

This will return something like:

[
  [
    'id' => 1
    'name' => 'Sam',
    'projects' => [...]
  ],
  [...]
]

This does restructure some of your logic, but I think in the future this will be far easier for your to reference.

Having to deal with array keys is never fun, and this lets Laravel do the most heavy lifting for you with one simple query, and no SQL commands.

Sam
  • 2,856
  • 3
  • 18
  • 29
  • Thank you very much for you asnwer! However, I need to make a search by checbox'es and now my code: $projectsQuery = Project::query() and depends on condition I add where closures in $projectsQuery, for example, $projectsQuery->whereIn('state',$filteredStates); – JaNix Oct 21 '20 at 02:15
  • @Xeros what do you mean by the checkboxes? You can add conditions to relationships if you need to filter it. – Sam Oct 21 '20 at 17:10
  • really, ok. Thank you! https://stackoverflow.com/questions/64462459/how-to-save-pre-existing-gets-parameters-values-and-add-new-ones-to-route?noredirect=1#comment113986204_64462459 Could you who this my topic? This is a more complex question, which probably no one will answer, it concerns how to pass get parameters on top of others (site.ru/previousGetParams&currentGetParams) I want to add current Get Params in this way. – JaNix Oct 22 '20 at 08:18