I want to get a pop-up notification when new value in inserted in my mysql database table using Laravel - laravel-5

I want to show a successful message as notification in my home page when a new value is inserted into my database.
Controller
function insert(Request $request)
{
$teacher = new teacher;
$teacher->Teacher_name = $request->Teacher_name;
$teacher->School_name = $request->School_name;
$teacher->Date_of_Birth = $request->Date_of_Birth;
$teacher->save();
$teacher = teacher::find($request->id);
return back()->with('success', 'Data inserted Successfully');
}
View
<ul class="nav navbar-nav navbar-left">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown"><span
class="label label-pill label-danger count" style="border-
radius:10px;"></span> <span class="glyphicon glyphicon-bell"
style="font-size:18px;"></span></a>
<ul class="dropdown-menu">
</ul>
</li>
</ul>

In Laravel, you can show success or failure alerts using Flash as below,
Controller:
\Session::flash('success', 'Data inserted Successfully.');
Blade file:
#if ($message = \Session::get('success'))
<div class="alert success-msg">
{{ $message }}
</div>
#endif
UPDATE:
To keep message in blade until closed;
#if (session()->has('success'))
<div class="alert alert-dismissable alert-success">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<strong>
{!! session()->get('success') !!}
</strong>
</div>
#endif
UPDATE:2
To get real time notification, you can use Pusher and implementation guide for the same is: https://www.sitepoint.com/add-real-time-notifications-laravel-pusher/
Hope this helps.

Use Flash,
Controller:
\Session::flash('success', 'Data inserted Successfully.');
Blade file:
#if ($message = \Session::get('success'))
<div class="modal hide fade" id="myModal">
<div class="modal-header">
<a class="close" data-dismiss="modal">×</a>
<h3>Modal header</h3>
</div>
<div class="modal-body">
<p>{{$message}}</p>
</div>
<div class="modal-footer">
Close
</div>
</div>
<script> $('#myModal').modal('show');</script>
#endif
Post this after you Load jquery and bootstrap.

Related

How to keep the modal open if the validation is failed in Laravel 8

