I am newbie on Laravel and exploring more to learn. I am working on a project which is a Video store. It has 5 tables:
- Customer (customer_id, name, address)
- Category (category_id, name, description, price)
- Videos (video_id, title, description, category_id , stock)
- Rental (customer_id, rented_at, total_amount, interest_amount, status)
- Transaction (video_id, price, transaction_id, quantity, status, returned_at [date])
I want to display in the Rental Table the Title of the Video with the corresponding Category Name and Price.
This is my code for 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'));
}
}
create.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
form.create.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']); !!}