Laravel Controller
my insert data method in controller
$category = array(
'name' => $request->name,
'code' => $request->code,
'image' => $image_name
);
Category::create($category,$image_name);
return Redirect::to('/category')->with('success','Record
inserted successfully');
laravel view
index view where I want to display message
#if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
#endif
Route.php
Route::resource('/category', 'CategoryController');
Try something like this:
<div class="panel panel-default">
#if (session('success'))
<div class="alert alert-success">{{ session('success') }}</div>
#endif
</div>
try:
#if(session()->has('success'))
<div class="alert alert-dismissable alert success" style="background: white;">
<button type="button" class="close" data-dismiss="alert" aria-label="close">
<span aria-hidden="true">×</span>
</button>
<strong>
{!! session()->get('success') !!}
</strong>
</div>
#endif
Try this, is should work.
Route
Route::get('category', 'CategoryController#index')->name('category');
$category = array(
'name' => $request->name,
'code' => $request->code,
'image' => $image_name
);
Category::create($category, $image_name);
return redirect()->route('category')->with('success', 'Record inserted successfully');
Success Message View
#if($message = Session::get('success'))
<div class="alert alert-success">
<p>{{$message}}</p>
</div>
#endif
Hi every one and thanks alot for replying me I got solution to my stated problem from below link
Laravel 5.2 Validation Error not appearing in blade
Related
Hi I am New to Laravel I have done Upload a Image to path
img/banner/{{image upload here}}
My doubt is here is to save the image path in database with which
user uploaded
And while retrieving data how to give my URL path here.
I have created a database name uploads
id user_id group_id filename extension filesize location created_at updated_at
Blade File
<div class="panel panel-primary">
<div class="panel-heading"><h2>Slide Image Upload</h2></div>
<div class="panel-body">
#if ($message = Session::get('success'))
<div class="alert alert-success alert-block">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>{{ $message }}</strong>
</div>
<img src="/img/banner/{{ Session::get('image') }}">
#endif
#if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
<form action="{{ route('image.upload.post') }}" method="POST" enctype="multipart/form-data">
#csrf
<div class="row">
<div class="col-md-6">
<input type="file" name="image" class="form-control">
</div>
<div class="col-md-6">
<button type="submit" class="btn btn-success">Upload</button>
</div>
</div>
</form>
</div>
</div>
Controller:
public function imageUploadPost()
{
request()->validate([
'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
]);
$imageName = time().'.'.request()->image->getClientOriginalExtension();
request()->image->move(public_path('img/banner'), $imageName);
return back()
->with('success','You have successfully upload image.')
->with('image',$imageName);
}
Routes:
Route::post('/imageupload', 'Admin\ImageController#imageUploadPost')->name('image.upload.post');
Any Help will be appreciated.
I am expecting that you have included namespace, if not
use DB;
$full_path = public_path('img/banner');
$ext = request()->image->getClientOriginalExtension();
$size = request()->image->getSize();
DB::table('uploads')->insert(
['group_id' => '1', user_id' => '1', 'filename' => $imageName,'filesize'=>$size ,'extension'=> $ext,'location' => $full_path,'created_at'=> date('Y-m-d H:m:s')]
);
You can get by this your database values, $imagename is the name you used to upload the image, not sure about your group_id so set to 1.
latest Edit
this is how you can get imageName:
$imageName = time().'.'.request()->image->getClientOriginalExtension();
froom your code only
I'm doing a error exception where display a modal popup when fail to delete an entry due to parent constrain
Controller :
public function deleteUnitType(Request $request, $proj_id)
{
$id = $request->id;
$unit_types = UnitType::where('id', $id);
$floors = UnitTypeFloor::where('unit_type_id', $id)->get();
if(count($floors) != 0){
return redirect()->route('dev-admin.projects.unit-types.index', ['unit_types' => $unit_types, 'proj_id' => $proj_id, 'floors' => $floors])->with('failed', 'Unit Type failed to delete due to existing floor plan.');
} else {
// $unit_types->delete();
return redirect()->route('dev-admin.projects.unit-types.index', ['unit_types' => $unit_types, 'proj_id' => $proj_id])->with('status', 'Unit Type is successfully deleted.');
}
}
HTML :
<div class="col-12">
#if (session('status'))
<div class="alert alert-success alert-dismissible fade show" role="alert">
{{ session('status') }}
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
#elseif (session('failed'))
<div class="modal fade" id="unit-type-notification-modal" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<p>{{ session('failed') }}</p>
<ul>
#foreach($floors as $floor)
<li>{{ $floor -> name }}</li>
#endforeach
</ul>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
#endif
</div>
$( document ).ready(function() {
#if (session('failed'))
$('#unit-type-notification-modal').modal('show');
#endif
});
The modal popup and everything but for some reason it cannot find my $floor variable, it says :
Undefined variable: floors
but when I dd it on my controller, the data exist
I think you should check the first session has 'failed' following way.
#if(Session::has('status'))
#elseif( Session::has( 'failed' ) )
And your script:
$( document ).ready(function() {
#if(Session::has('failed'))
$('#unit-type-notification-modal').modal('show');
#endif
});
Thanks
You can add another with to your redirect route
return redirect()->route('dev-admin.projects.unit-types.index', ['unit_types' => $unit_types, 'proj_id' => $proj_id, 'floors' => $floors])
->with('failed', 'Unit Type failed to delete due to existing floor plan.')
->with('floors', $floors);
Or this;
return redirect()->route('dev-admin.projects.unit-types.index', ['unit_types' => $unit_types, 'proj_id' => $proj_id, 'floors' => $floors])
->with(['failed' => 'Unit Type failed to delete due to existing floor plan.', 'floors' => $floors]);
O. I am in need of help with this problem. I have a contact form within a blade.php file, I have a route set up in my web.php file and I have a controller set up which is routed from the web.php file and is to perform validation on the fields and display a flash message on the page when the form is submitted. Right now the form is properly being submitted to my database so it is working but if I submit with a blank form, the validation is not working as it should (laravel) and also the flash message does not show upon successful form submission:
CODE:
Web.php
<?php
Route::get('/', 'HomeController#index')->name('home');
Route::post('/contact/submit','MessagesController#submit');
MessagesController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Message;
class MessagesController extends Controller
{
public function submit(Request $request)
{
$validatedData = $request->validate([
'name' => 'required|min:2',
'email' => 'required|max:255',
'phonenumber' => 'required|min:10|max:10',
'message' => 'required|min:5',
]);
Message::create($validatedData);
return redirect('/')->with('success', 'Message has been sent');
}
}
contact.blade.php
{{--CONTACT FORM--}}
<section id="contact">
<div class="container-fluid padding">
<div class="row text-center padding">
<div class="col-12">
<h2>Contact PDMA</h2>
</div>
<div class="col-12 padding">
{!! Form::open(['url' => 'contact/submit']) !!}
#csrf
<div class="form-group">
{{Form::label("name", 'Name')}}
{{Form::text('name', '', ['class' => 'form-control', 'placeholder' => 'Enter name'])}}
</div>
<div class="form-group">
{{Form::label("email", 'E-Mail Address')}}
{{Form::text('email', '', ['class' => 'form-control', 'placeholder' => 'Enter email'])}}
</div>
<div class="form-group">
{{Form::label("phonenumber", 'Phone Number')}}
{{Form::text('phonenumber', '', ['class' => 'form-control', 'placeholder' => 'Enter phone number'])}}
</div>
<div class="form-group">
{{Form::label("message", 'Message')}}
{{Form::textarea('message', '', ['class' => 'form-control', 'placeholder' => 'Enter message'])}}
</div>
<div>
{{Form::submit('Submit Form', ['class' => 'btn btn-success'])}}
</div>
{!! Form::close() !!}
</div>
</div>
</div>
</section>
Just use Session:
First Import Session class in your controller
use Session;
Message::create($validatedData);
Session::flash('success', 'Message has been sent');
return redirect('/')
Then Create a blade file in view folder, you can call it whatever you want, e.g: notify.blade.php
#if (Session::has('success'))
<div class="alert alert-success" role="alert" style="bottom:10px; position: fixed; left:2%; z-index:100">
×
<h4 class="alert-heading">Well done!</h4>
<p>{{ Session::get('success') }}</p>
</div>
#endif
#if (Session::has('danger'))
<div class="alert alert-danger" role="alert" style="bottom:10px; position: fixed; left:2%; z-index:100">
×
<h4 class="alert-heading">Error!</h4>
<p>{{ Session::get('danger') }}</p>
</div>
#endif
Finaly, include this file in any view.
include('notify')
As liverson suggested create the blade file for the session
And the other thing you can also is catch the error and change the input style using another blade something like error.blade.php and include it in your form
#if($errors->any())
<div class="alert alert-danger" role="alert">
<ul>
#foreach($errors->all() as $error)
<li>{{$error}}</li>
#endforeach
</ul>
</div>
#endif
For the Form you can add {{$errors->has('name') ? 'is-danger' : ''}} to your div class
For Example
<div class="form-row text-left">
<label for="name" class="col-md-3">Name</label>
<div class="col-md-9">
<input type="text" name="name" class="input {{$errors->has('name') ? 'is-danger' : ''}}" required
value= #if(isset($user))"{{$user->name}}"#else "{{old('name')}}"#endif>
</div>
</div>
https://laracasts.com/series/laravel-from-scratch-2018/episodes/15
I'd like to know why my bootstrap success message that's coming from controller (PostsController.php) won't appear in the view (messages.blade.php)?
I see the Bootsrap success green banner coming about but not the message itself. What could be the reason why?
Here's messages.blade.php:
#if(count($errors) > 0)
#foreach($errors->all() as $error)
<div class="alert alert-danger">
{{$error}}
</div>
#endforeach
#endif
#if(session('success'))
<div class="alert alert-success">
{{session('session')}}
</div>
#endif
#if(session('error'))
<div class="alert alert-danger">
{{session('error')}}
</div>
#endif
Here's PostController.php:
public function store(Request $request)
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
$post = new Post;
$post->title = $request->input('title');
$post->body = $request->input('body');
$post->save();
return redirect('/posts')->with('success', 'Post Created');
}
#if(session('success'))
...
{{session('session')}}
...
#endif
I think you want session('success') inside the div?
I am working with Laravel and I am new. I jsut set a flash message by using this line of code: session()->flash('status', 'This is my flash message to display');
To retrieve the message I use session('status').
Now my question is, is there any possibility to get the key of the flash message? In my example, the key of the flash message is status
Set an array of data in session with type and message.
session()->flash('message', [
'type' => 'success',
'body' => 'This is my flash message to display'
]);
Then you can access the message type like
session('message.type')
In your blade view you can do this to have a dynamic alert message
#if (session()->has('message'))
<div class="alert alert-{{ session('message.type') }}">
{{ session('message.body') }}
</div>
#endif
You can get an array of all keys of newly flashed values using:
session('_flash.new');
Pass message like this
return redirect()->back()->with('success', 'Destination deleted successfully');
Use like this
#if(Session::has('success'))
<div class="alert alert-success alert-dismissable alert-box">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ Session::get('success') }}
</div>
#endif
#if(Session::has('error'))
<div class="alert alert-danger alert-dismissable alert-box">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button>
{{ Session::get('error') }}
</div>
#endif