Reputation: 103
My error: Undefined variable: asd (View: C:\xampp\htdocs\kproject\resources\views\posts\index.blade.php)
Controller:
use App\post;
public function index()
{
$asd = post::all();
return view ('posts.index')->with('post','$asd');
}
Index.blade.php
@extends('layout.app')
@section('content')
<h1> Peoples</h1>
<hr>
@if(count($asd)>0)
@foreach($asd as $post)
<p> {{$post->username}} </p>
@endforeach
@else
<p> No Posts </p>
@endif
@endsection
I don't know why I get this error, please help needed!
Upvotes: 2
Views: 1578
Reputation: 1
I identified this problem. You send the posts variable in the index function.
public function index(){
$posts = Post::all();
return view('posts.index',compact('posts'));
}
But most likely you get this error when you want to create a new post, so here the store function is executed and the index function is not executed in the controller, because the store function is responsible for storing form information. Then you edit this function as follows.
public function store(Request $request) {
$post = new Post();
$post->title = $request->input("title");
$post->description = $request->input("description");
$post->save();
$posts = Post::all(); //Send posts in compact
return view("posts.index", compact($posts));
}
Excuse me for the typing problem, I am a Persian speaker ❤
Upvotes: 0
Reputation: 1301
Remover upper dashes from '$asd'
,
change this to your controller
use App\post;
public function index()
{
$asd = post::all();
return view ('posts.index')->with('post','$asd');
}
to
use App\post;
public function index()
{
$asd = post::all();
return view ('posts.index')->with('post',$asd);
}
Assigned $asd varible to post then in view you have to iterate post varible not $asd,
In index.blade.php,
@extends('layout.app')
@section('content')
<h1> Peoples</h1>
<hr>
@if(count($asd)>0)
@foreach($asd as $post)
<p> {{$post->username}} </p>
@endforeach
@else
<p> No Posts </p>
@endif
@endsection
Change it to,
@extends('layout.app')
@section('content')
<h1> Peoples</h1>
<hr>
@if(count($post)>0)
@foreach($post as $single_post)
<p> {{$single_post->username}} </p>
@endforeach
@else
<p> No Posts </p>
@endif
@endsection
Upvotes: 0
Reputation: 103
Since I am new to laravel I get a bit confused at times and yet I m not understanding the whole process.
Firstly i removed the quotes from $asd but i have passed the same variable to view.
As mentioned above by @Bostjan, I would like to know the process, can anyone clarify this to me
You are passing $asd variable to view ... but with a different name. Name in view would be
$post->with('post', $asd)
... pass $asd variable to view and assign it to $post
Upvotes: 0
Reputation: 1609
You can be passing data to Laravel view by 2 type of method.
1 Using ->with()
method.
Your Controller method
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function index()
{
$post_data = post::all();
return view('posts.index')->with('post',$post_data);
}
}
OR
2 You can pass an array the second argument of view()
method.
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class PostController extends Controller
{
public function index()
{
$post_data= post::all();
$post_data = ['post' => $post_data]
return view('posts.index')->with('post',$post_data);
}
}
You can access it in Index.blade.php
by following
{{$post}}
Upvotes: 0
Reputation: 814
You are passing $asd
variable to view ... but with a different name. Name in view would be $post
.
->with('post', $asd)
... pass $asd variable to view and assign it to $post
change index.blade.php to something like:
@extends('layout.app')
@section('content')
<h1> Peoples</h1>
<hr>
@if(count($post)>0)
@foreach($post as $p)
<p> {{$p->username}} </p>
@endforeach
@else
<p> No Posts </p>
@endif
@endsection
Upvotes: 4