【问题标题】:Laravel Route Model Binding return id null on product deletionLaravel Route Model Binding 在产品删除时返回 id null
【发布时间】:2016-01-18 17:14:06
【问题描述】:

我使用的是 laravel 5.1.28。这里我只是想创建一个可以添加、删除和编辑产品的简单应用程序,但我注意到在使用绑定模型时,$product 实例只返回 null。接下来我会解释,但首先这里是完整的代码(没有刀片):

在我的 route.php 中,我有以下产品路线:

Route::model('products', 'App\Product'); 

Route::group(array('prefix' => 'admin', 'middleware' => 'SentinelAdmin'), function ()
{

# Product Management admin backend
Route::group(array('prefix' => 'products'), function () {
    Route::get('/', array('as' => 'products', 'uses' => 'ProductController@index'));
    Route::get('create', 'ProductController@create');
    Route::post('create', 'ProductController@store');
    Route::get('{productId}/delete', array('as' => 'delete/product', 'uses' => 'ProductController@destroy'));
    Route::get('{productId}/confirm-delete', array('as' => 'confirm-delete/product', 'uses' => 'ProductController@getModalDelete'));
    Route::get('{productId}/restore', array('as' => 'restore/product', 'uses' => 'ProductController@getRestore'));
    Route::get('{productId}', array('as' => 'products.show', 'uses' => 'ProductController@show'));
});

    Route::resource('products', 'ProductController');

});

所以,这是我的简单数据库迁移:

class CreateProductsTable extends Migration
{

public function up()
{
    Schema::create('products', function (Blueprint $table) {
        $table->increments('id');
        $table->string('user_id');
        $table->string('name');
        $table->string('description')->nullable();
        $table->string('category')->nullable();
        $table->decimal('price')->nullable(); 
        $table->integer('quantity')->nullable(); 
        $table->tinyInteger('in_stock')->nullable(); //0 - no, 1 - yes
        $table->string('photos')->default('default_product_photo.jpg')->nullable(); //add one first
        $table->timestamps();
    });
}
public function down()
{
    Schema::table('products', function (Blueprint $table) {

    });
}
}

在我的 Product.php 模型中,我有这个:

**class Product extends Model
 {
   protected $table = 'products';
   protected $guarded = ['id'];
   protected $fillable = [
    'user_id',
    'name',
    'description',
    'category',
    'price',
    'quantity',
    'in_stock',
    'photos',
];
}**

这是我在 ProductController.php 中的两个函数

对于下面的第一个FUNCTION getModalDelete,它会找到产品id,并显示一个确认框来删除产品。如果单击确认按钮,将调用路由删除/产品/{id},然后调用销毁方法(第二个函数)。第二个功能很重要:

class ProductController extends Controller
{
  //1st FUNCTION
  public function getModalDelete(Request $request, $id) 
  {
    $product_id = Product::where('id', '=', $id)->firstOrFail();
    $model = 'products'; ////testing s
    $confirm_route = $error = null;
    try {
        $confirm_route = route('delete/product', ['id' => $product_id]);
        return View('admin/layouts/modal_confirmation', compact('error', 'model', 'confirm_route'));
    } catch (ProductNotFoundException $e) {

        $error = trans('product/message.error.delete', compact('id'));
        return View('admin/layouts/modal_confirmation', compact('error', 'model', 'confirm_route'));
    }
  }

  //2nd FUNCTION
  public function destroy(Product $product)
  {
    Product::destroy($product);
   // $product->forceDelete(); //doesn't work either

    //dd($product); //Weird that it returns null.

    //$success = Lang::get('products/message.success.delete');

    //return Redirect::route('admin.products.index')->with('success', $success);

  }
}

但是如果我从

更改函数的参数
public function destroy(Product $product)
{
}

public function destroy($id)
{
}

它有效。但我只是不知道为什么参数 (Product $product) 在这里使用路由模型绑定不起作用。 $product 只是返回 null。但 $id 返回一个值。

【问题讨论】:

  • 我认为这与我前几天遇到的相同问题是您的路线 {productId}{product} 的路线模型绑定更改它应该可以工作:)
  • 实际上刚刚意识到上述方法无法正常工作,因为您在 5.2 中使用 5.1 查看文档laravel.com/docs/5.1/routing#route-model-binding 您需要将模型绑定到路由器。
  • 是的,它不起作用。我想在 5.2 上尝试这个,但我有几个不支持 5.2 的软件包虽然.. :(
  • 啊,很糟糕,但是Route::model('product', 'Product'); 理论上应该可以为您工作。随着上面提到的更改参数名称的变化。
  • 我已尝试将 s 拉出,但也不起作用。刚才,我按照文档注册了路由,运行composer dump-autoload。还是不行。该产品只返回一个空值。

标签: laravel


【解决方案1】:

发现是前端模板的问题:

其实我尝试过从

Route::model('products', 'App\Product'); 

Route::model('product', 'App\Product'); 

在尝试发布此问题之前,由于我正在使用的前端模板的问题,它没有工作。所以,@Mark Davidson 的答案改变了

Route::model('products', 'Product');

Route::model('product', 'Product');

和改变参数

{productId} 

{product}

完美运行。

前端删除产品的路径有问题。我可以使用 URL 浏览器删除产品,但单击删除按钮不会弹出,我无法调试该值。不知何故,如果我更改

Route::model('product', 'App\Product'); 

Route::model('products', 'App\Product');

但它会将 $product 实例返回为 null。问题解决了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-21
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 2019-02-02
    • 2022-06-22
    • 1970-01-01
    相关资源
    最近更新 更多