I am creating a laravel application. In my database i have 2 table's called: 'folder' and 'subfolder' folder is basically the parent of subfolder. I almost have a working crud, except for create and edit subfolders. To create a subfolder i also have to give a folder_id value. but this needs to be a dropdown with the name's of the folders. i kind of got it working, it only duplicate the value of the folder name multiple times according to the amount of subfolders i have .
but it only has to show 1 time 'product documentatie' instead of three times as you can see on the image
subfolder.create:
<div >
<label for="name">Folder</label>
<select name="folder_id" >
@foreach($subfolders as $subfolder)
<option value=" {{$subfolder->folder->name}}">{{$subfolder->folder->name}}</option>
@endforeach
</select>
</div>
subfolder controller
<?php
namespace App\Http\Controllers\admin;
namespace App\Http\Controllers\admin;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Subfolder;
class SubfolderController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$subfolders = Subfolder::with('folder')->get();
return view('admin.subfolder.index', compact('subfolders'));
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$request->validate([
'name'=>'required',
]);
$subfolder = new Subfolder([
'name'=> $request->get('name'),
]);
$subfolder->save();
return back();
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
$subfolders = Subfolder::all();
$subfolderEdit = Subfolder::find($id);
return view('admin.subfolder.index', compact('subfolderEdit', 'subfolders'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
$request->validate([
'name'=>'required',
'folder_id'=>'required',
]);
$subfolderData = [
'name' => $request->name,
'folder_id' => $request->folder_id,
];
Subfolder::whereId($id)->update($subfolderData);
return redirect()->route('admin.subfolder.index');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
$subfolder = Subfolder::find($id);
$subfolder->delete();
return redirect()->route('admin.subfolder.index');
}
}
if i need to add any imformation i will gladly provide it!
CodePudding user response:
Basically when you do $subfolders = Subfolder::with('folder')->get();
you are getting all folders but you get duplicates. For example if a folder has 3 subfolders, you will retrieve it 3 times.
You could group the result by folder ID.
Something like
$subfolders = Subfolder::with('folder')->groupBy('folderId')->get();
could work (assuming the folder ID is 'folderId'
)
You can check Laravel official documentation to find out more about grouping.