Home > Blockchain >  how to use condition in 2 tables using with relation in laravel 8
how to use condition in 2 tables using with relation in laravel 8

Time:06-03

I have two tables name is courses and admissions. Now i want to set if user purchase the course then show admitted else show course price. please check the below code:

 $courses = Course::orderby('id', 'desc')->get();
 $admission = Admission::where('users_id', Auth::user()->id)->first() ?: app(Admission::class);            
                
 return view('Backend.Student.courses', compact('courses', 'admission'));

here is my condition in blade file:

@if( $course->id == $admission->courses_id )
    <li >
        <a href="javascript:void(0)" ><h5 > {{ __('Admitted') }}</h5></a>
    </li>
    @if( !empty($admission->status == 'active') )
    <li >
        <a href="{{ route('access.course', $course->slug) }}" ><h5 > {{ __('Continue Course') }}</h5></a>
    </li>
    @endif
    @else
    <li >
        <a href="{{ route('purchase.course', $course->slug) }}" ><h5 > {{ __('Admission Now') }}</h5></a>
    </li>
    <li >{{ __('Price') }}: ৳{{ number_format( $course->price , 0 , '.' , ',' ) }} BDT</li>
@endif

This course is working but its showing only one course. When same user purchage multiple course it will show only one course is admitted. But i want if a user purchases multiple courses it should be shown admitted each course. Sorry for my bad english and thanks for your kindness.

CodePudding user response:

You are fetching just the first record for admissions, hence there will be only one record.

To get all Admission records for the currently logged in user

$courses = Course::latest('id')->get();
$admissions = Admission::where('user_id', Auth::id())->get();

return view('Backend.Student.courses', compact('courses', 'admissions'));

In blade view, you loop over the user's admissions

@foreach($courses as $course)
    {{-- If the user has purchased course --}}
    @if(in_array($course->id, $admissions->pluck('course_id')->toArray())
        @foreach($admissions as $admission)
            {{-- If the course is active --}}
            @if($admission->course_id === $course->id && $admission->status === 'active')
                <li >
                    <a href="{{ route('access.course', $course->slug) }}" ><h5 > {{ __('Continue Course') }}</h5></a>
                </li>
            @elseif($admission->course_id === $course->id && $admission->status !== 'active')
               {{-- If the course is not active --}}
                <li >
                    <a href="javascript:void(0)" ><h5 > {{ __('Admitted') }}</h5></a>
                </li>
            @endif
        @endforeach
    @else        
        {{-- User has not purchased the course --}}
        <li >
            <a href="{{ route('purchase.course', $course->slug) }}" ><h5 > {{ __('Admission Now') }}</h5></a>
        </li>
        <li >{{ __('Price') }}: ৳{{ number_format( $course->price , 0 , '.' , ',' ) }} BDT</li>
    @endif
@endforeach
                
  • Related