1
votes

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']); !!}
2
hello there kimberly .. do you have model relationship ? and out of the topic are you pinay ?Demonyowh
Yes I already have, and yes I am a pinay.Kimberly Carreon

2 Answers

1
votes

Ok first and foremost, the structure of your database should be like:

Customer (customer_id, name, address)

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

Category (category_id, name, description, price)

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

Videos (video_id, title, description, category_id , stock)

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

Rental (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 (transaction_id, rental_id, price, video_id, quantity, status, 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 ..

now, on how to show data from this is:

view rentals by user

$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;
        }
    }
}
-1
votes

Its better you go for mysql view. Create a mysql view from a join query and use that view as Model in Laravel. Please note. Views can only be used to read data i.e. for select queries only