If isset blade laravel is not working laravel 5 - php

i'am new in Laravel and i have the next code:
return redirect()->back()->with('erriorIMG','filetype not is supported');
in the blade file:
#if(isset($erriorIMG))
<div class="alert alert-danger" role="alert">
{{ $erriorIMG }}
</div>
#else
<h3>sadasd</h3>
#endif
but not working, when i use redirect()->back() i cant pass parameters?

it's a flash data so try this:
Session::get('erriorIMG')
reference:
http://laravel.com/docs/5.0/responses#redirects
http://laravel.com/docs/5.0/session#flash-data

Related

How to attach a message to download in Laravel 6?

Hello I would like to send a message with the download function from laravel.
What I'm currently using:
return response()->download($pathToFile, $fileName);
What I tried:
return response()->download($pathToFile, $fileName)->with('success','Download started!');
It doesn't work.
In my view:
#if(Session::has('message'))
<p class="alert alert-success">{{ Session::get('message') }}</p>
#endif

Laravel 5 Flash messages not working

My flash messages are not displaying on the view. I have tried post SO questions but didn't worked.
Here is my controller code:
use Session;
//other code
//my code to set flash message and redirect it
\Session::flash('message', $message);
return redirect('admin/groups/add');
My View code:
#if(Session::has('message'))
<div class="alert alert-danger" id="alert_danger">
{!!Session::get('message')!!}
</div>
#endif
I don't know where I am going wrong.
In your controller, add
return Redirect::back()->withErrors('Password is incorrect.');
In view, add
#if($errors->first())
<p class="alert alert-danger">{{$errors->first()}}</p>
#endif
in your controller :
use Session;
public function myfunction(){
return redirect('admin/groups/add')->with('message', $message);
}
in your blade view :
#if(session()->has('message'))
<div class="alert alert-danger" id="alert_danger">
{{{{session('message')}}}}
</div>
#endif

Trying to get property of non object Laravel 5.2

The error occurs when i print the messages for the logged in user sent by others to him. I did var_dump in my controller as well as in my view. I also did {{$threads->count()}} in my view and it showed 27(thread count). But when i access the view file it throws me the above mentioned error.
Here is my controller code
public function index()
{
$currentUserId = Auth::user()->id;
$threads = Thread::getAllLatest()->get();
return view('chatindex', compact('threads', 'currentUserId'));
}
I also printed the currentUserId in view and it also works fine
Here is my view code (where the error occurs)
#extends('layouts.app')
#section('content')
#if (Session::has('error_message'))
<div class="alert alert-danger" role="alert">
{!! Session::get('error_message') !!}
</div>
#endif
#if($threads->count() > 0)
#foreach($threads as $thread)
<?php $class = $thread->isUnread($currentUserId) ? 'alert-info' : ''; ?>
<div class="media alert {!!$class!!}">
<h4 class="media-heading">{!! link_to('messages/' . $thread->id, $thread->subject) !!}</h4>
<p>{!! $thread->latestMessage->body !!}</p>
<p><small><strong>Creator:</strong> {!! $thread->creator()->name !!}</small></p>
<p><small><strong>Participants:</strong> {!! $thread->participantsString(Auth::id()) !!}</small></p>
</div>
#endforeach
#else
<p>Sorry, no threads.</p>
#endif
#stop
It throws me the error Trying to get property of non-object (View: C:\xampp\htdocs\laravel-projects\user_prof\resources\views\chatindex.blade.php)
maybe in your foreach $thread is an array() so use it like one as
$thread['id'], $thread['subject']

Redirect back with input not working in Laravel

