1

Hi i am new to laravel i have 3 forms ie customer ,service,contact all have different tables like customer ,service,contact i need to do insert for these in database in laravel.

Service Form:

 <form method="POST" action="{{ route('service.store') }}">
                          @csrf

                           <div class="input-w">
                              <label class="label-style">Service Type:</label>
                              <span><select class="form-control" required >
                                <option type="brillare">Brillare</option>
                                <option type="hair">Hair</option>
                              </select></span>
                          </div>
                          <div class="input-w">
                              <label name="service_name">Service Name:</label>
                              <span><input id="service_name" name="service_name" class="form-control" placeholder="Service Name" required ></span>
                          </div>

                          <div class="input-w">
                              <label name="service_price">Service Price:</label>
                              <span><input type="number" id="service_price" name="service_price" class="form-control" required placeholder="0000.00"></span>
                          </div>                       

                          <div align="center" class="button_style">
                           <p style="margin:15px -40px 20px 182px"><input type="submit" name="submit" id="submit_service" value="Submit" class="btn btn-outline-success"></p>

                          </div> 


                        </form>

Migration:

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




<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class ServiceTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('service', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('servicetype');
            $table->string('servicename');
            $table->string('serviceprice');            
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('service');
    }
}

Controller:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

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

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, Service $service)
    {
        //

        $service->servicetype = $request->service_type;
        $service->servicename = $request->service_name;
        $service->serviceprice = $request->service_price;

         $service->create();
        return redirect()->route('service.show');

    }

    /**
     * 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)
    {
        //
    }
}

i need service form insert to service table customer form insert to customer table contact form insert to contact table in laravel 5.8

15
  • you can do it in one controller with sql query can you give more information your about your question and show me your all html form customer service and contact Commented Sep 10, 2019 at 11:22
  • simillar 2 more forms customer like customer info name moblie email Commented Sep 10, 2019 at 11:36
  • your all forms submitted same time? what you want exactly? do you want to insert data from 3 forms with one controller Commented Sep 10, 2019 at 11:38
  • if your form different you should create 3 model and 3 controller Commented Sep 10, 2019 at 11:39
  • yep all are different i created 3 model 3 controller 3 views .but in service view form after submit button i get this error ReflectionException Class App\Http\Controllers\Service does not exist Commented Sep 10, 2019 at 11:46

1 Answer 1

1

In this case you have 3 entities.

  1. Customer
  2. Service
  3. Contact

For all 3 entities you would want to have a model, a controller and a migration. If the entities have relations between one and other you can specify those in the model. Take a look at the link for more information about relationships in laravel.

https://laravel.com/docs/master/eloquent-relationships

When you have made the relations in the model, made the migrations its time to move on to the controller.

In the controller you will have the following functions:

public function index(Contact $model)
{
    // return index view
}

public function create()
{
    // return create form view
}

// Option 1
public function store(Request $request, Service $service)
{
    $service->create($request->all);
    return redirect()->route('your.route');
}

// Option 2
public function store(Request $request, Service $service)
{
    $service->servicetype = $request->service_type;
    $service->servicename = $request->service_name;
    $service->serviceprice = $request->service_price;

     $service->create();
    return redirect()->route('your.route');
}


public function edit() 
{
    // return edit view
}

public function update(Request $request,  Service $service)
{
    $service->update($request->all);
    //return view
}

public function destory(Service $service)
{
    $service->delete();
    // return view
}
Sign up to request clarification or add additional context in comments.

12 Comments

hi i know the store function but simillar to this i have 2 other forms i am not able to insert those customer form and contact form
The best practice in this case would be to create 3 models, 3 controllers and 3 migrations. It will keep in clean and will make it easier to find stuff.
i did the same does it work like that insert is not working what about create.blade.php i am fully confused
Ill adjust my awnser
all three are differnt no relationship.. one is service i posted the code another customer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.