view doesn't work in laravel - php

I am newbie for laravel and doing my first project called blog. For post article i can fetch data but I am trying to display reader's comment in the index page just below the article by fetching from database but it gives error(For info,I have already inserted a row from xammp just to fetch).Here is the code for PostController.php
public function index()
{
//$show = Post::all();
$show = Post::orderBy('id','desc')->paginate(1);
return view('pages/blog')->with('post',$show);
}
public function comment()
{
$show = readerComment::all();
return view('pages/blog')->with('commentShow',$show);
}
In index pages or blog.blade.php
#if(count($post)>0)
#foreach($post as $article)
<div class = "row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
<!-- Comment section -->
<div class = "comment">
<h3>Comments</h3>
#foreach($commentShow as $commShow)
<div class = "row">
<div class = "col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
#endforeach
</div>
And in web route
Route::resource('posts','PostController');
Route::get('/','PostController#comment');
I get error as
Undefined variable: post (View: C:\xampp\htdocs\blogging\resources\views\pages\blog.blade.php)
Any help would be appreciated. Thanks

Do it like that,
public function index()
{
$posts = Post::orderBy('id','desc')->paginate(1);
return view('pages.blog',compact('posts'));
}

You're blade looks good without error, but let's try this view..
#extends('layouts.app')
#section('content')
#if(count($post)>0)
#foreach($post as $article)
<div class = "row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
<!-- Comment section -->
<div class = "comment">
<h3>Comments</h3>
#foreach($post as $commShow)
<div class = "row">
<div class = "col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
#endforeach
</div>#endsection

Here you are defining your Eloquent Collection of models to be $commentShow:
return view('pages/blog')->with('commentShow',$show);
In your view, you are using the variable $post.
You need to change one of them to match the other.