I have a route post called "postContact" and when the post is success I redirect to the same contact.blade.php where place the post:
<form action="{{ route('post_contact) }}"></form>
I want to see a msg if the post have success, but the Input::get('some data') not working to me.
this is my controller resume:
public function postContact($params) {
//if successful
$msg_params = array(
'msg_type' => 'success',
'msg_text' => 'my text to show',
);
return redirect()->back()->withInput($msg_params);
}
But in the contact.blade.php this not working:
#if(isset($msg_type))
<div class="alert">{{ $msg_text }}</div>
#endif
The variables not exits...
I don´t want use flash data here, because contact.blade is a module of another external app laravel and can't share sessions.
What is happening here?
Because doing a redirect, those variables are set in session. So you may try:
#if(Session::has('msg_type'))
<div class="alert">{{ Session::get('msg_text') }}</div>
#endif
If you don't want to use session variables, then you can use route parameters. You can get the parameters using the Request facade:
#if(Request::has('msg_type'))
<div class="alert">{{ Request::get('msg_text') }}</div>
#endif
If you're redirecting back to the page, Laravel will store the data in the Session. So you need to enter this to show the data:
#if(Session::has('msg_type'))
<div class="alert">
{{ Session::get('msg_text') }}
</div>
#endif
Hope this works!
Controller
return redirect()->back()->with('success', ['your message,here']);
Blade:
#if (\Session::has('success'))
<div class="alert alert-success">
<ul>
<li>{!! \Session::get('success') !!}</li>
</ul>
</div>
#endif

Laravel 5.2 redirect back with success message

I'm trying to get a success message back to my home page on laravel.
return redirect()->back()->withSuccess('IT WORKS!');
For some reason the variable $success doesn't get any value after running this code.
The code I'm using to display the succes message:
#if (!empty($success))
<h1>{{$success}}</h1>
#endif
I have added the home and newsletter page to the web middleware group in routes.php like this:
Route::group(['middleware' => 'web'], function () {
Route::auth();
Route::get('/', function () {
return view('home');
});
Route::post('/newsletter/subscribe','NewsletterController#subscribe');
});
Does anyone have any idea why this doesn't seem to work?
You should remove web middleware from routes.php. Adding web middleware manually causes session and request related problems in Laravel 5.2.27 and higher.
If it didn't help (still, keep routes.php without web middleware), you can try little bit different approach:
return redirect()->back()->with('message', 'IT WORKS!');
Displaying message if it exists:
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
you can use this :
return redirect()->back()->withSuccess('IT WORKS!');
and use this in your view :
#if(session('success'))
<h1>{{session('success')}}</h1>
#endif
Controller:
return redirect()->route('subscriptions.index')->withSuccess(['Success Message here!']);
Blade
#if (session()->has('success'))
<div class="alert alert-success">
#if(is_array(session('success')))
<ul>
#foreach (session('success') as $message)
<li>{{ $message }}</li>
#endforeach
</ul>
#else
{{ session('success') }}
#endif
</div>
#endif
You can always save this part as separate blade file and include it easily.
fore example:
<div class="row">
<div class="col-md-6">
#include('admin.system.success')
<div class="box box-widget">
You can simply use back() function to redirect no need to use redirect()->back() make sure you are using 5.2 or greater than 5.2 version.
You can replace your code to below code.
return back()->with('message', 'WORKS!');
In the view file replace below code.
#if(session()->has('message'))
<div class="alert alert-success">
{{ session()->get('message') }}
</div>
#endif
For more detail, you can read here
back() is just a helper function. It's doing the same thing as redirect()->back()
One way to do that is sending the message in the session like this:
Controller:
return redirect()->back()->with('success', 'IT WORKS!');
View:
#if (session()->has('success'))
<h1>{{ session('success') }}</h1>
#endif
And other way to do that is just creating the session and put the text in the view directly:
Controller:
return redirect()->back()->with('success', true);
View:
#if (session()->has('success'))
<h1>IT WORKS!</h1>
#endif
You can check the full documentation here: Redirecting With Flashed Session Data
I hope it is very helpful, regards.
All of the above are correct, but try this straight one-liner:
{{session()->has('message') ? session()->get('message') : ''}}
In Controller
return redirect()->route('company')->with('update', 'Content has been updated successfully!');
In view
#if (session('update'))
<div class="alert alert-success alert-dismissable custom-success-box" style="margin: 15px;">
×
<strong> {{ session('update') }} </strong>
</div>
#endif
You can use laravel MessageBag to add our own messages to existing messages.
To use MessageBag you need to use:
use Illuminate\Support\MessageBag;
In the controller:
MessageBag $message_bag
$message_bag->add('message', trans('auth.confirmation-success'));
return redirect('login')->withSuccess($message_bag);
Hope it will help some one.
Adi
in Controller:
`return redirect()->route('car.index')->withSuccess('Bein ajoute')`;
In view
#if(Session::get('success'))
<div class="alert alert-success">
{{session::get('success')}}
</div>
#endif

Categories