mardi 19 juin 2018

Laravel fill DB table from form in view and display it back on the same view

I'm a newbie in Laravel framework and I'm trying to build a tool to administrate a database. I already created my table and I manage to fill it from view, what I do not get is to display the table back to the view. I built two functions, one that just displays the form 'Project_create@showpform' and the other that should update the same view with the inserted data 'Project_create@showtable'. I'm getting the error: "Undefined variable: project_data (View:/var/www/tcc/src/resources/views/form_project.blade.php)".

Here you can see my routes:

Route::get('/', 'DataController@overview');

Route::get('/myproject', 'Project_create@showpform');
Route::post('/myproject', 'Project_create@showtable');

Route::get('/upload','DataController@showform');
Route::post('/upload', 'DataController@read_xsl');

Route::get('/scrape', 'DataController@scrape');

Here is my Project_createcontroller:

<?php

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;

class project_create extends Controller
{
    public function showpform()
    {
        return view('form_project');
    }

    public function showtable(Request $request)
    {
        $name = $request->input('name');
        $description = $request->input('description');

        \DB::table('projects')->insert(
            [
                'project_id' => 1,
                'name' => $rawUrl,
                'description' => $hashedUrl,
                'created_at' =>  \Carbon\Carbon::now(),
            ]);

        $project_data = \DB::table('projects')->select('*')->get();
        return View::make('form_project', compact('project_data'));
    }
}

And here is my view:

<html lang="en">
    <head>
        <title>File Upload</title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    </head>
<body>

    <div class="container">
        <h3 class="jumbotron">Create here your project</h3>

        <form class="w3-container w3-light-grey" action= enctype="multipart/form-data">
        
            <p>
            <label>Project Name</label>
            <input class="w3-input w3-border w3-round" name="name" type="text"></p>
            <p>
            <label>Project Description</label>
            <input class="w3-input w3-border w3-round" name="description" type="text"></p>

        <button type="submit" class="btn btn-primary" style="margin-top:10px">Create Project</button>
        </form>

    </div>

    <table>
        <thead>
            <tr>
                <th> id</th>
                <th> name</th>
                <th> description</th>
                <th> created_at </th>
            </tr>
        </thead>
        <tbody>
             @foreach($project_data as $data)
              <tr>
                  <td>  </td>
                  <td>  </td>
                  <td>  </td>
                  <td>  </td>
              </tr>
             @endforeach
       </tbody>
   </table>


</body>
</html>

Thanks for the help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire