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
Related
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')}}
I have laravel application! I use original laravel authentication from
php artisan make:auth
My question is how to check if name already exist in database and if exist to return error message!
My user table structure is:-
user:
id - UNIQUE
name
email
password
remembertoken
timestamps
Laravel form validation
Following is quoted from there:
Use the "unique" rule for name.
$request->validate([
'name' => 'required|unique:users'
]);
And display error like this:
#if ($errors->any())
<div class="alert alert-danger">
<ul>
#foreach ($errors->all() as $error)
<li>{{ $error }}</li>
#endforeach
</ul>
</div> #endif
You actually have two solutions:
Make sure you have an unique constraint on your database for name
See the code below
$user = User::where("name", $nameToTestAgainst)->first();
if($user!= null) {
$v->errors()->add('Duplicate', 'Duplicate user found!');
return redirect('to-your-view')
->withErrors($v)
->withInput();
}
You can use auth()->user()->name in blade file.
<?php $name=auth()->user()->name; ?>
on signup i am checking the invitation code the user uses using:
$match_code = DB::table('codes')->where('code', $code)->pluck('code');
if($code == $match_code){
$ip->save();
$user->save();
Auth::login($user);
return redirect()->route('news');
}else{
return redirect()->route('home')->with('message','Invalid Invite Code');
}
i tried adding this in my view :
#if(isset($message))
<li>{{ $message }}</li>
#endif
but this does not display anything, i am new to laravel. i know this is basics but i have been googling for over 45 mins with no results
The data you have send using with() is available in session, check this:
return redirect('dashboard')->with('status', 'Profile updated!');
After the user is redirected, you may display the flashed message from the session. For example, using Blade syntax:
#if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
#endif
Reference
we are trying to get the value sent with the redirect method in blade template in laravel.
Here is how my return statement looks like:
public function movetotrash($id){
$page = Pages::where('id', $id) -> first();
$page -> active = 0;
//$page -> save();
//return redirect()->route('pages.index')->with('trash','Page Moved To Trash');
return redirect('pages')->with('trash','Page Moved To Trash');
}
Now once its redirect to pages it creates are URL like this http://localhost:8888/laravelCRM/public/pages.
Then i want to print the message to user that "Page is moved to trash" with $trash variable. For which i used this :
#if(isset($trash) ))
<div class="alert alert-error">
{{ $trash }}<br><br>
</div>
#endif
What should i do to print the value?
Thank you! (in advance)
The with method flashes data to the session which is only accessible in the next request.
So you can retrieve it from the session like this:
#if(session()->has('trash'))
<div class="alert alert-error">
{{ session()->get('trash') }}
</div>
#endif
In redirect with value pass as session flash,so you need to check value like that
#if(session()->has('trash'))
then you can show like this
{{ session('trash') }}
You can send data from controller like this,
You have to assign the message into variable
$trash = Page Moved To Trash';
return redirect('pages')->with('trash');
and then in view,
use like this.
#if(session($trash) ))
<div class="alert alert-error">
{{ session($trash) }}<br><br>
</div>
#endif
The data will store in session and we can use it on redirect page
Hope this will help :)
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'; });