I'm trying to pass a variable from one view to a controller to another view. I'm not getting any errors, but when it gets to the last view, it doesn't show the variable like it's supposed to. In the first view, I'm just getting a name.
{{ Form::open(array('route' => 'form', 'method'=>'post')) }}
{{ $name = Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
Here is my HomeController.php.
public function view1()
{
return View::make('stuff');
}
public function postView1($name)
{
return Redirect::route('view2')->with($name);
}
public function view2($name)
{
return View::make('view2')->with($name);
}
routes.php
Route::get('/', array('as' => 'stuff', 'uses' => 'HomeController#stuff'));
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController#postView1'));
Route::get('view2/{name}', array('as' => 'view2', 'uses' => 'HomeController#view2'));
view2.blade.php
{{ $name = Input::get('name') }}
<p> Hello, {{ $name }} </p>
So why isn't it showing up?
First you should change your postView function into:
public function postView1()
{
return Redirect::route('view2', ['name' => Input::get('name')]);
}
And your route:
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController#postView1'));
into:
Route::post('form', array('as' => 'form', 'uses'=>'HomeController#postView1'));
Now, you should change your view2 function into:
public function view2($name)
{
return View::make('view2')->with('name',$name);
}
Now in your view2.blade.php you should be able to use:
<p> Hello, {{ $name }} </p>
You need to name the variable:
public function view2($name)
{
return View::make('view2')->with('name', $name);
}
class HomeController extends Controller {
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
}
public function index()
{
$data = array (
'title'=>'My App yo',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('home')->with($data);;
}
}
Try form will be if you using POST method why setting variable in route it will get directly on your function with post data.
{{ Form::open(array('url' => 'form', 'method'=>'post')) }}
{{Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
route :-
Route::post('form','HomeController#postView1');
controller function :-
public function postView1() {
$data = Input::all();
return Redirect::route('view2')->with('name', $data['name']);
}
and get data on view2 :-
<p> Hello, {{ $name }} </p>
For more follow HERE
Here's what other answers are missing, straight from Laravel docs:
Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.
So instead of {{$name}} write {{Session::get('name')}}.
Related
My controller index file looks like this:
class KJVController extends Controller
{
public $book = "Genesis";
public $chapter = 1;
public function index() {
$results = KJV::where('book', '=', $this->book)->where('chapter', '=', $this->chapter)->get();
return view("bible", compact('results'));
}
}
And in my blade template I want to be able to show the variables above ($book,$chapter).
I tried:
{{ KJVController::book }}
{{ KJVController->book }}
I can't seem to access it. How can I display this information in my blade template?
You need to pass these variables to the view first:
return view("bible", ['results' => $results, 'book' => $this->book, 'chapter' => $this->chapter]);
In the bible view:
{{ $book }}
Or you can just do this:
{{ app('App\Controllers\KJVController')->book }}
There's nothing wrong with your controller query. You just aren't returning the variable. You're trying to return the controller and as you're running ->get(), it returns an array. Therefore, you need to do this within the blade:
#foreach ($results as $book)
{{$book->book}}{{$book->chapter}}
#endforeach
Blade documentation
Imagine that we have the controller AdvertController.php with the edit action public function editAction(Request $request) that sends an object $user to the view edit.html.twig, $user contains many attributes ans methods that return a strings
public function editAction(Request $request){
$user = new User();
return $this->render('OCPlatformBundle:Advert:edit.html.twig', array());
}
What I need to add in the code ? So that in the view I can display the values
edit.html.twig
{{ user.name }}
Its in the array() part you already defined.
$x = 'someothervalue';
$this->render('template', [
'somekey' => 'somevalue',
'someotherkey' => $x
]);
Then in twig:
{{ somekey }}, {{ someotherkey }}
Try this:
public function editAction(Request $request){
$user = new User();
return $this->render('OCPlatformBundle:Advert:edit.html.twig',
[
'user' => $user,
]
);
}
In twig template call function dump(), and twig render all new User() data's:
{{ dump(user) }}
You can get them like this:
{{ user.name }}
{{ user.soname }}
Edit3: Could a reason be because both controllers are leading to the same page?
Edit2: Still not working after the answers I got.
Edit: Error one is solved, now I'm getting:
Undefined variable: project (View:
/var/www/resources/views/pages/showProject.blade.php)
Can this be because both variables are leading to the same page? The projects variable was working perfectly before the comment system.
public function index()
{
$projects = Project::all();
return view('pages.projects', compact('projects'));
}
Project variable declare.
I'm trying to get my comments from my database to show on a specific 'project page' in the laravel 5 project I'm working on. The idea is that the user can add art projects and other users can comment on them, but whenever I try to visit the page I get
Undefined variable: comments (View:
/var/www/resources/views/pages/showProject.blade.php)
This is my controller
public function index()
{
$comments = Comment::all();
return view('pages.showProject', compact('comments'));
}
public function store()
{
$input = Request::all();
$comment = new Comment;
$comment->body = $input['body'];
$comment->project_id = $input['project_id'];
$comment->user_id = Auth::user()->id;
$comment->save();
return redirect('projects/'.$input['project_id']);
}
These are my routes
// add comment
Route::post('projects/{id}','CommentController#store');
// show comments
Route::post('projects/{id}','CommentController#index');
And my view
#if (Auth::check())
<article> <!--Add comment -->
<br/>
{!! Form::open() !!}
{!! form::text('body', null, ['class' => 'form-control']) !!}
<br/>
{!! Form::Submit('Post Comment', ['class' => 'btn btn-primary form-control']) !!}
{!! Form::hidden('project_id', $project->id) !!}
{!! Form::close() !!}
<br/>
</article>
<article>
#foreach ($comments as $comment)
<article>
<p>Body: {{ $comment->body }}</p>
<p>Author: {{ $comment->user->name }}</p>
</article>
#endforeach
</article>
#else
<p>Please log in to comment</p>
#endif
The Model
class Comment extends Model
{
//comments table in database
protected $guarded = [];
// user who has commented
public function author()
{
return $this->belongsTo('App\User','user_id');
}
// returns post of any comment
public function post()
{
return $this->belongsTo('App\Project','project_id');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
public $timestamps = false;
}
Is there any way I can solve this?
Thanks in advance
First, you need to make sure that you are aliasing your 'Comment' model in your controller. This is done with the use statement.
use App\Comment;
class CommentController extends Controller
{
public function index()
{
$comments = Comment::all();
return view('pages.showProject', compact('comments'));
}
}
Second, you will need to change your route for showing comments from a POST request to a GET request. At the moment you are making identical routes and furthermore GET is the correct request type for retrieving data.
Route::get('projects/{id}','CommentController#index');
Third, you are referencing a $project variable in your view, but never passing it in from the controller. That needs to reference something.
I think your relationship is wrong. Try this:
Comment model:
class Comment extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
User model:
class User extends Model
{
public function comments()
{
return $this->hasMany('App\Comment');
}
}
In the view you can use:
#foreach($comments as $comment)
<p>{{ $comment->user->name }}</p>
#endforeach
I needed to empty the show in the commentsController and only use it for saving the comments. For showing them I made an extra function in my projectsController. It ended up being this;
public function show($id)
{
$project = Project::findOrFail($id)->load("User");
$input = Request::all();
//-----------------------DB-----------------------------//
$project_comments = DB::table('comments')
->select('body', 'name')
->where('project_id', '=', $id)
->join('users', 'users.id', '=', 'user_id')
->get();
//-----------------------DB-----------------------------//
return view('pages.showProject', ['project' => Project::findOrFail($id), 'comments' => $project_comments]);
}
I want to pass the selected item's $id to my controller for doing changing on it .
it's my index.blade.php (view)code
<table>
#foreach($posts as $p)
<tr>
<td>{{$p->title}}</td>
<td>{{substr($p->body,0,120).'[...]'}}</td>
<td>{{HTML::link('posts_show',' Preview',array($p->id))}}</td>
<td>{{HTML::link('posts_edit','Edit',array($p->id))}}</td>
<td>
{{Form::open(array('method'=>'DELETE','url'=>array('posts.delete',$p->id)))}}
{{Form::submit('Delete')}}
{{Form::close()}}
</td>
</tr>
#endforeach
</table>
but it doesnt pass $id to my controller's methods.
thanks for your time.
What you need to do is to set route parameter. Your route should be like that.
Route::get('post','postController#index');
Route::get('posts_create', function (){ return View::make('admin.newPost'); });
Route::get('posts_show/{id}','postController#show');
Route::get('posts_edit/{id}','postController#edit');
Route::post('posts_delete/{id}','postController#destroy');
If you want to use named route {{ Form::open(array('url' => route('posts.edit', $p->id))) }}, you need to set name like that.
Route::post('posts_edit/{id}', array('uses' => 'postController#edit',
'as' => 'posts.edit'));
You can check routing in laravel official documentation.
Edit
For now, your form in view look like that.
{{ Form::open(array('url' => route('posts.edit', $post->id), 'method' => 'POST')) }}
In route,
Route::post('posts_edit/{id}', array('uses' => 'postController#edit',
'as' => 'posts.edit'));
In PostController,
public function edit($id)
{
// do something
}
I hope it might be useful.
Looks like your route has a name posts.destroy, if that is the case you should use route instead of url as a parameter
{{Form::open(array('method'=>'DELETE','route'=>array('posts.destroy',$p->id)))}}
it's my route:
Route::get('post','postController#index');
Route::get('posts_create', function (){ return View::make('admin.newPost'); });
Route::get('posts_show','postController#show');
Route::get('posts_edit','postController#edit');
Route::post('posts_delete','postController#destroy');
it's my postController:
class postController extends BaseController{
public function show($id){
$post=Post::find($id);
$date=$post->persian_date;
return View::make('posts.show')->with('post',$post)->with('date',$date);
}
public function edit($id){
$post=Post::find($id);
if(is_null($post)){
return Redirect::route('posts.index');
}
return View::make('posts.edit')->with('post',$post);
}
public function update($id){
$input=array_except(Input::all(),'_method');
Post::find($id)->update($input);
return Redirect::route('posts.index');
}
public function destroy($id)
{
Post::find($id)->delete();
return Redirect::route('posts.index');
}
I've tried tons of hours now. Bu still cannot figure it out.
So, I want to return the form input on same page after submit, via the controller.
But it don't give me anything. If I only using return Input::get('email'); then it at least show the result in a white page.
Any idea how to proceed?
My codes looks like:
Controller:
class SuperController extends BaseController {
public function quote() {
if (Input::get('email') == "") {
return array('empty' => true);
} else {
return View::make('users',array(Input::get()));
}
}
}
Routes:
Route::post('users', 'SuperController#quote');
Users file:
#extends('layout')
#section('form')
{{ Form::open(array('action' => 'SuperController#quote')) }}
<!-- Email -->
{{ Form::token() }}
{{ Form::text('email'); }}
{{ Form::submit('LCLCL'); }}
{{ Form::close() }}
#stop
Use isset and with()
class SuperController extends BaseController {
public function quote() {
if (isset(Input::get('email'))) {
return array('empty' => true);
} else {
return View::make('users')->with('input', Input::get());
}
}
}
And a HTTP GET route
Route::get('users', 'SuperController#quote');