in addition to this:
When you use the ->with() method the first parameter passed to it is
the name of the variable available on the view. So for the comment
part you make this call ->with('commentShow',$show); yet in your view
you try and access it via $post. Change this line #foreach($post as
$commShow) to #foreach($commentShow as $commShow)
change you view code to this: (please note the changed variable name in second #foreach)
#if(isset($post) && !empty($post))
#foreach($post as $article)
<div class="row">
<div class="col-md-12">
<h3 class="text-center">{{$article->title}}</h2>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p>{!!$article->article!!}</p>
</div>
</div>
#endforeach
#endif
#if(isset($commentShow) && !empty($commentShow))
<!-- Comment section -->
<div class="comment">
<h3>Comments</h3>
#foreach($commentShow as $commShow)
<div class="row">
<div class="col-md-12">
<p>{{$commShow->comment}}</p>
<p>{{$commShow->name}}</p>
</div>
</div>
#endforeach
</div>
#endif

Related

my error is undefined variable in laravel

I'm so confused, I was trying to fetch data from my database using laravel 8 and I'm sure I defined the variable right but it's having errors.
this is my controller:
public function homepage(){
$product_display = DB::table('products')->get();
return view('customer.homepage', compact('product_display'));
}
And this is the blade file:
<div class="content-wrapper pt-4">
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-3">
<div class="card border border-danger">
<div class="card-body">
</div>
</div>
</div>
<div class="col-lg-9">
<div class="card border border-danger">
<div class="card-body">
#foreach($product_display as $pd)
<div class="card">
<h3>{{ $pd->prod_name }}</h3>
</div>
#endforeach
</div>
</div>
</div>
</div>
</div>
</section>
</div>
it says, product_display is not defined.
Use #dd($pd) in Blade foreach loop. You will see if the variable defined or not.
DB::get() returns a stdClass so maybe tryto use view('customer.homepage')->with(['product_display' => $product_display]) instead

How to dump data into a view?

I get a list of number and series. But when i dump data in to a view it appears like this:
[{"id":1,"number":"e379079p272730","series":"88000000001","type":"import","group_series":null}]
I want on the view it will display the number value in the number column and the series value in the series column. But I don't know how to do that?
View admin.student.listcard
<div class="card-block" >
<div class="row">
<div class="col-md-3 ">
<div class="card-title">
<strong>Number</strong>
</div>
</div>
<div class="col-md-2">
<div class="card-title">
<strong>Series</strong>
</div>
</div>
</div>
#foreach($orders as $order)
<div class="row bottom">
<div class="col-md-3">
<div class="form-group">
{{ $order->card }}
</div>
</div>
<div class="col-md-2">
<div class="form-group">
</div>
</div>
</div>
#endforeach
</div>
function
public function listCard($studentId)
{
$orders = Order::where('member_id',$studentId)->get();
foreach($orders as $order){
$order->card = Card::where('id',$order->card_id)->get();
}
return view('admin.student.listcard',compact('orders'));
}
You have to use eloquent relationships !
In your Order model :
public function card()
{
return $this->belongsTo(Card::class);
}
In your controller :
$orders = Order::with('card')->where('member_id',$studentId)->get();
In your view :
#foreach($orders as $order)
<div class="row bottom">
<div class="col-md-3">
<div class="form-group">
{{ $order->card->number }}
</div>
</div>
<div class="col-md-2">
<div class="form-group">
{{ $order->card->series }}
</div>
</div>
</div>
#endforeach

Is my code wrong, or there is a bug in opencart

I've created extremely simple module for Opencart, just displaying hello world in the admin panel. Here is my controller:
<?php
class ControllerExtensionModuleHelloworld extends Controller {
public function index(){
$this->load->language('/extension/module/helloworld');
$this->document->setTitle('Hello World');
$data['heading_title'] = $this->language->get('heading_title');
$data['helloworld'] = $this->language->get('helloworld');
$data['header'] = $this->load->controller('common/header');
$data['column_left'] = $this->load->controller('common/column_left');
$data['footer'] = $this->load->controller('common/footer');
$this->response->setOutput($this->load->view('extension/module/helloworld', $data));
}
}
Here is my view:
{{header}}
<div class="container">
<div class="row>
<div class="col">
{{column_left}}
</div>
<div class="col">
<h1>{{heading_title}}</h1>
<p>{{helloworld}}</p>
</div>
</div>
</div>
{{footer}}
The problem is that the h1 and the p tags are displayed under the the column-left like this:
I changed the col classes with col-sm-4 and col-sm-8 like this:
{{header}}
<div class="container">
<div class="row>
<div class="col-sm-3">
{{column_left}}
</div>
<div class="col-sm-8">
<h1>{{heading_title}}</h1>
<p>{{helloworld}}</p>
</div>
</div>
</div>
{{footer}}
But this didn't work, finally I added col-sm-offset-2 class to the col-sm-8 div, and it shifted the text left, like it's suppose to be.
Any ideas where the problem may be?
{{ header }} {{column_left}}
<div id="content">
<div class="page-header">
<div class="container-fluid">
<h1>{{ heading_title }}</h1>
</div>
</div>
<div class="container-fluid">
<!-- content here -->
</div>
</div>
{{ footer }}
Try this, it should work.

Method does not exist error in laravel 5.2

I am working on laravel 5.2.I want to display those members who belongs to that particular group which is open at this time. Actually, i am getting all the members which i have stored in my database but, i only want to access or display only those members who belongs to a particular on which i am currently accessing. I am getting an error: Method groups does not exist. which is shown below:
My controller:
public function members($id){
$dashes=Grouptable::findorFail($id);
$members=Member::all();
return view('members' , ['dashes'=>$dashes,'members'=>$members]);
}
public function dashboard($id){
$dashes=Grouptable::findorFail($id);
return view('dashboard' , ['dashes'=>$dashes]);
}
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member);
return redirect()->back();
}
My view:
<body>
<div class="row">
<div class="col-lg-3 col-lg-offset-1">
<img src="images/ImgResponsive_Placeholder.png"
class="img-circle img- responsive" alt="Placeholder image"> </div>
<div class="col-lg-7">
<h1 style="color:black;">{{ $dashes->name }}</h1></div>
<br />
</div>
<div class="row">
<div class="col-lg-3">
<button class="btn btn-success" onclick="myFunction()">
Add Members + </button>
<div>
<form id="demo" style="display:none;" method="post"
action="{{ route('addmember') }}">
<input class="form-control" type="text" name="addmember">
<button class="btn btn-primary" type="submit">Add</button>
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
</div>
<div class="col-lg-7 col-lg-offset-0">
<div class="panel panel-default">
<div id="grp" class="panel-heading">
<h3 id="grouptitle" class="panel-title">Group Members</h3>
</div>
<div id="zx" class="panel-content">
<div class="row">
#foreach($members as $member)
<section class="col-md-6">
<div class="row">
<section class="col-md-offset-1 col-md-3 col-xs-offset-1
col-xs-4">
<img id="imagesize" src="images/g.jpg" class="img-circle"/>
</section>
<section class="col-md-offset-1 col-md-7 col-xs-7">
<section class="col-md-12">
<h5 id="friendname">{{$member->members}}</h5>
</section>
</section>
</div>
</section>
</div>
</section>
#endforeach
</div>
<div id="mn" class="panel-footer"><a id="seemr1"
href="#.html">See More</a></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
My routes:
Route::get('/members/{id}',[
'uses'=>'GroupController#members',
'as'=>'members'
]);
Route::get('/dashboard/{id}',[
'uses'=>'GroupController#dashboard',
'as'=>'dashboard'
]);
Route::post('/memeber/add',[
'uses'=>'GroupController#addmembers',
'as'=>'addmember'
]);
My modals:
Grouptable:
public function members(){
return $this->hasMany('App\Member');
}
Member:
public function groups(){
return $this->hasMany('App\Grouptable');
}
The way you are defining the relationship is wrong. I think this is OneToMany relationship. Like A Group has many users. Then you must define it like this
MODELS
Grouptable:
public function members(){
return $this->hasMany('App\Member','group_id'); // group_id represents the name of the foreign key. It is not neccessary if your foreign key is Grouptable_id, because Laravel automatically guess it unless the foreign key name is explicitly provided
}
Member:
public function groups(){
return $this->belongsTo('App\Grouptable','group_id');
}
To retrieve all members of a particular group say groupid 1:
$members = \App\Gouptable::find(1)->members;
CONTROLLER
Something seems to be wrong in your addmembers() function:
public function addmembers(Request $request){
$member=new Member();
$member->members=$request['addmember'];
$request->groups()->members()->save($member); //what is this groups()?
//You need to do something like this
$groupid = $request['groupid']; // send groupid as parameter from form
Grouptable::find($groupid)->members()->save($member);
return redirect()->back();
}

