Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Maison012's avatar

How to create a view for each table row laravel admin panel

I`m building a CRM when i fetch data from mysql db and show this data on a table. Now i need to create a view for each row. I have tryed to get this view on another page but i have a problem, no value is shown on table. Anyone can help? I am using quick admin panel

This is controller:

public function showdtl(Snd $snd){

abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');

$snd->load('roles');

return view('showdtl', compact('snd'));}

This is migration

    public function up() {
	Schema::create('snds', function (Blueprint $table) {
    $table->increments('id');

    $table->string('name')->nullable();
    $table->string('surname')->nullable();
    $table->string('email')->nullable();
    
    
    $table->timestamps();
    $table->string('notes')->nullable();
    
});}

this is role table

    public function up(){
    Schema::create('role_snd', function (Blueprint $table) {
    $table->unsignedInteger('snd_id');

    $table->foreign('snd_id', 'snd_id_fk_6892')->references('id')->on('snd')->onDelete('cascade');

    $table->unsignedInteger('role_id');

    $table->foreign('role_id', 'role_id_fk_6892')->references('id')->on('roles')->onDelete('cascade');

});}

this is model

  public $table = 'snds';

 protected $fillable = [
'name', 
'surname',
'email', 
 
'notes',];

this is blade

 <div class="mT-30">
 <div class="mb-2">
<table class="table table-bordered table-striped">
    <tbody>
    @foreach($snds as $key => $snd)
    <tr>
        <th>
            ID
        </th>
        <td>
            {{ $snd->id ?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            name
        </th>
        <td>
            {{ $snd->name?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            surname
        </th>
        <td>
            {{ $snd->surname?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            email
        </th>
        <td>
            {{ $snd->email?? '' }}
        </td>
    </tr>
    @endforeach
    </tbody>
</table>
<a style="margin-top:20px;" class="btn btn-info" href="{{ url()->previous() }}">
    {{ trans('global.back_to_list') }}
</a>

When i make this "dd(request)" i see db connection is null

   public function showdtl(Snd $snd) { abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, 
   '403 Forbidden');
 $snd->load('roles');
dd($snd);
return view('showdtl', compact('snd'));}
0 likes
30 replies
automica's avatar

in your blade you are expecting and array called $snds but in your method you are only passing in $snd and it also only looks like you are passing in one of those.

so blade should not include @foreach:

<table class="table table-bordered table-striped">
    <tbody>
    <tr>
        <th>
            ID
        </th>
        <td>
            {{ $snd->id ?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            name
        </th>
        <td>
            {{ $snd->name?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            surname
        </th>
        <td>
            {{ $snd->surname?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            email
        </th>
        <td>
            {{ $snd->email?? '' }}
        </td>
    </tr>
    </tbody>
</table>
Maison012's avatar

Thanks for reply. I have changed this but still my connection is null when i try dd(request) and still no value shown

automica's avatar

If you remove the Snd type hint and move your dd() before your gate, do you see any arguments being passed in?

Maison012's avatar

Are you thinking in this way

public function showdtl(Snd $snd) { 
dd($snd);
abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, 
'403 Forbidden');
$snd->load('roles');

return view('showdtl', compact('snd'));}

If i do this i get same result: connection: null

If i replace dd($hint); get and error , variable hint not found

automica's avatar

I meant do this:

public function showdtl($snd) { 
dd($snd);

if you get a result but don't when you add the type hint, then there may be an issue with your route, so please supply the contents of your routes/web.php

Maison012's avatar

If i do this i get an error like this:: Too few arguments to function App\Http\Controllers\MemberController::showdtl(), 0 passed

this is route

        Route::get('showdtl', 'MemberController@showdtl')->name('showdtl');
automica's avatar

you need to pass an argument otherwise your application doesnt know which resource to load.

    Route::get('showdtl/{snd}', 'MemberController@showdtl')->name('showdtl');

btw if this route is to return a single member, it would be more appropriate to access it as

Route::get('members/{snd}', 'MemberController@showdtl')->name('showdtl');

with your members index as

Route::get('members', 'MemberController@index')->name('members.index');
Maison012's avatar

I understand but if i do this. i get an error 404 | Not Found.

automica's avatar

that would mean that the id you are passing to your method doesnt have a corresponding row in your database.

if you are accessing: showdtl/1 for example, you need to have an id of 1 in your members table.

Maison012's avatar

Yes i can access: showdtl/1 , but stil my data dont show. I have an id on my db table.. When i run dd request i get this answer on view

+table: "Snds"
 #fillable: array:16 [▶]
 #connection: null
  #primaryKey: "id"
  #keyType: "int"
 +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: false
  +wasRecentlyCreated: false
   #attributes: []
   #original: []
   #changes: []
   #casts: []
   #dates: []
   #dateFormat: null
    #appends: []
   #dispatchesEvents: []
   #observables: []
   #relations: []
   #touches: []
   +timestamps: true
    #hidden: []
    #visible: []
    #guarded: array:1 [▶]
Maison012's avatar

if i make this chages:

     public function showdtl(Snd $snds)
   {
    abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
    
    $snds->load('roles');

    $snds = DB::table('Snds')->get();
    
    return view('showdtl',['snds'=>$snds], compact('snds'));
     }

and add @foreach($snds as $key => $snd) on blade ... my data coms back. but i want to show each table row separated on view

automica's avatar

this

public function showdtl(Snd $snds)

needs to match

Route::get('showdtl/{snd}', 'MemberController@showdtl')->name('showdtl');

singular snd not snds

automica's avatar

BTW if you only want to get a single record (eg 1 member) you shouldn't have

    $snds = DB::table('Snds')->get();

using route model binding having showdtl(Snd $snd) in your method signature will return a single record for Snd model which with id of {snd}

Maison012's avatar

okay.. but now i get this error:: Property [id] does not exist on this collection instance.

automica's avatar

ok. so what are you getting now?

   public function showdtl(Snd $snd) 
{ 
abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN,    '403 Forbidden');
 $snd->load('roles');
dd($snd->toArray());
return view('showdtl', compact('snd'));}
Maison012's avatar

i`m getting this::