I am trying to show the modal with the errors after the failure of the validation directly.
My code now shows the modal with the errors only if I clicked the button that shows it (to add a new user).
I have found some related answers but they did not work for me.
My controller
function add(Request $req){
$validated = $req->validate([
'name' => 'required|regex:/^[\pL\s\-]+$/u',
'email' => 'email|unique:users,email',
'password' => 'required|min:8',
]);
modal.blade
<form action="addUser" method="post" enctype="multipart/form-data">
#csrf
<div class="modal fade text-left" id="ModalCreate" tabindex="-1" role="dialog" aria-hidden="true" >
<div class="modal-dialog modal-lg" role="document" >
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title">{{__('Add New User')}} </h4>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div>
#endif
...
</div>
</div>
</div>
</div>
</form>
The page that calls the modal
#extends('layouts.admin')
#section('content')
<div class="container">
<div class="float-right">
<a href="#" data-toggle="modal" data-target="#ModalCreate">
<button type="button" class="btn btn-dark"><i class="bi bi-person-plus"></i></button>
</a>
</div><br><br>
<table class="table">
...
</table>
</div>
#include('admin.Users.addUser')
#endsection

Laravel Error: Too few arguments to function, 0 passed and exactly 1 expected

Using Laravel-5.8, I have this code:
public function manager_employee_list(Request $request)
{
$employees = HrEmployee::paginate(6);
return view('appraisal.appraisal_goals.manager_employee_list')->with('employees', $employees);
}
And it render this view: manager_employee_list
<div class="row d-flex align-items-stretch">
#if (count($employees))
#foreach($employees as $key => $employee)
<div class="col-12 col-sm-6 col-md-4 d-flex align-items-stretch">
<div class="card bg-light">
<div class="card-header text-muted border-bottom-0">
{{isset($employee->designation) ? $employee->designation->designation_name : ''}}
</div>
<div class="card-body pt-0">
<div class="row">
<div class="col-7">
<h2 class="lead"><b>Staff ID: {{$employee->employee_code}}</b></h2>
<h2 class="lead"><b>{{$employee->first_name}} {{$employee->last_name}}</b></h2>
<h6 class="lead"><b>Employee Department: </b>{{isset($employee->department) ? $employee->department->dept_name : ''}}</h6>
</div>
<div class="col-5 text-center">
#if($employee->emp_image != '')
<img src="{{ URL::to('/') }}/public/storage/employees/image/{{ $employee->emp_image }}" class="img-circle img-fluid" />
#else
<img class="profile-user-img img-fluid img-circle" src="{{asset('theme/adminlte3/dist/img/default.png')}}" alt="" class="img-circle img-fluid">
#endif
</div>
</div>
</div>
<div class="card-footer">
<div class="text-right">
<a href="{{ route('appraisal.appraisal_goals.manager_employee_goal', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Goal
</a>
</div>
</div>
</div>
</div>
#endforeach
#else
<h4 style="text-align:center;">No matching records found</h4>
#endif
</div>
I want to pass the parameter
['id'=>$employee->id]
from above to another controller action:
public function manager_employee_goal($id)
{
$goals = AppraisalGoal::where('employee_id', $id)->get();
return view('appraisal.appraisal_goals.manager_employee_goal')->with('goals', $goals);
}
It utilizes it here:
$goals = AppraisalGoal::where('employee_id', $id)->get();
When I clicked on
<a href="{{ route('appraisal.appraisal_goals.manager_employee_goal', ['id'=>$employee->id]) }}" class="btn btn-sm btn-primary">
<i class="fas fa-user"></i> View Goal
</a>
I got this error:
Too few arguments to function App\Http\Controllers\Appraisal\AppraisalGoalsController::manager_employee_goal(), 0 passed and exactly 1 expected
and this is underlined:
public function manager_employee_goal($id)
route/web.php
Route::get('appraisal_goals/manager_employee_list', 'Appraisal\AppraisalGoalsController#manager_employee_list')->name('appraisal.appraisal_goals.manager_employee_list');
Route::get('appraisal_goals/manager_employee_goal', 'Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');
How do I resolve it?
Thank you.
You need to change in your web.php file. add {id} in URL.
otherwise, all are fine as now your get method will be like 'appraisal_goals/manager_employee_goal/{id}'
Route::get('appraisal_goals/manager_employee_goal/{id}','Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');
I believe your route need to be updated to
Route::get('appraisal_goals/manager_employee_goal/{id}','Appraisal\AppraisalGoalsController#manager_employee_goal')->name('appraisal.appraisal_goals.manager_employee_goal');

Laravel validation on array of radio inputs

I'm developing a module that have multiple radio inputs and has numeric index,
I have 15 radio input groups and each group has 7 radio input members with the same index.
I want to check each input if empty.
HTML output
I have tried dynamic requests validation on laravel it failed to validate.
I tried to hard code the validation of 15 inputs = failed,
Then I tried to test it individually and adding next index on success and it only validates up to 12th item/index.
my valdation code
$rules = [];
foreach($this->request->get('ratingsd') as $key => $val)
{
$rules["ratings[$val]"] = 'required';
}
<div class="card mb-3 bg-info">
<ul class="list-group list-group-flush bg-light">
#foreach($items as $index=>$group)
<li class="list-inline">
<h5 class="card-header text-white bg-info">
{{$group->group_name}}
</h5>
</li>
#foreach($group->items as $items=>$item)
<li class="list-group-item">
<span class="font-weight-bold">
{{ $item->number }} {{ $item->entry }}
</span>
<div class="float-md-right form-inline mt-2">
#foreach($ratings as $rate_value=>$rate_text)
{!!Form::hidden("ratingsd[".$item->item_id."]", $item->item_id)!!}
{!!Form::radio("ratings[$item->item_id]", $rate_text, "$group->group_id*$item->item_id*$rate_value")->inline()!!}
#endforeach
{!!Form::textarea("remarks[".$item->item_id."]", '')->placeholder('Remarks')->inline()!!}
</div>
</li>
#endforeach
#endforeach
<li class="list-inline bg-info text-white">
<h5 class="card-header">
Overall Remarks
</h5>
</li>
<li class="list-inline-item p-1">
{!!Form::textarea("overall_remark", '')->placeholder('Remarks')->inline()!!}
</li>
<li class="list-inline-item text-center p-2">
<button class="btn btn-primary btn-block" type="submit">
Submit
</button>
</li>
</ul>
</div>
I expect to validate properly each "ratings" radio inputs

How do I get the second last record of a table in laravel

I want the second last record from my table and search already and
I tried this:
$news2 = News::orderBy('created_at', 'desc')->skip(1)->take(1)->get();
but nothing works.
I get this error:
Property [datum] does not exist on this collection instance. (View: C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)
If I want the last one everything works it perfectly.
$news1 = News::all()->last();
and my view is the same
<div class="col-12 col-md-6 news">
<div class="news-content">
<div class="date"><i class="far fa-calendar-alt"></i> {{ $news1->datum }}</div>
<h4 class="news-title">{{ $news1->newstitel }}</h4>
<p class="news-text">{{ $news1->newsbeschreibung }}</p>
<a href="#" class="btn btn-primary">
mehr lesen »
</a>
</div>
</div>
</div>
<div class="row no-gutters news-wrapper">
<div class="col-12 col-md-6 news-image">
<img class="img-fluid" src="https://picsum.photos/385/370/?random">
</div>
<div class="col-12 col-md-6 news">
<div class="news-content">
<div class="date"><i class="far fa-calendar-alt"></i>{{ $news2->datum }}</div>
<h4 class="news-title">{{ $news2->newstitel }}</h4>
<p class="news-text">{{ $news2->newsbeschreibung }}</p>
<a href="#" class="btn btn-primary">
mehr lesen »
</a>
</div>
</div>
</div>
try this
$news2 = News::orderBy('created_at', 'desc')->skip(1)->take(1)->first();
this return only one result if you need more you need used foreach inside blade
The error you are receiving is from the view, you are probably calling to a "datum" property for an object and one of them does not exist:
(View: C:\xampp\htdocs\j4ylara\resources\views\user\start.blade.php)
The query looks good to me
try to use getModel() instead of get() to get query result
Try it:
$news2 = News::orderBy('created_at', 'desc')->offset(1)->take(1)->first();
get() returns collection of News. You must use first()
I GET IIIIIIIIIIIT AHHHHHHHHHH :)
$row = News::count();
$newsid = $row -2;
$news1 = News::all()->last();
$news2 = News::orderBy('created_at', 'desc')->skip($newsid)->take($newsid)->first();
return view('user/start', compact('news1', 'news2', 'newsid'));