Problems while passing variable from controller to blade view

I have a model class
<?php
class CategoriaModificador extends Base
{
protected $table = 'categorias_modificadores';
public function restaurantes()
{
return $this->hasMany('CategoriaModificadorRestaurante', 'categorias_modificadores_id');
}
}
When I try to pass the variable to the blade view in my controller
public function show($id = 0)
{
if (!get_session_empresa()) {
return Redirect::route('empresa.logar')->with('message_error', 'Você precisa está logado');
}
$categoriaModificador = CategoriaModificador::where('empresas_id', (int)get_session_empresa()->id)
->where("id", (int)$id)
->first();
return View::make('frontend.' . $this->theme_base . '.categoria-modificador.show')
->with('categoriaModificador', $categoriaModificador);
}
I find this error
This is my show.blade.html
#extends('frontend.default.base_restrita')
#section('title')
Categoria de Modificador - {{ config_value('site_nome') }}
#stop
#section('content')
<section id="main-content">
<section class="wrapper">
<section class="panel">
<header class="panel-heading">
{{$categoriaModificador->nome}}
</header>
<div class="panel-body">
<div class="clearfix">
<div class="row">
<div class="col-lg-5">
<h4 class="pull-right"> Nome:</h4>
</div>
<div class="col-lg-6">
<h4 class="pull-left"> {{$categoriaModificador->nome}}</h4>
</div>
</div>
<div class="row">
<div class="col-lg-5">
<h4 class="pull-right"> Obrigatório:</h4>
</div>
<div class="col-lg-6">
<h4 class="pull-left"> {{$categoriaModificador->obrigatorio}}</h4>
</div>
</div>
<div class="row">
<div class="col-lg-5">
<h4 class="pull-right"> Máximo de Opções:</h4>
</div>
<div class="col-lg-6">
<h4 class="pull-left"> {{$categoriaModificador->maximo_opcoes}}</h4>
</div>
</div>
<div class="row">
<div class="col-lg-5">
<h4 class="pull-right"> Restaurantes:</h4>
</div>
#for($i=0;$i<$categoriaModificador->restaurantes->count();$i++)
#if($i==0)
<div class="col-lg-6">
<h4 class="pull-left">{{$$categoriaModificador->restaurantes[$i]->nome}}</h4><br>
</div>
#else
<div class="col-lg-5">
<h4 class="pull-right"></h4>
</div>
<div class="col-lg-6">
<h4 class="pull-left">{{$$categoriaModificador->restaurantes[$i]->nome}}</h4><br>
</div>
#endif
#endfor
</div>
</div>
</div>
</section>
</section>
</section>
#stop
Thanks for posting the view. I think the problem is you're using $categoriaModificador->restaurantes[$i]->nome, but it seems you do not have nome in your $categoriaModificador->restaurantes collection.
Also, why are you using double dollar sign with $$categoriaModificador?
If you clearly look at your error, you will find your solution yourself. Undefined variable error means you are printing something in the view file that is not defined.
For general solution, you should check if the variable is defined previously or is passed to the view. This may helps other.
Thanks

Categories