Laravel 5.6 - Grab message from Controller into view - php

I'm starting with Laravel and i need to show the output of a post request into the view. My controller file returns an array with a message, like this:
return redirect('/myroute')
->with('message', [
'type' => 'success',
'text' => 'It works'
]);
In my view file, i'm trying to grab the message text, but no success. See my code below
#if(Session::has('message'))
{{ $msg = Session::get('message') }}
<h4>{{ $msg->text }}</h4>
#endif
The point is: The condition works, if i changed the {{$msg->text}} to any text it works, but when i try to get the message text, it returns an error:
htmlspecialchars() expects parameter 1 to be string, array given
So, any help is apreciated. If more information is needed, just ask.
PS: i checked this question, but no success at all
EDITED:
PS2: Can't change controller structure

Try accessing the array as follows:
<h4>{{ $msg['text'] }}</h4>
or just pass an array with the items
->with([
'type' => 'success',
'text' => 'It works'
]);
//in the view
#if(session()->has('text'))
<h4> {{ session('text') }} </h4>
#endif
-- EDIT
iterate than over the session like so:
#foreach (Session::get('message') as $msg)
{{$msg['text']}}
#endforeach
you can read more about that here

Do this instead
return redirect('/myroute')->with('success','It worked');
Then on your view
{{session('success')}}

Related

laravel check if it exists and send an alert

I need help with Laravel
I need to get a code that runs: if and send a message ::
$text = App\Nota::select('nombre')->where('id', 3);
I need to check that if a string = 'hello' arrives it will print a message in .blade
in short check if it exists and send an alert
Help me please
you can do in this way.
$text = App\Nota::select('nombre')->where('id', 3);if($text->nombre=="hello"){Session::flash('message', 'This is a message!');Session::flash('alert-class', 'alert-danger'); }
In the view you can make it display like this.
#if(Session::has('message'))<p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{ Session::get('message') }}</p>#endif
Try this way using exist() method if you just want to check record is exists or not. Here, in your case, you just want to check that modal Nota having id 3 and it's nombre column should be equal to hello. There is no need to load the modal, you can try specifying both conditions in where() method and use exist() to check the record exist.
Controller
$exist = App\Nota::where([
'id' => 3,
'nombre => 'hello'
])->exists();
if(!$exist){
Session::flash('message', 'This is a message!');
Session::flash('alert-class', 'alert-danger');
}
View
#if(Session::has('message'))
<p class="alert {{ Session::get('alert-class', 'alert-info') }}">{{
Session::get('message') }}</p>
#endif

Laravel get data from dropdown list and pass it to the controller

