rory's avatar
Level 1

Passing string (path) from view to controller

I have a button that the user shall use to upload csv file. Then this csv is read in the controller and uploaded in a database.

This is my view:

<div class="upload-btn-wrapper">
    <button class="btn"> Upload csv file </button>
    <input type="file" name="myfile"/>
</div>
</form>

In my controller I just have the $path in the variable where the path to the file is stored:

public function read_xsl()
{

    $path = '../data/blog-urls-gartenbau.csv';
    if (($handle = fopen($path, "r")) !== FALSE)
    {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
        {
            $num = count($data);
            for ($c=0; $c < $num; $c++)
            {
                $rawUrl = $data[$c];
                $hashedUrl = sha1($data[$c]);
                \DB::table('job_urls')->insert(
                    [
                        'job_id' => 1,
                        'url' => $rawUrl,
                        'hash' => $hashedUrl,
                        'created_at' =>  \Carbon\Carbon::now(), # \Datetime()
                        'updated_at' => \Carbon\Carbon::now(),
                    ]);
            }
        }
        fclose($handle);
    }
     return view('form');
}

Which is the best way to pass data from view to controller? Thanks for the help.

0 likes
3 replies
Sinnbeck's avatar
return view('form')->with('path', $path);
rory's avatar
Level 1

@Resin thanks for the answer, but I think this is the solution to pass data from the controller to view. I'm looking for the opposite. I have data uploaded in the view and I want to pass it to the controller.

Please or to participate in this conversation.