Home > Software engineering >  How to get a single row by id in my laravel 8 project
How to get a single row by id in my laravel 8 project

Time:11-16

i'm new to laravel and trying to show a single row using its (id) in my view.blade.php, right now it works, but when i select another license_id it only shows' the contents of the first row (id) since they have same foriegn key, and the funny thing is that the route shows that am in the license_id i selected.

public function viewlicense($id)
{
    if(Beat::where('id', $id)->exists())
    {
        if(License::where('beat_id', $id)->exists())
        {
            $licenses = License::where('beat_id', $id)->first();
            return view('frontend.licenses.view', compact('licenses'));
        }
        else{
            return redirect('/')->with('Status', "The link was broken");
        }
    }
    else{
        return redirect('/')->with('Status', "No such beat found");
    }
}




Route::get('/', [FrontendController::class, 'index']);
Route::get('beat', [FrontendController::class, 'beat']);
Route::get('view-beat/{id}', [FrontendController::class, 'viewbeat']);
Route::get('view-beat/{beat_slug}/{license_slug}', [FrontendController::class, 'viewlicense']);

Edited Code

public function beat()
{
    $beats = Beat::all();
    return view('frontend.beat', compact('beats'));
}

public function viewbeat($slug)
{
    if(Beat::where('slug', $slug)->exists())
    {
        $beat = Beat::where('slug', $slug)->first();
        $licenses = License::where('beat_id', $beat->id)->get();
        return view('frontend.licenses.index', compact('beat', 'licenses'));
    }
    else{
        return redirect('/')->with('Status', "id doesnot exists");
    }
}

public function viewlicense($beat_slug, $license_slug)
{
    if(Beat::where('slug', $beat_slug)->exists())
    {
        if(License::where('slug', $license_slug)->exists())
        {
            $licenses = License::where('slug', $license_slug)->first();
            return view('frontend.licenses.view', compact('licenses'));
        }
        else{
            return redirect('/')->with('Status', "The link was broken");
        }
    }
    else{
        return redirect('/')->with('Status', "No such beat found");
    }
}

CodePudding user response:

Use dependency injection to cut your code.

public function viewlicense(Beat $beat, License $license)
{
    return view('frontend.licenses.view', compact('license'));
}

On routes:

Route::get('view-beat/{beat:slug}/{license:slug}', [FrontendController::class, 'viewlicense']);

The beat:slug and license:slug means that we have slug as identifier to find beat or license.

CodePudding user response:

The is my view.blade.php

<div class="container">
    <div class="shadow card">
        <div class="card-body">
            <div class="row">
                <div class="col-md-4 border-right">
                    <img src="{{ asset('assets/uploads/licenses/img/'.$license->image) }}" style="height:200px;width:200px;" alt="image here">
                </div>
                <div hljs-number">8">
                    <h3 hljs-number">0">
                        {{ $license->slug }} / {{ $license->license_name }}
                        <span style="font-size: i2px;" hljs-keyword">float-end">New</span>
                    </h3>

                    <hr>
                    <label hljs-string">">Price : {{ $license->price }}</label>
                    <p hljs-number">3">
                       Genre :  {!! $license->genre !!}
                    </p>
                    <hr>
                    @if ($license->qty > 0)
                        <label hljs-string">">In stock</label>
                    @else
                    <label hljs-string">">Out of stock</label>
                    @endif
                    <div hljs-number">2 row">
                        <div hljs-number">2">
                            <label for="Quantity">Quantity</label>
                            <div hljs-number">3 text-center input-group">
                                <span hljs-string">">-</span>
                                <input type="text" name="quantity" value="1" hljs-string">" />
                                <span hljs-string">"> </span>
                            </div>
                        </div>
                        <div hljs-number">10">
                            <br/>
                            <button type="button" hljs-number">3 float-start">Add to Wishlist</button>
                            <button type="button" hljs-number">3 float-start">Add to Cart</button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
  • Related