gpt4 book ai didi

php - 如何在每个表上显示 4 个不同的数据,以便在 Laravel 中名为 Transaction 的一个表上看到?

转载 作者:行者123 更新时间:2023-11-30 21:59:28 47 4
gpt4 key购买 nike

我是 Laravel 的新手,正在探索更多要学习的东西。我正在做一个视频商店的项目。它有 5 个表:

  • 客户(customer_id、姓名、地址)
  • 类别(category_id、名称、描述、价格)
  • 视频(video_id、标题、描述、category_id、库存)
  • 租金(customer_id, rented_at, total_amount, interest_amount, status)
  • 交易(video_id、价格、transaction_id、数量、状态、returned_at [date])

我想在租借表中显示带有相应类别名称和价格的视频标题。

这是我的 RentalController.php 代码

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use App\Http\Requests;

use Carbon\Carbon;


use App\Transaction;
use App\Customer;
use App\Category;
use App\Rental;
use App\Video;
use DB;


class RentalController extends Controller {

public function __construct(){
$this->middleware('auth');
}

public function index(){
$rentals = Rental::all();
return view('rental.index', compact('rentals'));
}

public function create(){
$rental = new Rental;
$customers = Customer::pluck('name','id');
$videos = Video::pluck('title','id');
$categories = Category::pluck('price', 'id');
return view('rental.create', compact('rental', 'customers', 'videos', 'categories'));

}

public function store(Request $request) {

$this->validate($request,[

'videos.*.id' => 'required'
]);

$data = $request->all();

DB::transaction(function() use ($data) {

$rental = new Rental;

$rental->customer_id = $data['customer_id'];
$rental->rented_at = Carbon::now();

$rental->interest_amount = 0;
$rental->status = 'rented';
dd($data['videos']);
$rental->save();

foreach ($data ['videos'] as $video) {

$detail = new Transaction;
$detail->video_id = $video['id'];
$detail->rental_id = $rental->id;
$detail->quantity = 1;
$detail->status = 'rented';
$detail->price = Video::find($video['id'])->category->price;

$detail->save;

$total_amount = $detail->price;

}

$rental->total_amount = $total_amount;
$rental->save();

});

flash('Request Successfully Saved', 'success');
return redirect()->action('RentalController@index');
}

public function edit(Rental $rental) {

$customers = Customer::pluck('name','id');
$videos = Video::pluck('title','id');
$categories = Category::pluck('price', 'id');

return view('rental.edit',compact('rental', 'customers', 'videos', 'categories'));
}

public function update(Request $request, Rental $rental) {
$data = $request->all();

$rental->customer_id = $data['customer_id'];
$rental->rented_at = $data['rented_at'];
$rental->total_amount = $data['total_amount'];
$rental->interest_amount = $data['interest_amount'];
$rental->status = $data['status'];

$rental->save();

flash('Request Successfully Updated', 'success');
return redirect()->action('RentalController@index');
}

public function destroy(Rental $rental) {
$rental->delete();
flash('Request Successfully Deleted', 'success');
return redirect()->action('RentalController@index');
}

public function show(Rental $rental) {
return view('rental.show', compact('rental'));
}
}

创建.blade.php

 @extends('layouts.app')

@section('content')


<div class="row">
<h1 class = "page-header">Rent a Video</h1>
<div class="col-md-12">
{!! Form::model($rental, ['action' => 'RentalController@store', 'method' => 'POST', 'class' => 'form']) !!}

@include('rental.form')
{!! Form:: close() !!}
</div>
</div> @endsection

表单创建.php

<div class= "form-group">
{!! Form::label('customer_id', 'Customer'); !!}
{!! Form::select('customer_id', $customers , null, ['class' => 'form-control','placeholder'=>'Customer Name']); !!}
</div>

<div class= "form-group">
{!! Form::label('total_amount', 'Total Amount'); !!}
{!! Form::text('total_amount', null, ['class' => 'form-control']); !!}
</div>
<table class="table table-bordered">


<thead>
<tr>
<th>Title</th>
<th>Category</th>
<th>Price</th>
</tr>
</thead>

<tbody>
<tr>
<td>{!! Form::select('videos[1][id]', $videos, null, ['class' => 'form-control', 'placeholder' => 'Choose a video']); !!}</td>
<td></td>
<td><</td>

</tr>


<tr>
<td>{!! Form::select('videos[1][id]', $videos, null, ['class' => 'form-control', 'placeholder' => 'Choose a video']); !!}</td>
<td></td>
<td></td>
</tr>

<tr>
<td>{!! Form::select('videos[2][id]', $videos, null, ['class' => 'form-control', 'placeholder' => 'Choose a video']); !!}</td>
<td></td>
<td></td>
</tr>
</tbody>
</table>

{!! Form::submit('Save', ['class' => 'btn btn-success']); !!}

最佳答案

好的,首先,你的数据库结构应该是这样的:

客户(customer_id、姓名、地址)

public function rentals()
{
return $this->hasMany('App\Rental');
}

类别(category_id、名称、描述、价格)

public function videos()
{
return $this->hasMany('App\Video');
}

视频(video_id、标题、描述、category_id、股票)

public function category()
{
return $this->belongsTo('App\Category');
}

租金(rental_id, customer_id, rented_at, total_amount, interest_amount, status)

public function customer()
{
return $this->belongsTo('App\Customer');
}

public function transactions()
{
return $this->hasMany('App\Transaction');
}

交易(transaction_id、rental_id、price、video_id、数量、状态、returned_at [date])

public function rental()
{
return $this->belongsTo('App\Rental');
}

public function video()
{
return $this->belongsTo('App\Video');
}

Explained: Customer is the peak of the hierarchy , having only many Rental as its children .. Rental as being a child of Customer we declare that it belongs to a Customer .. Also having one Transaction .. Transaction belongs to a rental, as well a video.. then Video belongsTo a Category and so Category hasMany videos ..

现在,关于如何显示数据的是:

按用户查看租金

$customers = Customers::all();
foreach($customers as $customer)
{
echo '<h1>'.$customer->name .'</h1>';
echo '<h3>Rentals</h3>';
foreach($customer->rentals as $rental)
{
echo $rental->total_amount;
// and so on ..
foreach($rental->transactions as $transaction)
{
echo $transaction->video->title;
// and so on ..
echo $transaction->video->category->price;
}
}
}

关于php - 如何在每个表上显示 4 个不同的数据,以便在 Laravel 中名为 Transaction 的一个表上看到?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43904958/

47 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com