trevorpan's avatar

__construct() must be of the type int, array given (html number to integer)

Have been trying to get a html <input type="number" to be stored as an integer. (for money stored as cents)

https://laracasts.com/discuss/channels/laravel/how-to-convert-my-input-to-integer this post shows a number of techniques. I tried type casting in the model.

/**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'bid' => 'integer',
    ];

This generates the array given error. However, when it's commented out, the error still persists.

Argument 2 passed to Symfony\Component\HttpFoundation\RedirectResponse::__construct() must be of the type int, array given

here's the full error, I found a post on github about trust proxies, but the version I have includes that (5.8). protected $headers = Request::HEADER_X_FORWARDED_ALL;

I gave this below a shot, but it too gives the error.

 public function store(Job $job)
    {
        $this->validate(request(), [
            'bid' => 'required|integer|min:2'
        ]);

        $bid = (int) request('bid');

        // add bid to job
        $bid = auth()->user()->bids()->create([
            'bid' => $bid,
            'job_id' => $job->id
            ]);
...

It seems like such a basic thing but I must have something out of skew.

How have you solved this?

0 likes
7 replies
Snapey's avatar

The error is nothing to do with your model, its probably whatever you are doing after storing the model.

Your code should work fine, however I would probably not use $bid twice, especially as you don't need this temp variable at all.


$bid = auth()->user()->bids()->create([
            'bid' => (int) request('bid'),
            'job_id' => $job->id
            ]);
trevorpan's avatar

whoops, well here's what the issue was::::

return redirect('jobs.jobfull', compact('bid'));

after another search, this led me on a better track: https://laracasts.com/discuss/channels/laravel/symfony-component-debug-exception-fatalthrowableerror-e-recoverable-error-argument-2-passed-to-symfonycomponenthttpfoundationredirectresponse-construct-must-be-of-the-type-int-array-given-called-in-cxampphtdocskeyvendorlaravelframeworksrcilluminaterouting?page=1

So, I left the type $casts to integer on the model.

    public function store(Job $job)
    {
        $this->validate(request(), [
            'bid' => 'required|integer|min:2'
        ]);

        // add bid to job
        $bid = auth()->user()->bids()->create([
            'bid' => (int) request('bid'),
            'job_id' => $job->id
            ]);

        return back();
    }
Snapey's avatar

I was pretty much correct then...

trevorpan's avatar

@snapey I know! I wish there was a 50/50 solution.

Decided to leave the added (int) for good measure, but what lifted the error had to do with the redirect (which you noted in general). The error is a bit misleading, as I thought it was about converting the number to int.

Do you know why return back(); fixes this?

octoquad's avatar
octoquad
Best Answer
Level 4

The redirect helper is actually expecting a HTTP status code for argument 2.

Perhaps you meant:

   public function store(Job $job)
    {
        $this->validate(request(), [
            'bid' => 'required|integer|min:2'
        ]);

        // add bid to job
        $bid = auth()->user()->bids()->create([
            'bid' => (int) request('bid'),
            'job_id' => $job->id
            ]);

        return redirect(route('jobs.jobfull', $job->id));
    }

Unfortunately, I'm not sure if the route jobs.jobfull is a POST or GET route, but essentially after storing the data, you want to redirect back to the GET page e.g. jobs.index if you have that. This will essentially be translated to /jobs/2 if $job->id was 2.

I hope that helps.

Snapey's avatar

yep, you were trying to redirect with data (as if it were a view). Redirects don't work like that. You cannot pass data, only a url and a status code if you want to override the default 302

trevorpan's avatar

ok @octoquad your notes on the redirect were right. I found the index showed a loop and it returns to a paginated collection of jobs, not exactly what I was hoping for.

The jobfull gave: Route [jobs.jobfull] not defined. In this case jobfull is the "blowup or detail" page of a job. I think this is the right way to do this, essentially a collection of jobs to look at snippets, thumbnails and then go to a detail or jobfull page...

So, then I tried this:

return redirect(route('jobs.show', $job->id));

That solved the issue, without using back();

@snapey I'll be sure to not use a return view on page data (e.g. adding a bid to a job).

Thank you both for helping on this. So happy laracasts is here.

1 like

Please or to participate in this conversation.