Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

You have to load blade file in controller and call in your PHP code

0

Can you be more specific? Please..

Name of main table is Directory... And name of view in database is view_directories

Controller

    class DirectoriesController extends Controller
    {
        public function index()
        {
            $directories = Directory::all();
            return view('pages.admin.directory', compact('directories'));
        }
    }

Model

class Directory extends Model
{
    //Table name
    protected $table = "directory";
}

directory.blade.php

   <tbody>
            <?php $i=1; ?>
             @foreach($directory as $item)
             <tr>
                <td scope="row">{{ $i }}</td>
                <td>{{ $item->name }}</td>
                <td>{{ $item->surname }}</td>
                <td>{{ $item->email }}</td>
                <td>{{ $item->phone }}</td>
                <td>{{ $item->town }}</td>
                <td>{{ $item->country }}</td>
                <td>{{ $item->gender->gender }}</td>
                <td>{{ $item->status->status }}</td>
                <td><a href="#" ><img src="{{ asset('storage/admin/edit.png') }}" height=23 width=23 
                    class="mx-auto d-block"/></a></td>
                <td><a href="#" class="text-center"><img src="{{ asset('storage/admin/delete.png') }}" 
                     height=23 width=23 class="mx-auto d-block"/></a></td>
                <?php $i++; ?>
             </tr>
              @endforeach
         </tbody>

view from phpmyadmin

DROP VIEW view_directories;
CREATE VIEW view_directories AS
    SELECT 
		t1.id,
        t1.name,
		t1.surname,
		t1.email,
		t1.phone,
		t1.town,
		t1.country,
		t2.gender,
		t3.status
    FROM directory AS t1
	JOIN 
		gender AS t2 ON t2.id = t1.gender_id
	JOIN 
		status AS t3 ON t3.id = t1.status_id
	WHERE 
		t1.active = 1
	ORDER BY
		t1.surname ASC,
		t1.name ASC
Last updated 6 years ago.
0

Yes that absolutely correct Do you get any error ?

0

There is no error... but it doesn't show gender and status... it's empty on screen...

but it exists in my view in database...

<td>{{ $item->gender }}</td>
<td>{{ $item->status }}</td>

I changed this but it still doesn't work!

Last updated 6 years ago.
0

I succeed!! :)

    public function index()
    {
        $directories = DB::select("SELECT*FROM view_directories");
        return view('pages.admin.directory', compact('directories'));
    }
Last updated 6 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.