I have error Laravel Undefined variable: articles . Why is happen ? Please help me
blog/home.blade.php
<div>
#forelse ($articles as $article)
<div>
<img class="news-image" src="{{URL::to('/images').'/'.$article->image_path}}" alt="">
<h1>{{$article->title}}</h1>
<p>{!!$article->description!!}</p>
</div>
#empty
<h2 class="text-center">Not found</h2>
#endforelse
</div>
Route
Route::get('/blog/home', 'BlogController#articlesAll')->name('articles');
BlogController
class BlogController extends Controller
{
public function articlesAll()
{
return view('blog.home', ['articles' => Article::all()]);
}
}
Try
class BlogController extends Controller {
public function articleAll()
{
$articles = Article::get();
return view('blog.home', compact('articles'));
// OR
return view('blog.home')->with('articles',$articles);
}
}
You can use that for multiple variables to view
return view('blog.home')->with(['articles' => $articles]);
Controller
class BlogController extends Controller {
public function articlesAll() {
$articles = Article::get()->toArray();
return view('blog.home',compact('articles'));
}
}
blog/home.blade.php
#if(!empty($articles))
#foreach($articles as $key => $article)
<div>
<img class="news-image" src="{{URL::to('/images').'/'.$article->image_path}}" alt="">
<h1>{{$article->title}}</h1>
<p>{!!$article->description!!}</p>
</div>
#endforeach
#else
<h2 class="text-center">Not found</h2>
#endif
I could. I deduced. The error was that I forgot to remove route
Route::get('/', function () {
return view('blog.home');
});
i remote this and put other route and it work for me
Route::get('/', 'BlogController#articlesAll', function () {
return view('blog.home');
});
Related
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");
}
I got an error says undefined variable data referring to my foreach in my blade
so my errors is :
Undefined variable: data (View: C:\Users\adila\Desktop\onlineshop\onlineshop\resources\views\user\product.blade.php)
so this is my blade file :
<div class="latest-products">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="section-heading">
<h2>Latest Products</h2>
view all products <i class="fa fa-angle-right"></i>
</div>
</div>`enter code here`
#foreach($data as $product)
<div class="col-md-4">
<div class="product-item">
<img src="assets/images/product_06.jpg" alt="">
<div class="down-content">
<h4>{{$product->title}}</h4>
<h6>$ {{$product->harga}}</h6>
<p>{{$product->deskripsi}}</p>
</div>
</div>
</div>
#endforeach
</div>
</div>
</div>
and this is my controller :
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Models\User;
use App\Models\Product;
class HomeController extends Controller
{
public function redirect(){
$usertype = Auth::user()->usertype;
if($usertype=='1'){
return view('admin.home');
}
else{
return view('user.home');
}
}
public function index(){
if(Auth::id()){
return redirect('redirect');
}
else{
$data = Product::all();
return view('user.home',compact('data', $data));
}
}
}
and my Route :
Route::get('/', [HomeController::class, 'index']);
Please help me
Please remove $data from compact() function
return view('user.home',compact('data'));
You passed the variable $data to the view('user.home', compact('data')) but you have an error in the product.blade I think the problem is on the product. blade file. check that file
You error is in the product.blade.php view and in your controller you're returning the home.blade.php view.
You can avoid the Undefined variable error in your product.blade.php adding an isset in your view like this :
#if(isset($data))
#foreach($data as $product)
(...)
#endforeach
#endif
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
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
}
I'm trying to create a Tag/Content structure. A content object is assigned to a Tag object and Tag objects can be assigned to many Contents. I'm getting an error:
Trying to get property 'name' of non-object (View: D:\laragon\www\project1\resources\views\contents\show.blade.php)
These are my Models:
Content:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Content extends Model {
public function tag() {
return $this->belongsTo('App\Tag');
}
}
Tag:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
public function contents() {
return $this->hasMany('App\Content');
}
}
ContentController:
/**
* Display the specified resource.
*
* #param int $id
* #return \Illuminate\Http\Response
*/
public function show($id) {
$content = Content::find($id);
return View('contents.show')
->with('content', $content);
}
show.blade.php:
#extends('layouts.app')
#section('content')
<div class="container">
<h1> {{ $content->tag->name }} - {{ $content->title }} </h1>
<div class="row">
<div class="col-lg-12">
Title: {{ $content->title }}
</div>
<div class="col-lg-12">
Body: {{ $content->body }}
</div>
<div class="col-lg-12">
Tag: {{ $content->tag }}
</div>
<hr />
<div class="col-lg-12">
{!! link_to('contents', 'Back', ['class' => 'btn btn-danger']) !!}
</div>
</div>
</div>
#endsection
The error I'm getting is from h1 tag: {{ $content->tag->name }}
Any Ideas? Thanks in advance :)
You have to check "$content->tag" is valid before call "$content->tag->name".
The problem is that in the table contents should have tag_id, but you can solve it in this way in the Content model
class Content extends Model {
public function tag() {
return $this->belongsTo('App\Tag');
}
}
class Tag extends Model {
public function contents() {
return $this->hasMany('App\Content', 'tag');
}
}
This was the solution:
Change column name tag to tag_id and change return $this->belongsTo('App\Tag'); to return $this->belongsTo('App\Tag','tag_id');
The final code was:
Content Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Content extends Model {
public function tag() {
return $this->belongsTo('App\Tag','tag_id');
}
}
Tag Model:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model {
public function contents() {
return $this->hasMany('App\Content');
}
}
Thank you all for the help.
Refer the table name inside model
protected $table ="<table_name>"