fluentd's avatar

Passing ID of one model to another model

I would like to pass an ID of a model for example mysite/membership/3 to another model such as mysite/enrollment/create so that the enrollment model knows which membership to enroll a class into. Normally I would grab the ID from the URI but as you can see my target URI does not include an ID. My first thought would be to pass the ID with the enroll button that is being used but I am not sure if that would be the best way to do this. Any help/suggestion on this would be great.

0 likes
6 replies
bobbybouwmann's avatar

Well you add it to the url right like this

mysite/enrollment/create?membership_id=3

Now you can use the request to get the value

$membershipId = Request::get('membership_id');
fluentd's avatar

Ok so I made the link the following:

{{ url('/enrollments/create?membership_id=' . $membership->id) }}

Then I added this to my create method:

$membership = Request::get('membership_id');

I am getting

ErrorException in EnrollmentsController.php line 32:
Non-static method Symfony\Component\HttpFoundation\Request::get() should not be called statically, assuming     $this from incompatible context
bestmomo's avatar

Reference the facade like this in your controller :

use Request

Not the symfony Request. Or use it :

use Illuminate\Http\Request;

And inject it in constructor or method.

christiangerdes's avatar

You can just import the Request class in the top like so:

use Illuminate\Http\Request;

Then you can inject the Request class into your method like this:

public function create(Request $request)
{
    $membership_id = $request->get('membership_id');
}

In case you don't wan't the use the facade :D

fluentd's avatar

Ok so here is my controller

At the top of the controller I had this:

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

The controller method:

public function create()
{
    $pageTitle = 'Membership Class Enrollment';

    $membership = Request::get('membership_id');

    dd($membership);

    $classes = CurrentClass::all();

    return view('families.memberships.createEnrollment', compact('pageTitle', 'classes', 'membership'));
}

And the button URL

$membershipId = Request::get('membership_id');

As you can see I am using DD to test and make sure I am getting the correct value.

Im still getting the same error:

ErrorException in EnrollmentsController.php line 32:
Non-static method Symfony\Component\HttpFoundation\Request::get() should not be called statically, assuming $this from incompatible context
fluentd's avatar

So I changed my controller to the following and it seemed to work:

public function create(Request $request)
{
 $membership_id = $request->get('membership_id');
}

Please or to participate in this conversation.