array:1 [▼
"roles" => []
 ]
automica's avatar

get rid of $snd->load('roles'); line for now. you're not using the roles in your blade, so you dont need to load them.

let me know what dd($snd->toArray()); looks like after that

automica's avatar

if you switch to:

public function showdtl($snd) { 

Log::info($snd);
$snd = Snd::find($snd);
Log::info('result', $snd->toArray());

abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, 
'403 Forbidden');
$snd->load('roles');

return view('showdtl', compact('snd'));}

what do you get in your log file?

Maison012's avatar

this error:::

 Too few arguments to function App\Http\Controllers\MemberController::showdtl(), 0 passed in
automica's avatar

are you sure you've copied my example?

public function showdtl($snd) {

has no Snd in it

check in the logs

Maison012's avatar

i have copied your code. but same error..

couse when i start this project was just to fetch data from db and for me was good to call like this. And now project chaged. So i need to make view modal and some other changers.

automica's avatar

can you answer my question about what is in your logs

look in /storage/logs/laravel.log

and also post the whole showdtl method from your members controller.

Maison012's avatar

Now i have made this change on code

    public function showdtl(Snd $snds)
   {
    abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
    
    $snds->load('roles');

    $snds= DB::table('Snd')->get();
    foreach ($snds as $snd) {
        echo $snd->id;
        
        return view('showdtl', compact('snd'));
    }
     }

and i get right value but returned just row with id 1, i want to see all table rows

Maison012's avatar

No logs is saved on the /storage/logs

And if i run your coe this is error what i see ::

Too few arguments to function App\Http\Controllers\MemberController::showdtl(), 0 passed in C:\Users\ag\Desktop\laravel\Laravel-Adminator- QuickAdminPanel\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 1 expected

Maison012's avatar

i want to output each row seperated . when i click view for id 1 to show only meber with id 1, and samse for the member with id 2

automica's avatar
automica
Best Answer
Level 54

if you want all the records, then you should be doing this with an index method

public function index()
{
    abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
    
    $members = Snd::with('roles')->all();

    return view('index', compact('members'))
}

Your route will be:

Route::get('members', 'MemberController@index')->name('members.index');

and your blade will be:

 <div class="mT-30">
 <div class="mb-2">
<table class="table table-bordered table-striped">
    <tbody>
    @foreach($members as $member)
    <tr>
        <th>
            ID
        </th>
        <td>
            {{ $member->id ?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            name
        </th>
        <td>
            {{ $member->name?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            surname
        </th>
        <td>
            {{ $member->surname?? '' }}
        </td>
    </tr>
    <tr>
        <th>
            email
        </th>
        <td>
            {{ $member->email?? '' }}
        </td>
    </tr>
    @endforeach
    </tbody>
</table>
<a style="margin-top:20px;" class="btn btn-info" href="{{ url()->previous() }}">
    {{ trans('global.back_to_list') }}
</a>

then you would have a show method to display each member

public function show(Snd $snd)
{
    abort_if(Gate::denies('user_show'), Response::HTTP_FORBIDDEN, '403 Forbidden');
    
    return view('show', compact('snd'))
}

with route:

Route::get('members/{snd}', 'MemberController@show')->name('members.show');

and blade: (called show)

<div class="mT-30">
    <div class="mb-2">
        <table class="table table-bordered table-striped">
      
            <tr>
                <th>
                    ID
                </th>
                <td>
                    {{ $snd->id ?? '' }}
                </td>
            </tr>
            <tr>
                <th>
                    name
                </th>
                <td>
                    {{ $snd->name?? '' }}
                </td>
            </tr>
            <tr>
                <th>
                    surname
                </th>
                <td>
                    {{ $snd->surname?? '' }}
                </td>
            </tr>
            <tr>
                <th>
                    email
                </th>
                <td>
                    {{ $snd->email?? '' }}
                </td>
            </tr>
            
        </table>
        <a style="margin-top:20px;" class="btn btn-info" href="{{ url()->previous() }}">
            {{ trans('global.back_to_list') }}
        </a>

BTW all of this is pretty basic laravel. I would strongly suggest you watch https://laracasts.com/series/laravel-8-from-scratch to learn the fundamentals.

automica's avatar

great. mark as best answer and that will close the thread.

1 like

Please or to participate in this conversation.