Laravel pass array to view using with - php

I have a problem with with method in view function. I would like to pass data variable and status. I do that:
return view('user.blogsettings')
->with(array('data' => $this->_data, 'status' => 'Save'));
Data item from array works ok, but status is not
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
There is no message.

The with() method on View doesn't send to session, it passes it as variable to the view so you can access it as $status instead of session('status')
If you want to explicitly add it to session then use session(['status' => 'Save']) and this would be flashed to the session for the ongoing request.
You may check Laravel's doc Passing Data to view for further information
I hope this is useful.

Related

Laravel 5.1 flash message handling

In Laravel 5.0 and 4.* there was this option to check if the Session had flash_messages:
Controller:
public function store(Request $request) {
Session::flash('flash_message', 'Success');
return redirect('somepage');
}
And then you could call this in your layout:
#if (Session::has('flash_message'))
<p>{{ Session::get('flash_message') }}</p>
#endif
This worked perfectly fine, however in Laravel 5.1 it changed. Now it's stored in the request variable. So, I changed it in the controller to:
public function store(Request $request) {
$request->session()->flash('flash_message', 'Success');
return redirect('somepage');
}
And I call this in the layout:
#if($request->session()->has('flash_message'))
<p>{{ $request->session()->get('flash_message') }}</p>
#endif
I'm doing exactly what is stated in the Laravel 5.1 docs:
Determining If An Item Exists In The Session
The has method may be used to check if an item exists in the session.
This method will return true if the item exists:
if ($request->session()->has('users')) { // }
Now this works perfectly fine for store method now, because I only added a flash_message there, but if I want to access another method like index, create, edit, etc. it shows this error:
ErrorException
Undefined variable: request
I understand that the request variable is not declared in the other methods and I can workaround this if-statement using isset($request).
Is there a better solution to check if my flash_message is set in the $request variable like in the earlier versions of Laravel?
It sounds like you're using controllers generated by Laravel.
If you look at the method definitions for store and update you'll find the parameter Request $request. By including this Laravel automatically creates a Request instance for you.
In order to access this in other methods you have three options:
Add the same parameter to any of the other method definitions and Laravel will generate it for you in the same way.
Create a constructor function and have it assign a Request instance to an attribute.
public function __construct(Request $request)
{
$this->request = $request;
}
Declare use Session; before the class definition to make the Session class accessible in your namespace.
You can still use the old method :) . latest laravel just added new ways to achieve that goal.here goes the sample code to display error messages which are passed as flash message into a view page .
Sample code
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<h4>Success</h4>
#if(is_array($message))
#foreach ($message as $m)
{{ $m }}
#endforeach
#else
{{ $message }}
#endif
</div>
#endif

Using Form button/Eloquent to set an entire column to null

I am having a real issue getting a button to scan and clear out an entire column using an eloquent model. I have two columns in my SQLite DB, "States" and "Totals"... I want the States to stay in their own order, but I want the totals to be cleared out upon the user selecting a button. The character type for 'totals' is BigInt... After the user selects the button, I want them redirected to the home page (with the values cleared so they can start over).
Here are my routes:
Route::resource('states', 'StateController');
Route::get('/', 'StateController#index');
Route::post('create', 'StateController#store');
Route::post('states.update', 'StateController#update');
Here is my controller:
public function update()
{
Distributors::update(['total' => null]);
return View::make('states');
}
Here is my form:
{{ Form::open(['route' => 'states.update']) }}
{{ Form::submit('Destroy and Start Anew') }}
{{ Form::close() }}
The error I get is:
MethodNotAllowedHttpException
Is there a simple issue with my routes? I can't figure it out.
You did not specify a method attribute on your form, so it will automatically execute a GET request. Your state.update route is only setup to accept POST requests.
Change your form to this:
{{ Form::open(['route' => 'states.update', 'method' => 'post']) }}
Please delete
Route::resource('states', 'StateController');
in your route and try again.

Populate form with Auth::user()