How do i make a modal accept data when button is clicked?

I have a bootstrap modal. i call it using the #data-target. Problem i have is I'd like to populate the modal with the details from item on a list of feedbacks which i fetch from the database.
I want it dynamic, so it shows data of any item clicked from the loop. I'm really new to js and dont know ho to handle it. here's my laravel code for the feedbacks:
<div class="body">
#if(count($feedbacks) > 0)
<ul class="basic-list">
#forelse($feedbacks as $feedback)
<li>{{ $feedback->subject }} <span class="pull-right label-info label">View</span></li>
#empty
<p class="text-danger">No Feedbacks Yet</p>
#endforelse
#endif
</ul>
</div>
and this is the modal:
<div class="modal fade" id="defaultModal" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="defaultModalLabel">{{ $feedback->subject }}</h4>
</div>
<div class="modal-body">{{ $feedback->content }}</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect">SAVE CHANGES</button>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
The modal shows right now, but doesn't pass data when clicked.
if you want to avoid multiple modals, and still get data from your database, perform an ajax call to retrieve the $feedback->content by its id
Remove the data-target and data-toggle attribute from your link
Add a class attribute and a unique id = {{$feedback->id}} to the link
<div class="body">
#if(count($feedbacks) > 0)
<ul class="basic-list">
#forelse($feedbacks as $feedback)
<li>{{ $feedback->subject }} <span class="pull-right label-info label">View</span></li>
#empty
<p class="text-danger">No Feedbacks Yet</p>
#endforelse
</ul
#endif
</div>
create a route that accepts the feedback id as a url parameter
Route::get('/get-feedback-content/{feedback}', 'YourController#getFeedbackContent');
Add this to YourController:
public function getFeedbackContent($feedback){
//this assumes you have a feedback model, change as appropriate
$fb = FeedbackModel::find($feedback);
//return an feedback object as an array
return response()->json(array('feedback'=> $fb), 200);
}
Using jQuery your script should look something like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
$(".feedback-open").click(function(){
id = $(this).attr('id');
getMessage(id);
})
function getMessage(id){
$.ajax({
type:'GET',
url:'/getmsg/'+id,
data:'_token = <?php echo csrf_token() ?>',
success:function(data){
$("#defaultModal .modal-title").html(data.feedback.subject);
$("#defaultModal .modal-body").html(data.feedback.content);
$('#defaultModal').modal('show');
}
});
}
});
</script>
Modal have to be unique for every data and you didn't use {{}} to echo data, Please have a look the following code:
<div class="body">
#if(count($feedbacks) > 0)
<ul class="basic-list">
#foreach($feedbacks as $key => $feedback)
<li>
{{ $feedback->subject }}
<a href="javascript:void(0);" data-toggle="modal" data-
target="#modal-{{$key}}"><span class="pull-right label-info label">View</span></a>
<div class="modal fade" id="modal-{{$key}}" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="defaultModalLabel">{{$feedback->subject}} </h4>
</div>
<div class="modal-body"> {{$feedback->content}}</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect">SAVE CHANGES</button>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
</li>
#endforeach
</ul>
#else
<p class="text-danger">No Feedbacks Yet</p>
#endif
I think you have not displayed both fields i.e. $feedback->subject and $feedback->content into modal
popup.
Please try below code to display the subject and content fields into model as per [Laravel]:
1) {{ $feedback->subject }}
2) {{ $feedback->content }}
<div class="body">
#if(count($feedbacks) > 0)
<ul class="basic-list">
#forelse($feedbacks as $feedback)
<li>{{ $feedback->subject }} <span class="pull-right label-info label">View</span></li>
<div class="modal fade" id="defaultModal_{{ $feedback->id }}" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h4 class="modal-title" id="defaultModalLabel">{{ $feedback->subject }}</h4>
</div>
<div class="modal-body">{{ $feedback->content }}</div>
<div class="modal-footer">
<button type="button" class="btn btn-link waves-effect">SAVE CHANGES</button>
<button type="button" class="btn btn-link waves-effect" data-dismiss="modal">CLOSE</button>
</div>
</div>
</div>
</div>
#empty
<p class="text-danger">No Feedbacks Yet</p>
#endforelse
#endif
</ul>
</div>
Hope this helps you.

Resources