I'm just confused about my code. But I really thought that my code is correct. I'm trying to use with() method in Laravel 5.1 and then return to a view, then the sweet alert appears if the session that has been set is exists. Please see my code below:
PageController.php
return redirect()->route('list.view')->with('sweetalert', 'List has been created!');
view.blade.php
#extends('layout.master')
#section('container')
#foreach($lists as $list)
<li>{{ $list->name }}</li>
#endforeach
#stop
master.blade.php
<div class="container">
// some markup here...
</div>
#if(Session::has('sweetalert'))
<script>
swal('Success!', '{{ Session::get('sweetalert') }}', 'success');
</script>
#endif
I only want it to appear once, but if I try to click the back button, the message appears again. I have also tried the ff. code but nothings change:
#if(Session::has('sweet'))
<script>
swal('Success!', '{{ Session::get('sweetalert') }}', 'success');
</script>
<?php Session::forget('sweetalert'); ?>
#endif
Little help here?
a flash message has to be trigerred otherwise it will not make sense as you will set it everytime for the view
however you can use this code wherever the trigger is
Please Note :- I am just trigerring it everytime
Route::get('/', function () {
session()->flash('testing', 'I see this'); // Please have this line inside the trigger so the session does not get created everytime the view is called
return view('welcome');
});
Hope this helps
Related
I'm new to Laravel, I'm trying to follow along with this tutorial
https://laravel.com/docs/7.x/blade#displaying-data
I want to assign a value to a variable and then display it in the view but I don't know where I should do it. I tried to add this snippet
Route::get('greeting', function () {
return view('welcome', ['name' => 'Samantha']);
});
to the web.php file and then display it in the view like this hello, {{name}} but I either get an error or just a plain text:
hello, {{name}}
I'm reading the documentation but I can't figure out where or how to assign values to variables and then display it.
My view named welcome.blade.php has this:
<h1>Example</h1>
Hello, {$name}
In my web.php file I have this:
<?php
use Illuminate\Support\Facades\Route;
Route::get('greeting', function () {
return view('welcome', ['name' => 'Samantha']);
});
It doesn't give me any error. Just shows Hello {$name} instead of Samantha
you are missing the $ sign in your blade.
hello, {{ $name }}
I will show an example, I'm not good at writing English and it's better for the comprehension.
Route::get('/', function () {
return view('home')->withSucces("#lang('home.account_create_confirm')");
});
Here is a route and here is the code from the page:
<div class="container">
<div class="alert alert-success">
{{ $success }}
</div>
</div>
The message "home.account_create_confirm" is "your account has been created", but when I go to the page, instead of this, the page display this:
#lang('home.account_create_confirm')
Screenshot to understand:
https://gyazo.com/623fd5899b95819b6196bbae0197b1d4
I am sorry for this, I know that I'm a beginner and it must be evident for must of you!
Thanks for the help!
#lang is a tag of blade template processor and is not available in your controller. use trans() instead:
Route::get('/', function () {
return view('home')->withSucces(trans('home.account_create_confirm'));
});
I'm creating a nested list with data from database. On this list, I'm using the JQuery UI Drag effect. What I need to do is when the drag is over, it will update the database.
The list coints Professor's name and his ID, it has as sub-list with his classes, like:
John teaches Math & physics, Dwayne teaches English.
John
*Physics
*Math
Dwayne
*English
Let's say I want to give Math class to Dwayne. So I'll drag Math from John and drop it on Dwayne sub-list. It's working fine.
What I can't do is to make the update on database, because John no longer teaches Math instead John is going to teach it. So I need to make an update there or a delete+insert.
Obs: I'm using laravel
Here is my Code:
#extends('app')
#section('assets')
<script type="text/javascript" src="{{ URL::to('/js/jquery.mjs.nestedSortable.js') }}"></script>
#stop
#section('content')
<ol>
#foreach($professores as $prof)
<li data-id=" {{ $prof->id }}">
{{ $prof->nome }}
<ol class="list-disc">
#foreach($prof->disc as $disc)
<li data-id="{{ $disc->id }}">{{ $disc->nome }}</li>
#endforeach
</ol>
</li>
#endforeach
</ol>
<script type="text/javascript">
$(function(){
var old_teacher;
$('.list-disc').sortable({
connectWith: '.list-disc',
start: function (event, ui){
old_teacher = ui.item.parent().parent().attr('data-id');
},
stop: function (event, ui){
$.ajax({
type: "POST",
url: '{{ URL::to("/professor") }}',
data: {disc: ui.item.attr('data-id'), professor: ui.item.parent().parent().attr('data-id'), old: old_teacher},
success: function(data){
console.log(data);
}
});
}
});
})
</script>
#stop
With this currently code, When I drop the item I get:
Internal Server Error (500)
UPDATE
Route File:
Route::post('professor', [
'uses' => 'ProfessorController#postProfessorList'
]);
Controller File:
public function postProfessorList()
{
Professor::submit(Input::post('disciplina'), input::post('professor'), input::post('old'));
}
Log file: Update
local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException' with message 'Call to undefined method Illuminate\Http\Request::post()' in F:\PathToProject\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:210
Internal Server Error (500) means that something is wrong with your server-side (laravel) code.
Can you provide us with the code which is used in POST: /professor ?
EDIT
You might want to check your logs in app/storage or storage/ (depending on the laravel version). They should give you a better description of the error that occurs.
Also, you should replace Input::post('...') with Input::get('...') laravel takes care of $_GET and $_POST variables automatically.
EDIT 2
The error you get is due to Laravels CSRF protection.
You will need to set the csrf token in your ajax request like that:
data: {disc: ....., _token: '{{csrf_token()}}' }
I'm creating a website with laravel and when a user edits their details it will access the controller update them and redirect to the 'edit' page using this
return Redirect::to('/member/editprofile')->with('message', 'Information Changed');
this part works fine, it redirects and sends the message which is printed out on the page with this
{{ Session::get('message') }}
I was wondering if there was a way to add a class to the session? I'm probably missing something completely obvious here and this is what I tried...
{{ Session::get('message', array("class" => "success")) }}
//added the class as you would with a HTML::link
any help is appreciated, thanks in advance!
You want this
<div class="success">{{ Session::get('message') }}</div>
In Laravel-4, the Session::get accepts two arguments :
$value = Session::get('key', 'default');
$value = Session::get('key', function() { return 'default'; });
I am building a practice app with the Laravel framework I built a form in one of the views which is set to post to the same view itself but when I hit submit the form is posted however I do not get the desired output, I see the original view again.
Here is my view index.blade.php
#extends('master')
#section('container')
<div class="wrapper">
{{ Form::open(array('url' => '/', 'method' => 'post')) }}
{{ Form::text('url') }}
{{ Form::text('valid') }}
{{ Form::submit('shorten') }}
{{ Form::close() }}
</div><!-- /wrapper -->
#stop
and my routes.php
Route::get('/', function()
{
return View::make('index');
});
Route::post('/', function()
{
return 'successfull';
});
What I've tried so far
I tried changing the post to a different view and it worked.
However I want the form to post to the same view itself.
Instead of returning a string I tried to return make a view still it
didn't work.
What am I doing wrong?
addendum
I see that when the form is making the post request I am getting a 301 MOVED PERMANENTLY HEADER
{{ Form::open(array('url' => ' ', 'method' => 'post')) }}
Passing a space as the url has worked for me.
I think this post: Form submits as GET Laravel 4 is related to your problem. I think the problem as I undersood it is caused by end a form url with a / . I found this when having problems to using post to a ./ url in my form. There is also a bug at github that seems like it is related https://github.com/laravel/framework/issues/1804.
I know this is an old question but I found this thread having the same problem so hopefully someone else is helped by my answer.
You need to make sure that your form's method does NOT end in a / for it to be routed correctly. For example if you have the following route:
Route::post('form/process', function()
{
# code here ...
});
Then you need to have the following form definition:
<form action="/form/process" method="POST">
I hope that helps.
I have same problem with OSx + MAMP, initially I've resolved with Raul's solution:
{{ Form::open(array('url' => ' ', 'method' => 'post')) }}
but after consultation with my friend we have concluded that my problem was due to the fact
my lavarel project is avaliable by long local path, as:
http://localhost/custom/custom2/...
in this location the post/get method on root path ("/") not working correctly.
Lavarel to working correctly must be avaliable by "vhost", in this case the problem get/post method on root location "/" not exist.
My friend advised me to use http://www.vagrantup.com/
BYE
There is some helpfull information in the Laravel Docs. Check these out:
Resource Controllers (or RESTful Controllers)
Forms & HTML
Opening A Form
Routing
Named Routes
I recommend you read the Resource Controllers documentation as it makes form handling a lot easier.
Well, you just return the view, so nothing change. You should bind your route to a controller to do some logic and add data to your view, like this:
index.blade.php
#extends('master')
#section('container')
<div class="wrapper">
#if (isset($message))
<p>{{$message}}</p>
#endif
{{ Form::open(array('url' => '/', 'method' => 'post')) }}
{{ Form::text('url') }}
{{ Form::text('valid') }}
{{ Form::submit('shorten') }}
{{ Form::close() }}
</div><!-- /wrapper -->
#stop
Your routes
Routes::any('/', 'home#index');
You controller HomeController.php
public function index()
{
$data = array();
$url = Input::get('url');
if ($url)
$data['message'] = "foo";
return View::make('index', $data);
}
You can also modify your current routes without using a controller like this (use the new view file)
Route::get('/', function()
{
return View::make('index');
});
Route::post('/', function()
{
return View::make('index')->with('message', 'Foo');
});
The problem is with defualt slashed in Apache from 2.0.51 and heigher:
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash
The best solution if you do not want to change Apache config is to make the post in a different path:
GOOD:
Route::get('/',
['as' => 'wizard', 'uses' => 'WizardController#create']);
Route::post('wizard-post',
['as' => 'wizard_store', 'uses' => 'WizardController#store']);
NOT GOOD:
Route::get('/',
['as' => 'wizard', 'uses' => 'WizardController#create']);
Route::post('/',
['as' => 'wizard_store', 'uses' => 'WizardController#store']);