I'm trying to populate a form with my logged in user so I can edit some profile information.
I currently have this code (only trying to display the email first, will populate the form later once this works):
I made an editprofile.blade.php template with:
#extends('masterpage')
#section('content')
<h1> Edit Profile </h1>
{{ Form::model($user) }}
<div class="form-group">
{{ Form::label('email', 'E-mail') }}
{{ Form::Text('email', null, ['class' => 'form-control']) }}
</div>
{{ Form::close() }}
#stop
In myProfileController.php I have:
<?php namespace App\Http\Controllers;
use Auth;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Html\FormFacade;
use Illuminate\Html\HtmlFacade;
class ProfileController extends Controller {
public function show() {
$user = Auth::user();
return view('pages.editprofile')->withUser($user);
}
}
In my route I added:
Route::get('/profile/edit', 'ProfileController#show');
But when I try to load the page I get this:
I have two questions:
1: Why isn't there a form showing up with the data? As you can see the email is being loaded into the form but it displays it as text.
2. Once the data is loaded into a succesful form and the user has edited some of the data, how would I make update function to save the edited data?
I think that you are just outputting the raw data because probably you are using double brackets {{ }}.
As i remember i had a same thing when i migrated a site from laravel 4 to laravel 5. They changed the way blade output and echo the data. try to change double brackets {{ }} to this {{!! !!}} or this {{{ }}} i don't remember exactly which one it is.
Edit: about saving you can use controllers so when user post the data it goes to something like the same url just with post method, so it calls the update function inside controller.
For some functions where user should have full control over his profile like edit, update, delete, create i would use the examples from this http://laravel.com/docs/5.0/controllers#restful-resource-controllers take a look at restful controller

Data is not being passed with redirect

Bellow is a code of redirect to route. I am trying to get success variable in view using $success but it is giving error "Undefined variable: success". I also tried passing data in array format to with method. How can I correct it?
if ($data->save())
return redirect('/cases/ppost/notification')->with('success', 'Your case has been posted.')->with('caseNo', $data->id);
According to the documentation the redirect()->with() method does not assign any data to your view, but flashes it to the session:
http://laravel.com/docs/5.0/responses#redirects
Returning A Redirect With Flash Data
Redirecting to a new URL and flashing data to the session are
typically done at the same time. So, for convenience, you may create a
RedirectResponse instance and flash data to the session in a single
method chain:
return redirect('user/login')->with('message', 'Login Failed');
This means you will have to access it as such in your view. That would probabaly look something like this:
#if (Session::has('message'))
<div class="alert alert-info">{{ Session::get('message') }}</div>
#endif
wouldn't that be ??
Redirect::route('/cases/ppost/notification')->with( array('success'=>'Your case has been posted.','caseNo'=>$data->id)) ;

MethodNotAllowedHttpException on resource defined method Laravel-4

I created a very simple form so that I could use a submit button rather than a link to open up an edit users page. Using a link works perfectly, but the form button fails and yields a MethodNotAllowedHttpException even though the method ("edit") is perfectly defined in the UsersController resource and otherwise works fine.
Route:
Route::resource('users','UsersController');
UsersController:
public function edit($id)
{
$user = $this->user->find($id);
return View::make('users.edit')->with('user',$user);
}
show.blade.php:
<!-- This works fine: -->
{{ link_to_route('users.edit', ("Edit: " .$user->first_name." ".$user->last_name), $user->id) }}
<!-- This doesn't work, and yields the Method Not Allowed exception: -->
{{ Form::open(array('route' => array('users.edit',$user->id))) }}
{{ Form::submit('Edit User', array('class'=>'button')) }}
{{ Form::close() }}
Thanks.
When you do Form::open(), it defaults to using the post request method. But when you create a Route::resource(), the edit method takes a get request.
To make it work through the form, you'll need to open it with an additional parameter, like this:
{{ Form::open(array('route' => array('users.edit',$user->id),
'method' => 'get')) }}
You need to point to the update route, not edit.
{{ Form::open(array('route' => array('users.update', $user->id))) }}
The edit route is for displaying the view, while the update is for the put/patch request.
For more information about using the RESTful routes, I'd recommend checking out http://laravel.com/docs/controllers#resource-controllers

Categories