Undefined variable: forum Hi...
Where is this problem?
Route::resource('/forum','ForumController');
public function index()
{
$forum = Forum::all();
return view('forum.index')->withForum($forum);
}
@foreach($forum as $forum)
<article class="well well-sm">
<div class="row">
<section class="col-md-7 title">
<a href="#">{{ $forum->title }}</a>
</section>
<section class="col-md-5 bio">
<span class="label label-primary">پاسخ : ۱۰</span>
<span class="label label-info">تشکر : ۱۰۰</span>
<span class="label label-success">RaymondDragon</span>
</section>
</div>
</article>
@endforeach
Erorr:
ErrorException (E_ERROR)
Undefined variable: forum (View: /opt/lampp/htdocs/forum/resources/views/forum/includes/post-list.blade.php) (View: /opt/lampp/htdocs/forum/resources/views/forum/includes/post-list.blade.php)
just do
public function index()
{
$forums = Forum::all();
return view('forum.index', compact('forums'));
}
@foreach($forums as $forum)
<article class="well well-sm">
<div class="row">
<section class="col-md-7 title">
<a href="#">{{ $forum->title }}</a>
</section>
<section class="col-md-5 bio">
<span class="label label-primary">پاسخ : ۱۰</span>
<span class="label label-info">تشکر : ۱۰۰</span>
<span class="label label-success">RaymondDragon</span>
</section>
</div>
</article>
@endforeach
@rob897
<?php $__currentLoopData = $forums; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $forum): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
<article class="well well-sm">
<div class="row">
<section class="col-md-7 title">
<a href="#"><?php echo e($forum->title); ?></a>
</section>
<section class="col-md-5 bio">
<span class="label label-primary">پاسخ : ۱۰</span>
<span class="label label-info">تشکر : ۱۰۰</span>
<span class="label label-success">RaymondDragon</span>
</section>
</div>
</article>
<?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
You should try this:
public function index()
{
$forums = Forum::all();
return view('forum.index',compact('forums'));
}
OR
public function index()
{
$forums = Forum::all();
return view('forum.index')->with(['forums'=>$forums]);
}
@foreach($forums as $forum)
<article class="well well-sm">
<div class="row">
<section class="col-md-7 title">
<a href="#">{{ $forum->title }}</a>
</section>
<section class="col-md-5 bio">
<span class="label label-primary">پاسخ : ۱۰</span>
<span class="label label-info">تشکر : ۱۰۰</span>
<span class="label label-success">RaymondDragon</span>
</section>
</div>
</article>
@endforeach
Please check my updated answer
@saurabhd
Check your answer and unfortunately your code did not work.
If you aren't including this partial post-list.blade.php in your index view, then of course it won't work.
@Jaytee
I changed this item. Unfortunately, it did not work
can u paste code of forum.index.blade.php here so that we can have better clarity?
@satish9323
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\ForumController;
use App\Forum;
class ForumController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$forums = Forum::all();
return view('forum.index')->with(['forums'=>$forums]);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('forum.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this -> validate($request, array(
'title' => 'required|max:200',
'post' => 'required'
));
$forum = New Forum;
$forum -> title = $request -> title;
$forum -> post = $request -> post;
$forum->save();
return redirect()->route('forum.show', $forum->id)->withMassage('نوشته شما با موفقیت ثبت شد :)');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
$forum = Forum::find($id);
return view('forum.show')->withForum($forum);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
}
That is controller file - ForumController.php. Please give as view - e.g. forum.index.blade.php.
@jovanvta
This is not a file forum.index.blade.php.
Show us the file where this section of your code is stored:
@foreach($forum as $forum)
<article class="well well-sm">
<div class="row">
<section class="col-md-7 title">
<a href="#">{{ $forum->title }}</a>
</section>
<section class="col-md-5 bio">
<span class="label label-primary">پاسخ : ۱۰</span>
<span class="label label-info">تشکر : ۱۰۰</span>
<span class="label label-success">RaymondDragon</span>
</section>
</div>
</article>
@endforeach
Is this your error?
ErrorException (E_ERROR)
Undefined variable: forum (View: /opt/lampp/htdocs/forum/resources/views/forum/includes/post-list.blade.php) (View: /opt/lampp/htdocs/forum/resources/views/forum/includes/post-list.blade.php)
Problem is in post-list.blade.php file.
@jovanvta
Yes...The problem is with post-list.blade.php, but I still could not fix this problem ...
Off course it will not work, the way you are the forum data variable is not found, you need to create a view composer and bind the forum data to the view ..
View Composers
So you can attach a composer to multiple views ..
Then show us post-list.blade.php.
@jovanvta
Yes...To display the title on the index page.
Fuck me!
You've shown us the ForumController, but we don't need that. What we need is the the contents of the view file (forum/index.blade.php & post-list.blade.php)
Please, 19 replies and still no where near to what everyone has asked for.
Might as well bail out now @Jaytee lol - OP is obviously a help vampire who is just copying and pasting and not really thinking about what they are doing.
Oh my for the love of the linux gods...
ErrorException (E_ERROR)
Undefined variable: forum (View: /opt/lampp/htdocs/forum/resources/views/forum/includes/post-list.blade.php) (View: /opt/lampp/htdocs/forum/resources/views/forum/includes/post-list.blade.php)
$forum isnt being defined so OBVIOUSLY the error will be in post-list.blade.php.
Solution -> pass the $forum so it is defined.
public function index(){
$forums = Forum::all();
return view('forum.index')
->with('forum', $forum);
}
Have a nice day.
@blueshift9 Meet you at the pub in about 20 minutes, have a pint and watch this shit blow over lol.
post.blade.php
@foreach($forums as $forum)
<article class="well well-sm">
<div class="row">
<section class="col-md-7 title">
<a href="#">{{ $forum->title }}</a>
</section>
<section class="col-md-5 bio">
<span class="label label-primary">پاسخ : ۱۰</span>
<span class="label label-info">تشکر : ۱۰۰</span>
<span class="label label-success">RaymondDragon</span>
</section>
</div>
</article>
@endforeach
show.blade.php
@extends('layouts.main')
@section('content')
<div class="container">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<div class="row">
<div class="main">
<div class="col-lg-8 col-md-8 col-sm-9 col-xs-12">
<div class="well pagec">
{{ $forum->title }}
{{ $forum->post }}
</div>
</div>
<div class="col-lg-4 col-md-4 col-sm-3 col-xs-12">
@include('forum.includes.flash')
</div>
</div>
</div>
@include('forum.includes.footer')
</div>
</div>
</div>
@endsection
index.blade.php
@extends('layouts.main')
@section('content')
<div class="container">
index
</div>
@endsection
Yeah since you passed the $forums variable to index.blade.php, it means that post.blade.php doesn't have access to it.
What you need to do in index.blade.phpis include post.blade.php
@extends('layouts.main')
@section('content')
<div class="container">
@include('forum.includes.post')
</div>
@endsection
The better way to do it would be to remove the foreach loop from the post.blade.php:
@extends('layouts.main')
@section('content')
<div class="container">
@foreach($forums as $forum)
@include('forum.includes.post')
@endforeach
</div>
@endsection
Then you would just use the $forum variable in forum.includes.post view.
According variables have changed and you did not work.
public function index()
{
$forums = Forum::all();
return view('forum.index',compact($forums));
}
public function index(){
$forums = Forum::all();
return view('forum.index')
->with('forums', $forums);
}
index.blade.php
@extends('layouts.main')
@section('content')
<div class="container">
@include('forum.includes.post')
</div>
@endsection
post.blade.php
@foreach($forums as $forums)
<article class="well well-sm">
<div class="row">
<section class="col-md-7 title">
<a href="#">{{ $forums->title }}</a>
</section>
<section class="col-md-5 bio">
<span class="label label-primary">پاسخ : ۱۰</span>
<span class="label label-info">تشکر : ۱۰۰</span>
<span class="label label-success">RaymondDragon</span>
</section>
</div>
</article>
@endforeach
In your post.blade.php you have:
@foreach($forums as $forums)
shouldn't it read:
@foreach($forums as $forum)
and then call:
{{ $forum->title }}
??
Please sign in or create an account to participate in this conversation.