1

i am trying to make a list of locations that you can rent. but to rent the place you need to fill in some information. to fill in this information you excess another page. how do i make it so laravel knows the page belongs to a certain location

this is what ive done now but i keep getting the error:

Call to undefined method App\Reservation::location()

as soon as i have filled in the fields of information

this is the blade file that links to the the create reservation file

@foreach
($locations as $location => $data)
                            <tr>
                                <th>{{$data->id}}</th>
                                <th>{{$data->name}}</th>
                                <th>{{$data->type}}</th>
                                <th><a class="btn" href="{{route('Reservation.index', $data->id)}}">rent</a></th>
                            </tr>

@endforeach

this is the create reservations blade

<form action="{{ route('location.store') }}" method="post">
                        @csrf
                        <label>title</label>
                        <input type="text" class="form-control" name="name"/>
                        <label>type</label>
                        <select>
                            <option value="0">klein</option>
                            <option value="1">groot</option>
                        </select>
                        <button type="submit" class="btn">inschrijven</button>

                    </form>

this is what the location controller looks like

public function store(Request $request)
    {
        $location = new Reservation;
        $location->name = $request->get('name');
        $location->type = $request->get('type');
        $location->location()->associate($request->location());

        $location->save();

        return redirect('/location');
    }

and the relationships in my models should also work

class Reservation extends Model
{
    public function locations()
    {
        return $this->belongsTo('Location::class');
    }
}

class Location extends Model
{
    public function reservations()
    {
        return $this->hasMany('Registration::class');
    }
}

ive been stuck at this all day and i really dont know where to look anymore

1 Answers1

1

The error you are getting is because of the wrong function name, you are calling location, while it is locations.

public function locations(){}

&

$location->location()->associate($request->location());

and you can pass the variable as a query parameter, you'll need to pass this data as an array in your blade file.

Web.php

Route::get('/somewhere/{id?}, function(){
  //do something
})->name('test');

Blade

route('test', ['id' => $id]);

Controller Method

public function store(Request $request, $id) //Adding the query parameter for id passed in Route.
{
    $location = new Reservation;
    $location->name = $request->get('name');
    $location->type = $request->get('type');
    $location->location()->associate($id);

    $location->save();

    return redirect('/location');
}
Aditya Thakur
  • 2,562
  • 2
  • 10
  • 21
  • thanks it's always the stupid typo's that get you. but now i get this error: Method Illuminate\Http\Request::locations does not exist – Collin Erdhuizen May 21 '19 at 21:40
  • @CollinErdhuizen its because of this `$request->location()`, laravel is looking for location method in request class. Let me update my anwser with controller code. – Aditya Thakur May 21 '19 at 21:44