大佬教程收集整理的这篇文章主要介绍了根据 laravel-8 中的 product_id 显示所有数据,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的项目表。谁能帮我?我坚持了 2 天
我可以在这里显示产品表的数据
您可以看到每个产品或应用程序都有唯一的 ID 和按钮,就像我点击 ID 6 一样,它会带我到下一页,我可以在项目表中上传 excel 文件。所以事情就像这个 App ID:6 可以有很多项目数据,可以上传表单 excel 文件。并且excel文件的每个数据都有ID这是产品表
这是我可以创建产品的地方 如果我点击保存然后我可以上传excel文件这个 ID 项目表
您可以在我的项目表中看到我可以根据product_ID
存储数据。在我的项目表中,我有三个 product_ID
1,2,3。我想根据 product_ID
显示它们。例如,如果我想显示 product_ID
2 的数据,那么在我的 index.blade.php 中将只显示 product_ID
2 的数据,而不是 product_ID 1 和 3
这是我的 projectController.php 的 index()
方法
public function index()
{
$product = Product::with('projects')->where('user_iD',Auth::ID())->firstOrFail();
$projects = $product->projects()->latest()->paginate(20);
return vIEw('projects.index',compact('projects'))
->with('i',(request()->input('page',1) - 1) * 5);
}
这是我在 ProjectController.php 中的 show()
方法
public function show(Project $project)
{
return vIEw('projects.show',compact('project'));
}
这是用户模型 user.php
public function Products(){
return $this->hasmany('App\Models\Product');
}
public function Project(){
return $this->hasmany('App\Models\Project');
}
这是产品模型product.php
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name','detail','image','color','logo','user_iD'
];
public function User(){
return $this->belongsTo(User::class);
}
public function Project(){
return $this->hasmany(Project::class);
}
}
class Project extends Model
{
use HasFactory;
protected $fillable = [
'chapter_name','sub_section_name','title_1','description_1','image_1','image_2','image_3','title_2','description_2','title_3','description_3','vIDeo_1','vIDeo_2','vIDeo_3','user_iD','product_ID'
];
public function User(){
return $this->belongsTo(User::class);
}
public function Product(){
return $this->belongsTo(Product::class);
}
}
这是 projectimport.php,我可以在其中上传 execl
class Projectsimport implements ToModel,WithheadingRow
{
public function __construct()
{
Project::where('product_ID',Product::where('user_iD',Auth::ID())->pluck('ID')->last())->delete();
}
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Project([
'chapter_name' => $row['chapter_name'],'sub_section_name' => $row['sub_section_name'],'title_1' => $row['title_1'],'description_1' => $row['description_1'],'image_1' => $row['image_1'],'image_2' => $row['image_2'],'image_3' => $row['image_3'],'title_2' => $row['title_2'],'description_2' => $row['description_2'],'title_3' => $row['title_3'],'description_3' => $row['description_3'],'vIDeo_1' => $row['vIDeo_1'],'vIDeo_2' => $row['vIDeo_2'],'vIDeo_3' => $row['vIDeo_3'],'user_iD' => auth()->user()->ID,'product_ID' => Product::where('user_iD',Auth::ID())->pluck('ID')->last()
]);
}
}
@extends('layouts.app')
@section('content')
<div class="row">
<div class="col-lg-12 margin-tb">
<div class="pull-left">
<h2>Laravel 8 CRUD </h2>
</div>
<div class="d-flex flex-row-reverse flex-column">
<div class="d-flex">
<a class="btn btn-success text-light mr-5" data-toggle="medel" ID="mediumbutton" data-target="#mediumModel"
data-attr="{{ route ('projects.create')}}" title="upload project">
<i class="fas fa-cloud-upload-alt fa-2x"></i>
</a>
<form action="{{ route('importProject') }}" method="POST" enctype="multipart/form-data" class="d-flex">
@csrf
<input type='file' name="file">
<button class="btn btn-info" style="margin-left: -60px" title="import Project">
<i class="fas fa-cloud-upload-alt fa-2x"></i></button>
<a class="btn btn-warning" href="{{ route('export') }}">Export User Data</a>
</form>
</div>
</div>
<div class="pull-right">
<a class="btn btn-success text-light" data-toggle="modal" ID="mediumbutton" data-target="#mediumModal"
data-attr="{{ route('projects.create') }}" title="Create a project"> <i class="fas fa-plus-circle"></i>
</a>
</div>
</div>
</div>
@if ($message = Session::get('success'))
<div class="alert alert-success">
<p>{{ $message }}</p>
</div>
@endif
<table class="table table-bordered table-responsive-lg table-hover">
<thead class="thead-dark">
<tr>
<th scope="col">No</th>
<th scope="col">Chapter name</th>
<th scope="col" >Sub-Section name</th>
<th scope="col">title 1</th>
<th scope="col">Description 1</th>
<th scope="col">Image 1</th>
<th scope="col">Image 2</th>
<th scope="col">Image 3</th>
<th scope="col">title 2</th>
<th scope="col">Description 2</th>
<th scope="col">title 3</th>
<th scope="col">Description 3</th>
<th scope="col">VIDeo 1</th>
<th scope="col">VIDeo 2</th>
<th scope="col">VIDeo 3</th>
<th scope="col">Date Created</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
@foreach ($projects as $project)
<tr>
<td scope="row">{{ ++$i }}</td>
<td>{{ $project->chapter_name }}</td>
<td>{{ $project->sub_section_name }}</td>
<td>{{ $project->title_1 }}</td>
<td>{{ $project->description_1 }}</td>
<td>{{ $project->image_1 }}</td>
<td>{{ $project->image_2 }}</td>
<td>{{ $project->image_3 }}</td>
<td>{{ $project->title_2 }}</td>
<td>{{ $project->description_2 }}</td>
<td>{{ $project->title_3 }}</td>
<td>{{ $project->description_3 }}</td>
<td>{{ $project->vIDeo_1 }}</td>
<td>{{ $project->vIDeo_2 }}</td>
<td>{{ $project->vIDeo_3 }}</td>
<td>{{ date_format($project->created_at,'Js M Y') }}</td>
<td>
<form action="{{ route('projects.destroy',$project->ID) }}" method="POST">
<a data-toggle="modal" ID="smallbutton" data-target="#smallModal"
data-attr="{{ route('projects.show',$project->ID) }}" title="show">
<i class="fas fa-eye text-success fa-lg"></i>
</a>
<a class="text-secondary" data-toggle="modal" ID="mediumbutton" data-target="#mediumModal"
data-attr="{{ route('projects.edit',$project->ID) }}">
<i class="fas fa-edit text-gray-300"></i>
</a>
@csrf
@method('deletE')
<button type="submit" title="delete" style="border: none; BACkground-color:transparent;">
<i class="fas fa-trash fa-lg text-danger"></i>
</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
{!! $projects->links() !!}
<!-- small modal -->
<div class="modal fade" ID="smallModal" tabindex="-1" role="dialog" aria-labelledby="smallModalLabel"
aria-hIDden="true">
<div class="modal-dialog modal-sm" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hIDden="true">×</span>
</button>
</div>
<div class="modal-body" ID="smallBody">
<div>
<!-- the result to be displayed apply here -->
</div>
</div>
</div>
</div>
</div>
<!-- medium modal -->
<div class="modal fade" ID="mediumModal" tabindex="-1" role="dialog" aria-labelledby="mediumModalLabel"
aria-hIDden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hIDden="true">×</span>
</button>
</div>
<div class="modal-body" ID="mediumBody">
<div>
<!-- the result to be displayed apply here -->
</div>
</div>
</div>
</div>
</div>
<script>
// display a modal (small modal)
$(document).on('click','#smallbutton',function(event) {
event.preventDefault();
let href = $(this).attr('data-attr');
$.AJAX({
url: href,beforeSend: function() {
$('#loader').show();
},// return the result
success: function(result) {
$('#smallModal').modal("show");
$('#smallBody').HTML(result).show();
},complete: function() {
$('#loader').hIDe();
},error: function(jq
您传递的是整个模型而不是 id
列
public function index()
{
$product = Project::whereIn('product_id',Product::where('user_id',Auth::id())->pluck('id')->toArray())->firstOrFail();
$projects = Project::where('product_id',$product->id)->latest()->paginate(20);
return view('projects.index',compact('projects'))
->with('i',(request()->input('page',1) - 1) * 5);
}
class Project extends Model
{
public function product()
{
return $this->belongsTo('App\Models\Product');
}
}
class Product extends Model
{
public function projects()
{
return $this->hasmany('App\Models\Project');
}
}
并像这样访问它:
public function index()
{
$product = Product::with('projects')->where('user_id',Auth::id())->firstOrFail();
$projects = $product->projects()->latest()->paginate(20);
return view('projects.index',1) - 1) * 5);
}
更新:
public function index()
{
$products = Product::with('projects')->where('user_id',Auth::id())->firstOrFail();
$projects = $products->map(function($product) {
return $product->projects()->latest()->first();
})->flatten();
return view('projects.index',1) - 1) * 5);
}
以上是大佬教程为你收集整理的根据 laravel-8 中的 product_id 显示所有数据全部内容,希望文章能够帮你解决根据 laravel-8 中的 product_id 显示所有数据所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。