4

Here is my controller for ProducerType model:

namespace App\Http\Controllers;

use App\ProducerType;
use App\Http\Requests\ValidateProducerTypes;
use Illuminate\Http\Request;

class ProducerTypeController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    ...

    public function destroy(ProducerType $producerType)
    {
        $producerType->delete();
        return redirect('/producers');
    }
}

And here is my model:

namespace App;

use Illuminate\Database\Eloquent\Model;

class ProducerType extends Model
{
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'nome'
    ];
}

View:

<form action="/producers-type/{{ $type->id }}" method="POST">
    @csrf
    @method('DELETE')
    <button type="submit" class="btn-icon">
        <img src="{{ asset('images/times-circle-regular.svg') }}" alt="">
    </button>
</form>

Routes:

Route::resource('producers-type', 'ProducerTypeController', [
    'only' => ['store', 'update', 'destroy']
])->middleware('permission:create users');

My problem is: $producerType variable is not grabbing the necessaries attributtes.

marcelo2605
  • 2,734
  • 4
  • 29
  • 55

2 Answers2

6

Although the answer above gives a solution to the problem, I finnaly found the problem on my code!

From the Laravel documentation for Route Model Binding:

Since the $user variable is type-hinted as the App\User Eloquent model and the variable name matches the {user} URI segment, Laravel will automatically inject the model instance that has an ID matching the corresponding value from the request URI. If a matching model instance is not found in the database, a 404 HTTP response will automatically be generated.

This is my problem: I'm using $producerType variable and Laravel is expecting $producers_type because my route producers-type/{producers_type}.

marcelo2605
  • 2,734
  • 4
  • 29
  • 55
  • Right check my comment 'Specially delete route. I think your route is having `delete/{id}` which should be `delete/producerType` Can you check it from `route:list` command!' – Iftikhar uddin Jan 23 '19 at 03:56
  • I'm using {add_product_log} in my route and using $addProductLog in my method and it works. I was originally just using AddProductLog $log, but it was missing attributes. AddProductLog $addProductLog works now. hope that helps others. – MicB Aug 14 '21 at 22:08
1

Try changing:

public function destroy(ProducerType $producerType)
{
    $producerType->delete();
    return redirect('/producers');
}

to

public function destroy($id)
{
    $producerType = ProducerType::find($id);
    $producerType->delete();
    return redirect('/producers');
}
Iftikhar uddin
  • 3,117
  • 3
  • 29
  • 48