I am trying to have the user choose a month from a dropdown list displaying the months, and passes that month value (in numbers) to the controller
I am having trouble making the dropdown list an action so once the user picks the month, the controller gets called.
here is my index page:
<h>Display a logs by monthly <h>
{{ $id=Form::selectMonth('month')}}
<a href="{{action('LogController#monthly',['id' => $id]) }}" class="btn btn-primary">Monthly Logs
</a>
and when I add the Form method inside the tag i am getting an error says the variable is not defined.
here is the Controller.php
public function monthly($id)
{
$dcmlogs = log::with('users')
->whereMonth('created_at', '=', $id)
->paginate(15);
return view('dcmlog.index', compact('dcmlogs'));
}
You can use something like this :
{!! Form::open(['route' => ['logs', $id], 'method' => 'POST' ])!!}
{{ Form::select('in_out',[1 => 'Jan', 2 => 'Feb'] , null, ['class'=>'
form-control'])
}}
{!! Form::close() !! }}
hope this helps ..
I recommend creatintg a route
Route::get('/logs/{id}', 'LogController#monthly')->name('logs');
And use the route function in the template:
route('logs', ['id'->$id]);

Laravel Passing Variable from Forms

I am brand new to Laravel, and following a super basic tutorial.
However the tutorial did not come with an edit record section, which I am attempting to extend myself.
Route:
Route::controller('admin/products', 'ProductsController');
Controller:
class ProductsController extends BaseController
{
public function getUpdate($id)
{
$product = Product::find($id);
if ($product) {
$product->title = Input::get('title');
$product->save();
return Redirect::to('admin/products/index')->with('message', 'Product Updated');
}
return Redirect::to('admin/products/index')->with('message', 'Invalid Product');
}
..ECT...
I realise the controller is requesting an ID to use, but I cannot figure out how to pass it a product ID when the form is posted/get.
Form:
{{Form::open(array("url"=>"admin/products/update",'method' => 'get', 'files'=>true))}}
<ul>
<li>
{{ Form::label('title', 'Title:') }}
{{ Form::text('title') }}
{{ Form::hidden('id', $product->id) }}
..ECT...
{{ Form::close() }}
my initial idea was to pass the product id within the form URL like:
{{Form::open(array("url"=>"admin/products/update/{{product->id}}", 'files'=>true))}}
But no luck with that either.
The error I get is:
Missing argument 1 for ProductsController::postUpdate()
Interestingly if I type directly into the URL:
http://localhost/laravel/public/admin/products/update/3
It works and the item with id 3 is altered fine.
So can anyone help and inform me how to pass the id with a form?
Thanks very much
The first Problem here ist the following:
{{Form::open(array("url"=>"admin/products/update/{{product->id}}", 'files'=>true))}}
the {{product->id}} is wrong in two ways:
it should be {{$product->id}}
BUT it wouldn't work anyway because the inner {{..}} inside of the {{Form::...}} won't be recognized since it is inside a string and therefore part of the string itself.
You either have to write it this way:
{{Form::open(array("url"=>"admin/products/update/".$product->id, 'files'=>true))}}
or you give your route a name in your routes.php file and do it this way:
{{Form::open(array('route' => array('route.name', $product->id, 'files'=>true)))}}
I prefer the second way.
You also might want to look into Form Model Bingin

Laravel 4 - Redirect with flash message not working

In my Laravel 4 project, I am trying to set a flash message when redirecting, like so:
return Redirect::route('main')->with('flash', 'Message here.');
(I looked at the following: Laravel 4 - Redirect::route with parameters).
The redirect happens properly. In my 'main' view I've got this code to display the flash message:
#if (Session::has('flash'))
<div class="flash alert">
<p>{{ Session::get('flash') }}</p>
</div>
#endif
But it doesn't work: Session::get('flash') returns an array, not a string. If I insert a call to var_dump(Session::get('flash')), I get this output:
array (size=2)
'new' =>
array (size=0)
empty
'old' =>
array (size=1)
0 => string 'flash' (length=5)
What is happening? It seems like fairly straightforward code...
When you redirect with data, the data you save will be 'flashed' into session (meaning it will only be available during the next request, see: http://laravel.com/docs/session#flash-data).
I would be wary of using 'flash' as a flash data key, as Laravel stores its flash data in a key already with a name of 'flash'. Try changing your name to something else. You may be confusing Laravel's Session::get as to where it should be attempting to load the data from.
Inside of the routes.php file try to create your routes within the
Route::group(['middleware' => ['web']], function () {
Route::get('/', [
'as' => 'home',
'uses' => 'PagesController#home'
]);
Route::resource('tasks', 'TasksController');
});
And After that use.
#if (Session::has('flash'))
<div class="flash alert">
<p>{{ Session::get('flash') }}</p>
</div>
#endif
Where you need to access session

Laravel4: delete a comment

I have a simple blog with Post resource and Comment nested resource.
Until now I can see all the comments belonging to a post and create a new comment for a post.
I want to give the possibility to delete a specific comment, but somehow I am making some mistakes.
This is the view comments.index with all the comments:
#extends('master')
#section('blog')
#foreach($comments as $comment)
<div class="span11 well">
<ul>
<li><strong>Body: </strong> {{ $comment->body }} </li>
<li><strong>Author: </strong> {{ $comment->author }}</li>
</ul>
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $post_id), $comment->id)) }}
{{ Form::submit('Delete', array('class' => 'btn btn-danger')) }}
{{ Form::close() }}
</div>
#endforeach
{{ link_to_route('posts.index', 'Back to Post index') }}
This is the error i get running the index: Parameter "comments" for route "posts.comments.destroy" must match "[^/]++" ("" given) to generate a corresponding URL.
This is the Index method inside CommentsController:
public function index($post_id)
{
$comments = Post::find($post_id)->comments;
return View::make('comments.index', compact('comments'))->with('post_id', $post_id);
}
And this is the Destroy method inside CommentsController:
public function destroy($post_id, $comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::route('posts.comments.index', $post_id);
}
Someone can tell me please where I am making the mistake?
This the routes:
Route::resource('posts', 'PostsController');
Route::resource('posts.comments', 'CommentsController');
You have put a regexp tester on your route, to check your comments parameter.
This error message says that parameter that you give to Laravel isn't good.
If your parameter is only a decimal id, use \d+ regexp instead.
Without your routes.php file - I cant be sure, but I think this might be the problem.
Change
{{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', $post_id), $comment->id)) }
to
{{ Form::open(array('method' => 'DELETE', 'route' => array('post.comments.destroy', array ($post_id, $comment->id))) }
If this does not work - please post your routes.php file.
Edit: You have defined your route as a "resource". This means your destroy route is defined with only one variable. You dont actually need the $post included, so just define this:
{{ Form::open(array('method' => 'DELETE', 'route' => array('posts.comments.destroy', $comment->id))) }}
and change your destroy method to this - there is no need for the $post to delete a $comment:
public function destroy($comment_id)
{
$comment = $this->comment->find($comment_id)->delete();
return Redirect::back();
}

Categories