I am trying to pass a simple $name variable via controller.
Controller file:
if($validator->fails())
{
$this->layout->content = View::make('admin.login' )->with('name',$name);
}
Note:
protected $layout = 'admin.layout.login_master';
$layout is class member for login page layout
View file:
#section('content')
<h1>Admin Login</h1>
{{ Form::open(array('url' => 'admin/login','method'=>'post')) }}
<div style="color:red">
{{ $name }}
</div>
<div id="row">
<div class="form-group">
{{ Form::label('username', 'Username') }}
{{ Form::text('username', '', array('class' => 'form-control')) }}
</div>
</div>
<div id="row">
<div class="form-group">
{{ Form::label('password') }}
{{ Form::password('password', array('class' => 'form-control')) }}
</div>
</div>
<div id="row">
<div class="form-group">
{{ Form::submit('Login',array('class'=>'btn btn-default')) }}
</div>
</div>
{{ Form::close() }}
#stop
it's showing an error in the log file:
Next exception 'ErrorException' with message 'Undefined variable: name (View: C:\xampp\htdocs\portfolio\app\views\admin\login.blade.php)' in C:\xampp\htdocs\portfolio\app\storage\views\94976abb04474489c25123342a2993a1:7
Stack trace:
and in the browser it's showing:
Whoops, looks like something went wrong.
If this is not working, there are other ways also to pass variables from controller to view in Laravel
if you have $name variable defined and want to access in views with $name then u can try any of following
$this->layout->content = View::make('admin.login' )->withName($name);
$this->layout->content = View::make('admin.login' )->with(compact('name'));
$this->layout->content = View::make('admin.login', array('name'=>$name) )
Related
newbie here practicing laravel, im making an inventory for movies using laravel 5.6.. where I can do basic CRUD functionalities.
In here, Im sorting movies by genre. however I came up with this error Invalid argument supplied for foreach();
here is my Route: web.php
Route::resource('movies', 'MoviesController');
Route::get('movies/year/{movie}', 'MoviesController#released');
Route::get('movies/genre/{genre}', 'MoviesController#getgenre');
here is my controller: MoviesController.php
public function getgenre($genre){
$movies = Movie::where('genre', $genre)->whereIn('status', [1])->orderBy('title', 'asc')->get();
return view('genre', compact('movies'));
}
here is my view: genre.blade.php
#extends('layouts.app')
#section('content')
<div class="row">
<div class="col-md-10">
#if($movies->count() < 1)
<h1 style="text-align: center">No Movie Found</h1>
#else
#foreach($movies as $movie)
<div class="card" style="margin-bottom: 10px;">
<div class="card-body">
<div class="card-title">
{{ ucwords($movie->title) }}
</div>
<div class="card-text">
{{ ucfirst($movie->genre) }} | {{$movie->released}} |
#if($movie->seen == 1)
Seen: Yes
#else
Seen: No
#endif
<div style="margin-top: 5px">
{{ Form::open(['route' => ['movies.edit', $movie->id], 'class' => 'formupdate', 'method' => 'GET']) }}
{{ Form::submit('Update', ['class' => 'btn btn-primary col-md-2']) }}
{{ Form::close() }}
</div>
<div style="margin-top: 5px">
{{ Form::open(['route' => ['movies.destroy', $movie->id], 'class' => 'formdelete', 'method' => 'DELETE']) }}
{{ Form::hidden('hidden', 'hidden') }}
{{ Form::submit('Delete', ['class' => 'btn btn-danger col-md-2']) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
#endforeach
<div class="pagination" style="margin-top: 10px; text-align: center;">
{{ $movie->links() }}
</div>
#endif
</div>
<div class="col-md-2">
</div>
</div>
#stop
I tested it dd(): and it has results:
tried testing it using dd()
here is the error:
here is the error
thanks,
you have two issues:
1- Update this whereIn expects array and you use pagination in view and you do not return the data as pagination
$movies = Movie::where('genre', $genre)->whereIn('status', [1])>orderBy('title', 'asc')->paginate(10);
2- in the view you written $movie->links(); update it to
$movies->links();
Update this line, whereIn expects array
$movies = Movie::where('genre', $genre)->whereIn('status', [1])->orderBy('title', 'asc')->get();
I am trying to edit a record in a table. I have created a route and the form, but I can't get past this error. I have figured out the problem but I can't find a fix. Am I correct in thinking that the edit.blade.php file needs the $ad->id passing?
The $ad->id is an ID of a specific add in a List View. The list view has all the tickets displayed from a table, and the below link is meant to edit that one item.
The edit route is accessed using following code:
Edit
I have one route that is supposed to open up the edit view form:
Route::get('/ticket_ads/edit/{ad}', 'TicketAdsController#editTicketAdForm')->name('ticket.edit');
The above route points to this in the controller:
public function editTicketAdForm($id)
{
//$ad = DB::table('ticket_ads')->where('id', $id)->value('id');
return view('Ads.edit')->with('id', $id);
}
This is the view called by the above function:
#extends('Shared.Layouts.MasterWithoutMenus')
#section('title')
Edit a ticket ad
#stop
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading"><h2>Edit your ticket ad</h2></div> <br/>
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
<div class="form-group">
{{ Form::label('title', 'Title') }}
{{ Form::text('title', Input::old('title'), array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('comment', 'Comment') }}
{{ Form::text('comment', Input::old('comment'), array('class' => 'form-control')) }}
</div>
{{ Form::submit('Submit', array('class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
</div>
</div>
</div>
#endsection
This is the line that throws the error
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
The ID displays normally in the URL as ticket_ads/edit/7 for example.
How do I get past this?
Change this line:
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
to this:
{{Form::open(array('route' => array('ticket.edit', $id)))}}
This
{{ Form::open(array('route' => 'ticket.edit', $id = 'id')) }}
is wrong. Correct syntax is:
{{ Form::open(['route' => ['ticket.edit', $id]]) }}
also you should safely ditch using array() in favor of [] syntax as Laravel requires PHP 5.4+ anyway (unless you are using ancient version of Laravel, like v4?)
The correct syntax for calling route is.
{{ route('/cardetails', ['121','cars'] ) }}
In URL it will be like this below line.
127.0.0.1:8000/cardetails/121/cars
I'm trying to update the fields in the database, but I couldn't
here is my routes :
Route::get('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
here the controller:
public function postOrder()
{
$this->orderForm->validate(Input::all());
$order = $this->orders->getNew([
'link' => Input::post('link'),
'size' => Input::post('size'),
'color' => Input::post('color')
]);
$this->orders->save($order);
return Redirect::back()->withMessage('Order has been updated');
}
here is the blade:
{{ Form::open() }}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
so each time I try to update the order I got error "MethodNotAllowedHttpException ", I tried a lot of methods but I'm lost. I'm still beginner in php and this problem drive me crazy, so I'll be glad if you can help guys.
thanks
*** I've updated the code
So you're posting to the route, /orders. Therefor you need a HTTP POST request. You're now assigning a GET request to the /orders route.
You need to change your code to:
Route::post('orders', [
'uses' => 'OrderController#postOrder',
'as' => 'order.show'
]);
Also you need to add a CSRF Token, this can be done through adding {!! csrf_field() !!} in your blade (inside your Form open and close).
{{ Form::open() }}
{!! csrf_field() !!}
<div class="box-body">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('title', 'Product:') }}
{{ Form::text('title', $order->title, ['class' => 'form-control', ]) }}
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('link', 'Link:') }}
{{ Form::text('link', $order->link, ['class' => 'form-control']) }}
</div>
</div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
{{ Form::label('size', 'Size:') }}
{{ Form::text('size', $order->size, ['class' => 'form-control']) }}
</div>
</div>
<div class="col-lg-6">
</div>
</div>
<div class="box-footer">
{{ Form::submit('Save', ['class' => 'btn btn-primary']) }}
</div>
{{ Form::close() }}
Hope this works!
You must specify the method in the Form::open method.
{{ Form::open(array('method' => 'post')) }}
Just added this in the repository:
public function updateOrder($id, array $data)
{
$orders = $this->getById($id);
if (!empty($data['title'])) {
$orders->title = $data['title'];
}
if (!empty($data['link'])) {
$orders->link = $data['link'];
}
(AND SO ON)
$orders->save();
and in the controller:
public function postOrder($id)
{
$this->orders->updateOrder($id, Input::all());
return Redirect::back()->withMessage('Updated');
}
and that's it
Im making a simple blog with Laravel and I have some issues with the comments section. I'm passing the article_id trough the comments form but I get this error.
This is the Article View
<?php
$comment = new Comment;
?>
#extends('layout')
#section('showArticle')
<div class="row">
<div class="col-lg-12">
<h4>{{$idArticle->title}}</h4>
<p>{{$idArticle->text}}</p>
<div class="col-lg-4">Autor: {{$idArticle->author}}</div>
<div class="col-lg-4">Categorias</div>
</div>
</div>
<div class="comments">
<p>Comentarios</p>
</div>
<div>
{{Form::open(array('route' => array('comments.store', $idArticle->id, 'method' => 'POST')))}}
#include ('errors', array('errors' => $errors))
<div class="row">
<div class="form-group col-md-4">
{{ Form::label('comment', 'Contenido') }}
{{ Form::textarea('comment', null, array('placeholder' => 'Comment', 'class' => 'form-control')) }}
</div>
</div>
{{ Form::button('Publicar', array('type' => 'submit', 'class' => 'btn btn-primary')) }}
{{ Form::close() }}
</div>
#stop
The controller
public function store($idArticle)
{
var_dump($id);
exit();
}
And my route
Route::resource('comments', 'CommentsController');
What could be wrong?
Try it
Change the Article View
{{Form::open(array('route' => array('comments.store', 'method' => 'POST')))}}
{{ Form::hidden('article_id',$idArticle->id) }}
.......
.......
Change Your Controller
public function store()
{
var_dump(Input::all());
exit();
}
It sounds like you're looking for nested resources.
Change your route to:
Route::resource('articles.comments', 'CommentsController');
This will generate urls like:
articles/{articleId}/comments
articles/{articleId}/comments/{commentId}
Now you open your form as you did before (with updated route name):
{{Form::open(array('route' => array('articles.comments.store', $idArticle->id), 'method' => 'POST'))}}
i have created a messaging application. Main page contains two forms. One for registration and one for login. Both forms have different corresponding routes for processing. . On localhost, everything is working fine. Users are able to register and then login.
Today i uploaded files on the web server. Now it started showing token mismatch error. I searched a lot, but was unable to solve this.
Here is the View(named as login.blade.php) that has two forms
<div id="header">
<center>
<div id='loginform'>
{{ Form::open(array('url' => 'login')) }}
<div id='login_inputs_div'>
{{ Form::text('email', Input::old('email'), array('placeholder'=>'Email', 'class'=>'inputs')) }}
{{ Form::password('password', array('placeholder'=>'Password', 'class'=>'inputs')) }}
{{ Form::submit('Log In', array('class'=>'submit')) }}
</div>
<div id='error_wrapper'>
<div id='email_error'>{{ $errors->first('email') }}</div>
<div id='password_error'>{{ $errors->first('password') }}</div>
<div class='clear'></div>
</div>
{{ Form::close() }}
</div>
</center>
</div>
<div id="body">
<div id='registration_alert_div'>
<center>
#if(Session::has('registration'))
<div id="registration_alert">
{{ Session::get('registration') }}
</div>
#endif
</center>
</div>
<div id="regform">
{{ Form::open(array('url' => 'login/register')) }}
<center><h1>Register To Make Friends</h1></center>
{{ Form::text('name', Input::old('name'), array('id'=>'name', 'placeholder'=>'Name', 'class'=>'reginputs')) }}
<span>{{ $errors->first('name') }}</span>
{{ Form::text('username', Input::old('username'), array('id'=>'username', 'placeholder'=>'Username', 'class'=>'reginputs')) }}
<span>{{ $errors->first('username') }}</span>
{{ Form::password('Password', array('id'=>'password', 'placeholder'=>'Password', 'class'=>'reginputs')) }}
<span>{{ $errors->first('Password') }}</span>
{{ Form::text('Email', Input::old('Email'), array('id'=>'email', 'placeholder'=>'Email', 'class'=>'reginputs')) }}
<span>{{ $errors->first('Email') }}</span>
{{ Form::text('ConfirmEmail', Input::old('ConfirmEmail'), array('id'=>'cemail', 'placeholder'=>'Confirm Your Email', 'class'=>'reginputs')) }}
<span>{{ $errors->first('ConfirmEmail') }}</span>
<center>{{ Form::submit('Register', array('class'=>'submit', 'id'=>'rsubmit')) }}</center>
{{ Form::close() }}
</div>
<div class='clear'></div>
</div>
Here are their respective routes:
//Route for forms display| showLogin method displays the view(login.route.php)
Route::get('login', array('uses' => 'HomeController#showLogin'));
//Route for processing login form
Route::post('login', array('before'=>'csrf', 'uses' => 'HomeController#doLogin'));
//route for processing registration form
Route::post('login/register', array('before'=> 'csrf', 'uses' => 'HomeController#register'));
I checked the source code and the token was present as an input field with some value. Why is it mismatching?
Laravel is probably not being able to keep a session in your webserver and everytime it starts a new session it creates a new token, which leads to the last one mismatch the current one. Some possible solutions:
1) Check if your webserver can write in app/storage, not only app/storage/session, because you might have problems with other files in that folder.
2) Change the session driver, for instance, from file to database (you will have to create the sessions table).