Seeker1337
5 months ago
604
2
Laravel

Laravel 6+ - Two CRUD controllers in one Route::resource

Posted 5 months ago by Seeker1337

Hey there. I have the following problem:

In my website I have a management page where I'm using two ( Create, Read, Update, Delete ) systems with two controllers and two models in order to store "autoparts" and "selling vehicles" in MySQL database, and then retrieve them in two pages in the website. Basically I haven't encountered any problems upon using the first controller and the first model but after I added the second controller with the second model I came to a some sort of a Routing conflict ( I guess ).

My models started messing up and I'm getting an error that they are missing or not recognized. If the first Route is the AutopartsController I get an error that my AutopModel is missing or not recognized. Then if I switch their places and the first Route is the VehiclesController I get an error that my VehiclesModel is missing or not recognized.

Here are my files. I will add only one controller because they are basically the same just working with different data, and I don't want to spam too much this topic. If needed I will add it too.

web.php

Route::resource('management', 'AutopartsController') ;
Route::resource('management', 'VehiclesController') ;

AutopartsController.php

<?php

namespace App\Http\Controllers ;

use App\AutopModel ;
use Illuminate\Http\Request ;

class AutopartsController extends Controller
{
    
    public function display() {
        
        return view('autoparts') ;
        
    }
    
    public function index()
    {
        
        $autopmodel = AutopModel::paginate(20) ;
        
        return view('management', compact('autopmodel')) ;
        
    }
    
    public function create()
    {
        
        return view('crud.create') ;
        
    }

    public function store(Request $request)
    {
        
        $autopmodel = new AutopModel ;
        $autopmodel->ancmod = $request->ancmod ;
        $autopmodel->ancmark = $request->ancmark ;
        $autopmodel->ancyear = $request->ancyear ;
        $autopmodel->ancpos = $request->ancpos ;
        $autopmodel->ancimage = $request->ancimage ;
        
        $file = $autopmodel->ancimage = $request->ancimage ;
        $filename = $file->getClientOriginalName() ;
        $destinationPath = 'storage/images' ; 
        $ancmodel->ancimage = $filename; 
        $uploadSuccess = $file->move($destinationPath, $filename);
        
        $autopmodel->save() ;

        return redirect('management')->with('success', 'The record has been added succesfully!') ;
    }

    public function show($id)
    {
        $autopmodel = AutopModel::findOrFail($id) ;
        
        return view('crud.show', compact('autopmodel')) ;
    }

    public function edit($id)
    {
        
        $autopmodel = AutopModel::findOrFail($id) ;
        
        return view('crud.edit', compact('autopmodel')) ;
        
    }

    public function update(Request $request, $id)
    {
        
        $autopmodel = AutopModel::findOrFail($id) ;
        
        $autopmodel->update([
            
        $autopmodel = new AutopModel ,
        $autopmodel->ancmod = $request->ancmod ,
        $autopmodel->ancmark = $request->ancmark,;
        $autopmodel->ancyear = $request->ancyear ,
        $autopmodel->ancpos = $request->ancpos ,
        $autopmodel->ancimage = $request->ancimage 
            
        ]);
        
        return redirect('management')->with('success', 'The record has been edited succesfully!') ;
        
    }

    public function destroy($id)
    {
        
        $autopmodel = AutopModel::whereId($id)->delete() ;
        
        return redirect('management')->with('warning', 'The record has been deleted succesfully!') ;
        
    }

}

management.blade.php

<div class="main_content">
        <div class="header"">Management system</div>  
    
        <div class="info">
            
            @include('layouts.flash-message') 
            
            @include('crud.index')
            @include('crud.create')

        @include('crudVehicles.index')
            @include('crudVehicles.create')

      </div>
</div>

Please sign in or create an account to participate in this conversation.