I have a DB with multiple tables. I want to display the DB data on my laravel application. I want to display two tables data on the same page. I have created the model, view, and controller for the app but I am being able to display only one table. I cannot show the other table.
I think I need to define a model with multiple relationships which I am not getting how to do.
My tables are called posts and videos I have nothing on my model. the controller and the view is given below.
Controller
namespace App\Http\Controllers;
use App\Post;
use App\Video;
use Illuminate\Http\Request;
class PostsController extends Controller
{
public function index()
{
$posts = Post::all();
return view('landing')->with('posts', $posts);
}
}
View
#extends('layouts.app')
#extends('layouts.navbar')
#section('title')
Landing Page
#endsection
#section('content')
<main class="py-4">
<div class="container">
<div class="row">
<div class="col-md-8">
<h3>Section 1</h3>
#foreach ($posts as $post)
<ul class="list-group">
<li class="list-group-item">
<a href="/posts/{{ $post->id }}">
{{ $post->title }}
{{ $post->brief }}
{{ $post->body }}
{{ $post->cover_image }}
</a>
</li>
</ul>
#endforeach
</div>
<div class="col-md-4">
<h3>Section 2</h3>
#foreach ($videos as $video)
<ul class="list-group">
<li class="list-group-item">
<a href="/videos/{{ $video->id }}">
{{ $video->title }}
</a>
</li>
</ul>
#endforeach
</div>
</div>
</div>
</main>
#endsection
Route on the web.php
<?php
use App\Http\Controllers\PagesController;
use Illuminate\Support\Facades\Route;
Route::get('/', 'PostsController#index');
Route::get('/', 'VideosController#index');
Route::get('/posts/{post}', 'PostsController#show');
Route::resource('posts', 'PostsController');
Route::resource('videos', 'VideosController');
Auth::routes();
Controller for videos data table
<?php
namespace App\Http\Controllers;
use App\Video;
use Illuminate\Http\Request;
class VideosController extends Controller
{
public function index()
{
$videos = Video::all();
return view('landing')->with('videos', $videos);
}
}
On this code I see this problem
If I remove the route for videos on my web.php I could see posts data.
So how could I display both the posts and the videos data at the same time?
Pass both Model together :
<?php
namespace App\Http\Controllers;
use App\Video;
use App\Post;
use Illuminate\Http\Request;
class VideosController extends Controller
{
public function index()
{
$videos = Video::all();
$posts = Post::all();
return view('landing', compact('videos','posts'));
}
}
public function profile($id)
{
$id = Crypt::decrypt($id);
$measurements = DB::select( DB::raw("SELECT * FROM measurements WHERE custom_id = '$id'") );
$customers = DB::table('customers')->where('customer_id', '=', $id)->get();
return view('measurements.profile', compact('measurements','customers'));
}
Related
PostsController:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use App\Models\Post;
class PostsController extends Controller
{
public function index()
{
$posts = Post::orderBy('created_at','desc');
return view('pages.index')->with('posts', $posts);
}
index.blade.php
<h1>Posts</h1>
#if(count($posts) > 0)
#foreach($posts as $post)
<div class="well">
<div class="row">
<div class="col-md-4 col-sm-4">
<img style="width:100%" src="/storage/cover_images/{{$post->cover_image}}">
</div>
<div class="col-md-8 col-sm-8">
<h3>{{$post->title}}</h3>
<small>Written on {{$post->created_at}} by {{$post->user->name}}</small>
</div>
</div>
</div>
#endforeach
#else
<p>No posts found</p>
#endif
when i go to website it pops up error and i do not know what should i do. Actually there is one question with this title which i have but actually it did not help me.
Laravel provide much better solution for this kind of scenario, use forelse
#forelse ($posts as $post)
...
#empty
<p>No posts found</p>
#endforelse
Please the blade document.
You forget to put get() to your query
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Intervention\Image\Facades\Image;
use App\Models\Post;
class PostsController extends Controller
{
public function index()
{
$posts = Post::orderBy('created_at','desc')->get();
return view('pages.index')->with('posts', $posts);
}
Check the offical docs for using Count()
https://www.php.net/manual/en/function.count.php
Can you do a "dump" of your "$posts" and see what do you have?
Before using "count" function, i recommend to check the type of your "$posts" or doing something like:
$posts = Post::orderBy('created_at','desc') ?? [];
Or make it more simple by adding a return type on your "orderBy" function like
public static function orderBy(...) : Array {}
Another related issue which can help: https://stackoverflow.com/a/69055096/7186622
I created a Model, Controller, Factory and dataTable files, but laravel keeps showing me the error $dataTable is undefined
How come? Is it a routing issue or I'm not linking the controller to the blade correctly?
My controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\DataTables\DataEntry\ReportsDataTable;
class ReportsController extends Controller
{
public function index(ReportsDataTable $dataTable)
{
return $dataTable->render("pages.dataEntry.reports.index");
}
}
The Route:
// Data Entry pages
Route::prefix('dataEntry')->name('dataEntry.')->group(function () {
Route::resource('reports', ReportsController::class)->only(['index']);
});
index.blade:
<x-base-layout>
<!--begin::Card-->
<div class="card">
<!--begin::Card body-->
<div class="card-body pt-6">
<!--begin::Table-->
{{ $dataTable->table() }}
<!--end::Table-->
{{-- Inject Scripts --}}
{{ $dataTable->scripts() }}
</div>
<!--end::Card body-->
</div>
<!--end::Card-->
</x-base-layout>
public function index()
{
return ReportsDataTable::all()->render("pages.dataEntry.reports.index");
}
Controller
namespace App\Http\Controllers;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticlesController extends Controller
{
public function show($id)
{
$article = Article::find($id);
return view('articles.show', ['article' => $article]);
}
}
Routes
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
return view('welcome');
});
Route::get('/about', function () {
return view('about', [
'articles' => App\Models\Article::take(3)->latest()->get()
]);
});
Route::get('/articles/{article}',
'App\Http\Controllers\ArticlesController#show');
show.blade.php
#extends ('layout')
#section ('content')
<div id="wrapper">
<div id="page" class="container">
<div id="content">
<div class="title">
<h3>{{ $article->title }}</h3>
<p>
<img src="/images/banner.jpg" alt=""
class="image image-full"/>
</p>
<p>{{ $article->body }}</p>
</div>
</div>
#endsection
Error
Class 'App\Http\Controllers\Controller' not found
http://localhost:8000/articles/2
I am not sure where I am going wrong. I have looked at the documentation for routes/controllers in Laravel 8, and it looks like what I have should be working.
It seems there is something wrong with your extended controller.
please check Controller.php existed in App\Http\Controllers or not.
All controller same as yours (ArticlesController) extended from Controller
Hello fellow Stackoverflow users,
I am currently working on a hobby-project (a Reddit clone) using Laravel.
What I am trying to achieve is to use a similar routing to posts as Reddit does.
In my case I want to navigate to a page using the current subreddit-name and ID of the post (b/subreddit-name/postId) to determine where the website has to route to. I made it work to request the ID of the post, but I couldn't manage to get it done with the current subreddit-name the user clicks on (Subbie).
By the way: Subbie's the name which I am using instead of the term "subreddit", each post has a column stored in the database with the name of the subbie, as well as the ID of the post, so it should be all retrievable.
How can I make it possible so Laravel recognizes the "{subbie}" variable?
(Maybe I am using the wrong words to describe my problem, but I hope it all makes sense.)
Here is the code that I am using:
Router: web.php:
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', 'IndexController#index');
Route::get('/b/{subbie}/{id}', 'IndexController#post');
Controller: IndexController.php:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Index;
class IndexController extends Controller
{
public function index() {
$posts = Index::all();
return view('index', ['posts' => $posts]);
}
public function post($id) {
$posts = Index::findOrFail($id);
return view('post', ['posts' => $posts]);
}
}
Model: Index.php:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Index extends Model
{
protected $table = 'posts';
protected $primaryKey = 'id';
}
View: index.blade.php:
#extends('layouts.app')
#section('content')
<div class="container mt-5">
<div class="col-12">
#if(count($posts) > 0)
<ul class="list-group">
#foreach ($posts as $post)
<a href="{!! url()->current() !!}/b/{{ $post->subbie }}/{{ $post->id }}">
<li class="list-group-item" style="text-decoration: none;">b/{{ $post->subbie }} ยท Posted by u/Test</li>
<li class="list-group-item" style="text-decoration: none;"><h5 class="h5">Tekst: {{ $post->body }}</h5></li>
</a>
#endforeach
</ul>
#else
<h1>Helaas zijn er nog geen artikels beschikbaar</h1>
#endif
</div>
</div>
#endsection
View 2: post.blade.php:
#extends('layouts.app')
#section('content')
<ul class="list-group">
<li class="list-group-item">ID: {{ $posts->id}}</li>
</ul>
#endsection
I am aiming on retrieving the user image (from profile table column 'profile_img') and display on each post's footer.
I can retrieve the name of the author using $post->author->name and the profile picture using $post->author->profile->profile_image but it only works when i have a single record (post). when there is more than one record i get an error Trying to get property 'profile' of non-object (View: xampp/........../home.blade.php)
Can somebody show me where do i go wrong??
Models:
User
public function post()
{
return $this->hasMany('App\Post');
}
public function profile()
{
return $this->hasOne('App\Profile');
}
Profile
public function user()
{
return $this->belongsTo('App\User');
}
Post
public function author()
{
return $this->belongsTo('App\User', 'id');
}
controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class MainController extends Controller
{
public function index() {
$posts = Post::paginate(9);
return view('pages.home')->with('posts', $posts);
}
home view
#if(count($posts) > 0)
#foreach($posts as$posts)
<div>
<div class="post-title"><h3>{{$post->title}}</h3></div>
<div class="post-description">
{!!mb_substr($post>body,10,rand(35,40)) !!} ....
</div>
<div class="featured-details">
<div class="p-clearfix">
<img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
<div class="author-title lite">{{ $post->author->name }}</div>
<div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
</div>
</div>
</div>
<hr>
#endforeach
#else
no post yet
#endif
There appears to be some issues in your home view. Try it again with this code and see.
Home View
#if(count($posts) > 0)
#foreach($posts as $post)
<div>
<div class="post-title"><h3>{{$post->title}}</h3></div>
<div class="post-description">
{!!mb_substr($post>body,10,rand(35,40)) !!} ....
</div>
<div class="featured-details">
<div class="p-clearfix">
<img class="authorimg"src="/storage/profile_images/{{ $post->author->profile->profile_image }}">
<div class="author-title lite">{{ $post->author->name }}</div>
<div class="lite thumbnail-date">{{ date('M j, Y', strtotime($post->created_at)) }}</div>
</div>
</div>
</div>
<hr>
#endforeach
#else
no post yet
#endif
You can even go further and avoid the n+1 problem for authors by running your eloquent model query like this om your controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class MainController extends Controller
{
public function index() {
$posts = Post::with("author")->paginate(9);
return view('pages.home')->with('posts', $posts);
}
I hope this helps.
In your controller index method try with :
public function index() {
$posts = Post::with("author.profile")->paginate(9);
return view('pages.home')->with('posts', $posts);
}
And update post model relationship:
public function author()
{
return $this->belongsTo('App\User', 'user_id'); // update to users table foreign key
}