Home > Software engineering >  In my laravel 8, i need to show all the licenses related to a single beat ($id) in the beatreferenci
In my laravel 8, i need to show all the licenses related to a single beat ($id) in the beatreferenci

Time:11-04

//Genre Model// class Genre extends Model { use HasFactory;

protected $table = 'genres';
protected $fillable = [
    'name', 'slug', 'status', 'popular',
];

/**
 * Get all of the licenses for the user.
 */

public function beat()
{
    return $this->hasMany(Beat::class);
}

public function license()
{
    return $this->hasManyThrough(license::class, Beat::class);
}

}

//Beat Model// class Beat extends Model { use HasFactory;

protected $table = 'beats';
protected $fillable = [
    'genre_id',
    'name',
    'slug',
    'image',
    'status',
    'new',
    'trending',
    'meta_title',
    'meta_keywords',
];

public function license()
{
    return $this->hasMany(License::class,'beat_id', 'id');
}

public function genre()
{
    return $this->belongsTo(Genre::class, 'genre_id', 'id');
}

}

//License// { use HasFactory;

protected $table = 'licenses';

protected $primaryKey = 'id';

protected $fillable = [
    'beat_id',
    'license_name',
    'beat_name',
    'genre',
    'bpm',
    'key',
    'tag_1',
    'tag_2',
    'time',
    'price',
    'status',
    'popular',
    'wav',
    'trackout',
    'unlimited',
    'exclusive',
    'image',
    'audio',
];

public function beat()
{
    return $this->belongsTo(Beat::class, 'id');
}

}

//LicenseController//

public function show($id)
{
    
    $license = License::find($id);

    return view('admin.license.show')->with('license', $license,);
} 

// Show.blade.php//

  @extends('layouts.admin')

 @section('content')
 <div class="container">
    @foreach($licenses->chunk(4) as $license)
    <div class="card">
        <div class="card-body">
            @foreach($licenses as $license)
            <div class="">
                <img src="{{ asset('assets/uploads/licenses/img/'.$license->image) }}"  
                alt="image here">
                <p>{{ $license->id }}</p>
                <p>{{ $license->beat_id }}</p>
                <p>{{ $license->beat_name }}</p>
            </div>
            @endforeach
        </div>
    </div>`enter code here`
    @endforeach
 </div>
 @endsection

Anytime i try to use @foreach to call

{{ $license->beat->id }}

, it give me errors like this Error:

Method name must be a string http://127.0.0.1:8000/licenses/1

CodePudding user response:

To get all the licenses related to a single beat, if you have the beat id in the $id variable, you can do the query like this:

License::where('beat_id', $id)->get();

So, your controller function could look like this:

//LicenseController//

public function show($id)
{
    $licenses = License::where('beat_id', $id)->get();

    return view('admin.license.show')->with('licenses', $licenses);
}

Then in the view you can loop over the licences collection to show each one:

// Show.blade.php//

@extends('layouts.admin')

@section('content')
<div class="container">
    @foreach($licenses as $license)
    <div class="card">
        <div class="card-body">
            <div class="">
                <img src="{{ asset('assets/uploads/licenses/img/'.$license->image) }}" alt="image here">
                <p>License Id: {{ $license->id }}</p>
                <p>License Name: {{ $license->license_name }}</p>
                <p>License Beat Id: {{ $license->beat_id }}</p>
            </div>
        </div>
    </div>
    @endforeach
</div>
@endsection

CodePudding user response:

Your organization is a bit strange, as I'd expect to see this method called something more like BeatController::showLicenses(). But setting that aside, you should be using route model binding to automate a lot of this stuff. This is what your controller method should look like:

public function show(Beat $beat)
{
    return view('admin.license.show')->with('licenses', $beat->licenses);
} 

If you define your route with a parameter called beat instead of id, something like this:

Route::get('/admin/license/{beat}', [LicenseController::class, 'show']);

The type hint in the method signature will signal the routing engine to automatically do the database lookup for you. As a bonus, it also handles 404 errors in case in invalid ID is passed.

  • Related