0
votes

have a problem, when I finish editing a product I must redirect to the previous view to continue loading products, but it gives me an error: Illegal offset type - Laravel

Function:

 public function storeProduct(Request $request)
{
    // $request->validate([
    //     'image' => 'mimes:jpeg,jpg,png,gif|max:10000',
    // ]);
    $subcategory = Subcategory::findOrFail($request->get('subcategory_id'));
    // dd($request->all());
    $fields = $request->except('_token', 'subcategory_id');
    // dd($fields);
    
        if ($request->file('image')) { 
            $image = $request->file('image');
            $fileName = $image->getClientOriginalName();
            $image->move(public_path('uploads/img/productos'), $fileName);
            $fields['image'] = "uploads/img/productos/" . $fileName;
        }


    
    $fields['visible'] = 1;

    $product = Product::firstOrCreate($fields);
    $subcategory->products()->attach($product);

    return redirect()->route('admin-products')->with($subcategory);
}

error in line: ** return redirect()->route('admin-products')->with($subcategory);**

Route:

Route::post('product/store', [
    'as' => 'admin-product-store',
    'uses' => 'AdminController@storeProduct'
]);

    Route::get('products/{id?}', [
    'as' => 'admin-products',
    'uses' => 'AdminController@products'
]);
2
Try with('subcategory', $subcategory);sta
error "Too few arguments to function App\Http\Controllers\AdminController::products(), 0 passed and exactly 1 expected" with('subcategory', $subcategory);user13523236
I told about this line change return redirect()->route('admin-products')->with($subcategory);sta
You pass id as array thats why you get this errorA.A Noman
On your route change this line Route::get('products/{id?} to Route::get('products/{id}sta

2 Answers

0
votes

You have to use like this. In laravel documentation follow this

// For a route with the following URI: profile/{id}
//return redirect()->route('profile', ['id' => 1]);

And I use like below. So I have no found error in my code

//Populating Parameters Via Eloquent Models
//return redirect()->route('profile', [$user]);

$id = serialize($subcategory);
return redirect()->route('admin-products', [$id]);
0
votes

The error is that you passing a NULL value (second param in with).

return redirect()->route('admin-products', ['id' => $subcategory->id])->with('subcategory', $subcategory);