-1

q) i want to insert a form in la-ravel ,and my version is P hp 7 and la ravel 5.8. I write all codes and run but it not showing the form or anything ,it shows blank page, and migration db and table also created ,but i cant insert ,because form is not displaying in the URL .. This is the URL HTTP://localhost:8000/student/create can u please help me?

1) I tried HTTP://localhost:8000/student/create but it is not working

this is the view file ,resources/views/student/create.blade.php

@extends('master')
@section('content')
<div class="row">
    <div class="col-md-12">
        <br />
        <h3 align="center">add data</h3>
        <br />

        @if(count($errors) > 0)
        <div class="alert alert-danger">
            <ul>

                @foreach($errors->all() as $error)
                    <li>{{$error}}</li>
                    @endforeach
            </ul>
        </div>
        @endif
        @if(\Session::has('success'))
        <div class="alert alert-success">
            <p>{{ \Session::get('success')}}</p>
        </div>
        @endif

        <form method="post" action="{{url('student')}}">
            {{csrf_field()}}
            <div class="form-group">
                <input type="text" name="first_name" class="form-control"
                 placeholder="enter the first name" />
            </div>

            <div class="form-group">
                <input type="text" name="last_name" class="form-control"
                 placeholder="enter the last name" />
            </div>

             <div class="form-group">
                <input type="submit" class="btn btn-primary" />
            </div>

        </form>
    </div>
</div>

@endsection

2) this is controller

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\student;
use Session;

class StudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('student.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'first_name' => 'required',
            'last_name' =>  'required'
            ]);
        $student=new student([
            'first_name' => $request->get('first_name'),
            'last_name'  => $request->get('last_name')
            ]);
        $student->save();
        return redirect()->route('student.create')->with('success','data added');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

3) this is the routes/web.php

<?php

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/first', function () {
    return view('first');
});


Route::get('/user', function () {
    return view('user');
});


Route::get('/{name}', function ($name) {
    return $name;
});


Route::get('user/{name?}', function ($name="default brace") {
    return $name;
});
//Route::redirect("/Home","/user");


Route::get('/site{site}','youtube@index');

//Route::get("/site","youtube@index");


Route::resource('student/create','StudentController');





output its shows nothing ,only blank page only..
zlatan
  • 3,346
  • 2
  • 17
  • 35
AMuus
  • 1
  • 2
  • Please add this
    . And also please edit your routes/web.php Route::resource('student','StudentController');
    – mukesh kumar Aug 22 '19 at 09:44
  • Please avoid asking multiple questions that have the same problem in them. Clarify why this question is different from the other one. – new Q Open Wid Aug 25 '19 at 16:53

4 Answers4

1

Change this line:

Route::resource('student/create','StudentController');

to this:

Route::resource('students','StudentController');

and visit this address:

localhost:8000/students/create

See the Laravel documentation here.

There is also a typo in your master.blade.php, it must be @yield('content') (notice the place of i and e).

And in your view change the form tag like this:

<form method="post" action="{{route('students.store')}}">
Rouhollah Mazarei
  • 3,969
  • 1
  • 14
  • 20
0

The problem is in your Resource route. It has index, create, store, show, edit, update and delete route. When you are using 127.0.0.1:8000/student/create it goes to your controller's index method and as you have nothing in your index method you are getting a blank page. So change your route like

Route::resource('student','StudentController');

Now you will able to visit the page

127.0.0.1:8000/student/create

And change your form action to

<form method="post" action="{{route('student.store')}}">
zahid hasan emon
  • 6,023
  • 3
  • 16
  • 28
0

Please add this

And also please edit your routes/web.php
Route::resource('student','StudentController');

For more detail please visit this link Laravel - Route::resource vs Route::controller

mukesh kumar
  • 580
  • 3
  • 14
0

You defined your student route resource wrong. You currently have it like this:

Route::resource('student/create','StudentController');

So when you check you php artisan route:list, you get the following output:

| GET|HEAD  | student/create/create        | create.create    | App\Http\Controllers\StudentController@create    

As you can see, you have create two times in your route name, and you are trying to find student/create

Change your route resource to this:

Route::resource('students','StudentController');

Now, when you go to php artisan route:list your route will look like this:

| GET|HEAD  | student/create         | student.create   | App\Http\Controllers\StudentController@create  

Now you will be able to call that route by going to student/create which will show your form.

zlatan
  • 3,346
  • 2
  • 17
  • 35