John Davidson

javascript - Laravel 8 Display Data in View and Sent Data to Database

0 comments
Message:


First of all, I'm a beginner in programming world. Just learn to print hello world this early January.


For this project, I’m using Laravel 8.


I have 4 table in database. The first one is forms, second is cats, catdetails is the third one and lastly is cat_catdetails. The attributes for each table is below:



The cats table is set to have this data (fixed):



cats and catdetails relationship is many-to-many, so I use cat_catdetails as a join table and the relationship is one to many between “cats and cat_catdetails” and “catdetails and cat_catdetails


Basically the goal I want to achieve is something like this


cat_catdetails table:



In views, I create something like this



When the checkbox is checked, user can enter the detail (This code is done).


This is what I can make so far:


<div class="row">
<div class="col">
<table class="table table-bordered table-hover">
<tr>
<th >Cat</th>
<th >Age</th>
<th >Weight</th>
<th >Height</th>
</tr>

@foreach($cats as $cat)
<tr>
<th scope="row">
<input type="checkbox" id="name" name="name" value="{{$cat->id}}"/>{{$cat->name}}
</th>
<td><input type="number" id="age" name="age" value="" disabled /></td>
<td><input type="number" id="weight" name="weight" value="" disabled /></td>
<td><input type="number" id="height" name="height" value="" disabled /></td>
</tr>
@endforeach
</table>
</div>
</div>

Question:



  1. How to configure the controller for this project ?

  2. How to display this in view using @foreach and assign each input id ?

  3. Are my database and table flow is accceptable ? Are there any simple way to do ?


Sorry if my question is very stupid. I really hope some expert can guide me for this project.


Thank You.



My Answer:
To configure the controller for this project, you will need to create a controller that will handle the logic for displaying the data in the view and saving the data to the database. Here is an example of how you can set up the controller:

php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Cat;

class CatController extends Controller
{
public function index()
{
$cats = Cat::all();
return view('cats.index', compact('cats'));
}

public function store(Request $request)
{
// Logic to store the data in the database
}
}


In the above controller, the `index` method fetches all the cats from the database and passes them to the view. The `store` method will handle the logic for saving the data from the form to the database.

To display the data in the view using `@foreach` and assign each input id, you can modify your existing code like this:

html
@foreach($cats as $cat)



{{$cat->name}}





@endforeach


In this code, each input field is assigned a unique id based on the cat's id, which will make it easier to identify the data when submitting the form.

Regarding your database and table structure, it seems acceptable for the many-to-many relationship between cats and catdetails. The join table `cat_catdetails` is a common way to handle many-to-many relationships in Laravel.

I hope this helps you get started with your project. Good luck!

Rate this post

4 of 5 based on 3338 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us