Skip to content
Advertisement

Create view in one controller and store data in other controller laravel

I’m new to Laravel and I’m trying to store a form. I created the view with the House controller but now I want to store the data in the view with the Booking controller. But when I click the button nothing happens.

My question is if it is possible to make a view with one controller and store it with another controller or maybe there is an other solution.

I also want to use the id of the house to store. How do I get that in the other controller as well?

Web Route

<?php

use IlluminateSupportFacadesRoute;

/*
|--------------------------------------------------------------------------
| 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('/', [AppHttpControllersHouseController::class, 'index']);
Route::get('house/{house}', [AppHttpControllersHouseController::class, 'show']);

Route::post('house/{house}', [AppHttpControllersBookingController::class, 'store']);

Route::get('rental', [AppHttpControllersHouseController::class, 'getUserHouses']);

Route::get('rental/new', [AppHttpControllersHouseController::class, 'create']);
Route::post('rental/new', [AppHttpControllersHouseController::class, 'store']);


Route::get('rental/edit/{house}', [AppHttpControllersHouseController::class, 'edit']);
Route::put('rental/edit/{house}', [AppHttpControllersHouseController::class, 'update']);

Auth::routes();

Booking controller

<?php

namespace AppHttpControllers;

use AppModelsBooking;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;

class BookingController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        // 
    }

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

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Request $request)
    {
        $newBooking = Booking::create([
            'user_id' => Auth::id(),
            'house_id' => $request->id,
            'begin' => $request->begin,
            'end' => $request->end,
            'status' => 0
        ]);
        return redirect('/');
    }

    /**
     * Display the specified resource.
     *
     * @param  AppModelsBooking  $booking
     * @return IlluminateHttpResponse
     */
    public function show(Booking $booking)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  AppModelsBooking  $booking
     * @return IlluminateHttpResponse
     */
    public function edit(Booking $booking)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  AppModelsBooking  $booking
     * @return IlluminateHttpResponse
     */
    public function update(Request $request, Booking $booking)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  AppModelsBooking  $booking
     * @return IlluminateHttpResponse
     */
    public function destroy(Booking $booking)
    {
        //
    }
}

House controller

<?php

namespace AppHttpControllers;

use AppModelshouse;
use IlluminateHttpRequest;
use IlluminateSupportFacadesAuth;
use AppHelperImageable;
use DB;

class HouseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return IlluminateHttpResponse
     */
    public function index()
    {
        $houses = House::all();
        return view('/home', [
            'houses' => $houses
        ]);
    }

    /**
     * Display a listing of the houses the owner has
     *
     * @return IlluminateHttpResponse
     */
    public function getUserHouses()
    {

        $houses = DB::table('houses')
            ->where('user_id', '=', Auth::id())
            ->get();

        return view('/rental/rental', [
            'houses' => $houses
        ]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return IlluminateHttpResponse
     */
    public function create()
    {
        return view('rental/new');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @return IlluminateHttpResponse
     */
    public function store(Request $request)
    {

        $path = Imageable::storeMedia($request);
        $request->online === 'on' ? $online = 1 : $online = 0;

        $newHouse = House::create([
            'title' => $request->title,
            'price_per_night' => $request->price,
            'summary' => $request->summary,
            'place' => $request->place,
            'country' => $request->country,
            'user_id' => Auth::id(),
            'online' => $online,
            'image' => $path,
        ]);

        return redirect('rental');
    }

    /**
     * Display the specified resource.
     *
     * @param  AppModelshouse  $house
     * @return IlluminateHttpResponse
     */
    public function show(house $house)
    {
        return view(
            '/house',
            [
                'house' => $house
            ]
        );
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  AppModelshouse  $house
     * @return IlluminateHttpResponse
     */
    public function edit(house $house)
    {
        return view(
            'rental/edit',
            [
                'house' => $house
            ]
        );
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  IlluminateHttpRequest  $request
     * @param  AppModelshouse  $house
     * @return IlluminateHttpResponse
     */
    public function update(Request $request, house $house)
    {

        $path = Imageable::storeMedia($request);
        $request->online === 'on' ? $online = 1 : $online = 0;

        $house->update([
            'title' => $request->title,
            'price_per_night' => $request->price,
            'summary' => $request->summary,
            'place' => $request->place,
            'country' => $request->country,
            'online' => $online,
            'image' => $path,
        ]);
        return redirect('rental/edit/' . $house->id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  AppModelshouse  $house
     * @return IlluminateHttpResponse
     */
    public function destroy(house $house)
    {
        //
    }
}

View

@extends('layouts.app')

@section('content')
    <div class="container">


        <div class="row">
            <div class="col-12">
                <h1 class="display-one ">{{ $house->title }}</h1>
                <p class=".text-light">{{ $house->place }}, {{ $house->country }}</p>
            </div>
        </div>
        <div class="row mt-5">
            <div class="col-sm-6">
                <img src="{{ asset("img/houses/$house->image") }}" alt="{{ $house->title }}" class="img-fluid" />
            </div>
            <div class="col-sm-6">
                <div class="form-group">
                    <label for="exampleFormControlSelect1">Kies een datum en reserveer direct</label>

                    <form method="POST" action="">
                        @csrf

                        <input type="date" name="begin">
                        <input type="date" name="end">

                        <div class="col-md-12 bg-light mt-3">
                            <button type="button" class="btn btn-warning ml-2">Vraag aan</button>
                        </div>

                    </form>

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

        <div class="row mt-3">
            <div class="col-sm-6">
                <p class="display-one ">{{ $house->summary }}</p>
            </div>
            <div class="col-sm-6">
                <h2 class="display-one ">Aangeboden door</h2>
                <p>Prijs per nacht &euro;{{ $house->price_per_night }}</p>
            </div>
        </div>

    </div>
@endsection

Advertisement

Answer

First of all, nothing happens when you click the form submit button because it is currently type="button" and in order this button to play role of submission button it must be type="submit". You can do whatever you want with Laravel. If you want your form to hit another controller method you can simply specify that in your form tag. Like so:

Imagine this is a form inside a view that is rendered by HouseController

<form method="POST" action="{{ url('/save/from/booking/controller') }}">
      // ....
</form>

And now on form submission inside a view that is rendered by HouseController, you will actually hit a route that is BookingController responsive for. And here is your route that is being hit by the form

Route::post('/save/from/booking/controller', [BookingController::class, 'store']);
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement