Laravel owner middleware not working
<p>I created a middleware (app/Http/Middleware/AbortIfNotOwner.php), this is code from another Stackoverflow post</p>

<pre><code>&lt;?php

namespace App\Http\Middleware;

use Closure;
use DB;

class AbortIfNotOwner
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string    $resourceName
     * @return mixed
     */

    public function handle($request, Closure $next, $resourceName)
    {
        $resourceId = $request-&gt;route()-&gt;parameter($resourceName);

        $user_id = \DB::table($resourceName)-&gt;find($resourceId)-&gt;user_id;

        if ($request-&gt;user()-&gt;id != $user_id) {
            abort(403, 'Unauthorized action.');
        }

        return $next($request);
    }
}
</code></pre>

<p>I register it in the app\Http\Kernel.php</p>

<pre><code>protected $routeMiddleware = [
     'owner' =&gt; 'App\Http\Middleware\AbortIfNotOwner',
];
</code></pre>

<p>and in my route file I have:</p>

<pre><code>Route::group(['middleware' =&gt; ['owner:bids']], function() {
        Route::get('user/{id}/bids', ['as' =&gt; 'buyer_bids', 'uses' =&gt; 'User\Buyer\BidsController@getBidsPerUser']);
    });
</code></pre>

<p>When I run this code I get an </p>

<blockquote>
  <p>ErrorException in AbortIfNotOwner.php line 23: Trying to get property
  of non-object</p>
</blockquote>

<p>This refers to the following lines in the middleware:</p>

<pre><code>&gt; $resourceId = $request-&gt;route()-&gt;parameter($resourceName);
&gt; $user_id = \DB::table($resourceName)-&gt;find($resourceId)-&gt;user_id;
</code></pre>

<p>The issue seems to be that resourceId is null I think. I do have a field user_id in the bids table, so am not sure what is wrong. The route URL is like /user/2/bids.</p>

<h2>EDIT - SOLVED</h2>

<p>I found that the below works:</p>

<pre><code> $user_id = \DB::table($resourceName)-&gt;find($request-&gt;id)-&gt;user_id;
</code></pre>

<p>instead of</p>

<pre><code> $resourceId = $request-&gt;route()-&gt;parameter($resourceName);
 $user_id = \DB::table($resourceName)-&gt;find($resourceId)-&gt;user_id;
</code></pre>

<p>This works with the routes like </p>

<blockquote>
  <p>Route::get('/{id}/bids'</p>
</blockquote>

<h2>EDIT - SOLVED - Other solution</h2>

<pre><code>$resourceId = $request-&gt;route()-&gt;parameter($resourceName);
$user_id = \DB::table($resourceName)-&gt;find($resourceId)-&gt;user_id;
</code></pre>

<p>will work if the route is changed to </p>

<blockquote>
  <p>Route::get('/{bids}/bids'</p>
</blockquote>

<p>... instead of </p>

<blockquote>
  <p>Route::get('/{id}/bids'...</p>
</blockquote>