Patrick Schocke
Patrick Schocke

Reputation: 1491

Laravel make mulitselect array result into string

I have one problem that I can't solve:

I have a form with a lot inputs. To keep the request clean I have sorted the input names with arrays. Everything works as expected. The only problem is I can't access the values of a multiselect input.

My input field:

<select multiple name="company[jobs][]">
    <option value="" disabled selected>Beschäftigte Berufe</option>
    <option value="ABC">ABC</option>
    <option value="DEF">DEF</option>
    <option value="GHI">GHI</option>
</select>

And I try to convert this into a string (each option seperated with a ;) with this php code (it is a function, but that doesn't matter here). This code is placed inside the company model and I call the method with $company->shortEdit($request->company);.

The method head looks like this:

public function shortEdit($request) {

    $jobs = "";
    foreach($request->jobs as $job) {
        $jobs = $jobs . ";" . $job;
    }

}

But I get this error:

Trying to get property 'jobs' of non-object

How can I fix this? The code works perfectly fine if my select isn't a multidimensional array.

And yes, I dumped the request and the array company[jobs] isn't empty.

Upvotes: 0

Views: 51

Answers (2)

spartyboy
spartyboy

Reputation: 426

public function shortEdit($request) {

    $jobs = "";
    foreach($request->input('company.jobs') as $job) {
        $jobs = $jobs . ";" . $job;
    }

}

Upvotes: 0

hossamGamal
hossamGamal

Reputation: 171

name of your input is company so the correct way is

$request->company['jobs'];

Upvotes: 1

Related Questions