在laravel中向付费用户显示页面正文的一部分

时间:2017-08-07 08:50:47

标签: php laravel

我已经存储在我的应用程序(LARAVEL 5.4)中,我希望在我的产品主体底部共享一些下载链接,但是要将其隐藏起来,除了谁支付了特定产品的费用。

以下是我如何获取用户数据及其对特定产品的请求的说明:

他们的要求表格:

    <form class="form-horizontal" action="{{route('buy_course')}}" method="POST"  id="contact_form">
  {{ csrf_field() }}


  <input  name="user_id" value="{{ Auth::user()->id }}" class="form-control"  type="hidden">
  <input  name="course_id" value="{{ $course->id }}" class="form-control"  type="hidden">

  <div class="form-group">
    <label class="col-md-3 control-label">UserName</label>
    <div class="col-md-9 inputGroupContainer">
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-user"></i></span>
        <input  name="username" value="{{ Auth::user()->username }}" class="form-control"  type="text" readonly>
      </div>
    </div>
  </div>

  <!-- Text input-->
  <div class="form-group">
    <label class="col-md-3 control-label">E-Mail</label>
      <div class="col-md-9 inputGroupContainer">
      <div class="input-group">
          <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
          <input name="email" value="{{ Auth::user()->email }}" class="form-control"  type="text" readonly>
      </div>
    </div>
  </div>

  <div class="form-group">
    <label class="col-md-3 control-label">Course</label>
    <div class="col-md-9 inputGroupContainer">
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-graduation-cap"></i></span>
        <input  name="course" value="{{ $course->course_name }}" class="form-control"  type="text" readonly>
      </div>
    </div>
  </div>

  <div class="form-group">
    <label class="col-md-3 control-label" for="type">Type</label>
    <div class="col-md-9 inputGroupContainer">
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
        <select class="form-control" id="type" name="type">
          <option value="">Select Type</option>
          <option value="Download Link">Download Link</option>
          <option value="Physical Disk">Physical Disk</option>
        </select>
      </div>
    </div>
  </div>

  @if (!empty($course->extra_description))
    <div class="form-group">
      <label class="col-md-3 control-label" for="type">{{ $course->extra_title }}</label>
      <div class="col-md-9 inputGroupContainer">
        <div class="input-group">
          <span class="input-group-addon"><i class="fa fa-file-code-o"></i></span>
          <select class="form-control" id="type" name="extra_price">
            <option value="">Do you need extra?</option>
            <option value="{{$course->extra_price}}">Yes - Add {{$course->extra_price}} Rp</option>
            <option value="0">No</option>
          </select>
        </div>
      </div>
    </div>
  @endif
  <div class="form-group">
    <label class="col-md-3 control-label">Note to seller</label>
    <div class="col-md-9 inputGroupContainer">
      <div class="input-group">
        <span class="input-group-addon"><i class="fa fa-comment-o"></i></span>
        <textarea name="note" id="text" placeholder="Your note to seller here..." class="form-control" rows="8" ></textarea>
      </div>
      <h6 class="pull-right" id="count_message"></h6>
    </div>
  </div>

  <div class="text-center">
    <input type="submit" class="btn btn-block btn-success" value="Send">
  </div>
</form>

控制器:

public function postbuycourse(Request $request) {
      $this->validate($request, array(
        'user_id' => 'required',
        'username' => 'required',
        'email' => 'required|email',
        'course' => 'required',
        'note' => 'sometimes|max:500',
        'type' => 'required',
      ));

      DB::table('purchases')->insert([
        'user_id' => $request->user_id,
        'course_id' => $request->course_id,
        'note' => $request->note,
        'type' => $request->type,
        'status' => 0,
        'invoice_nu' => str_random(15),
        'created_at' => Carbon::now()->format('Y-m-d H:i:s'),
        'updated_at' => Carbon::now()->format('Y-m-d H:i:s'),
      ]);

      $data = array(
        'username' => $request->username,
        'email' => $request->email,
        'course' => $request->course,
        'note' => $request->note,
        'type' => $request->type,
      );

      Mail::send('emails.buycourse', $data, function($message) use ($data) {
        $message->from($data['email']);
        $message->to('xxxxxx@xxxxxx.com');
        $message->subject($data['course']);
      });

      $id = $request->course_id;
      $course = Course::findOrFail($id);
      Mail::to($request->user())->send(new CourseReceived($course));

      Session::flash('flash_message', 'Your Order was sent. Our sell team will contact you shortly.');
      return redirect()->back();

    }

以下是我向他们展示订单和订单状态的方式,如果付款与否:

@if (Auth::user()->purchase->count() )
  <h4><i class="fa fa-graduation-cap"></i> Courses Orders</h4>
  <table class="mt-20 table table-bordered table-hover table-responsive">
    <thead>
      <tr class="bg-primary">
        <th class="text-center">ID</th>
        <th class="text-center">Invoice Number</th>
        <th class="text-center">Course Name</th>
        <th class="text-center">Note</th>
        <th class="text-center">Sum</th>
        <th class="text-center">Status</th>
        <th class="text-center">Time</th>
        <th class="text-center">Expire</th>
        <th class="text-center">Options</th>
      </tr>
    </thead>
    <tbody class="text-center">
      @foreach ($purchases as $purchase)
      <tr>
        <td id="primary">{{$purchase->id}}</td>
        <td>{{$purchase->invoice_nu}}</td>
        <td>{{$purchase->course->course_name}}</td>
        <td>
          @if(!empty($purchase->note))
            {{ $purchase->note }}
          @else
            -
          @endif
        </td>
        <td>
          @if( ! empty($purchase->course->course_disscount))
              <span class="text-primary">Price: <del>{{$purchase->course->course_price}}</del></span><br>
              <span class="text-danger">disscount: {{$purchase->course->course_disscount}}</span>
              <hr>
              <span class="text-success">Total: {{ number_format($purchase->course->course_price - $purchase->course->course_disscount, 0) }} Rp</span>
          @else
             {{ number_format($purchase->course->course_price, 0) }} Rp
          @endif
        </td>
        <td>
          @if($purchase->status == 0)
            <span class="text-danger">Waiting Payment</span>
          @else
            <span class="text-success">Paid</span>
          @endif
        </td>
        <td>{{ $purchase->created_at->format('d, M, Y | h:i A') }}</td>
        <td>
          @if($purchase->status == 0)
            <span class="text-danger">-</span>
          @else
            <span class="text-success">{{ $purchase->created_at->format('d, M, Y | h:i A') }}</span>
          @endif
        </td>
        <td>
          <a href="#" class="btn btn-sm btn-success btn-block">Payment</a>
          <a href="{{ route('userscourses.edit', $purchase->id) }}" class="btn btn-sm btn-info btn-block" role="button"><i class="fa fa-pencil"></i> Edit</a>
          {!! Form::open(['method' => 'DELETE', 'route' => ['userscourses.destroy', $purchase->id] ]) !!}
          {!! Form::submit('Delete', ['class' => 'btn btn-sm btn-danger btn-block mt-20']) !!}
          {!! Form::close() !!}
        </td>
      </tr>
      @endforeach
    </tbody>
  </table>

感谢。

1 个答案:

答案 0 :(得分:0)

您可以使用laravel Gatepolicies功能来实现这一目标,您可以阅读一次文档。