Add var in auth - laravel

I use stard auth from Laravel but I want to send var to auth's view. Actually I want to send title of website and keywords. In other controllers I can do that
return view('my.view')->with('title', 'My funny title');
How I can do that in Login, Register...

perhaps you should do something like this.
in your controller( you will find this controller in AuthenticatesUsers Traits located in Illuminate\Foundation\Auth folder.
$title= "my page title";
return view('my.view', compact('title'));
and in view, just use {{ $title }} where ever you cant to call that text. this should work.

Add this on baseController/Controller __construct() function
by this you will share the variable to every blade file.
$siteTitle = 'SiteTitle';
View::share($siteTitle);

Maybe try with this syntax as in documentation
return view('my.view', ['title' => 'My funny title']);
or
$data = [
'title' => 'My funny title',
...
];
return view('my.view', $data);
I remember having the similar issues while ago, though i cant remember how exactly i worked it out.

Related

Is it possible to include view to another blade file that requires an ID?

Ok, Seems it's possible. So here is my explanation.
A controller that requires ID
public function frameIndex($id){
abort_if(Gate::denies('itinerary_flight_access'), Response::HTTP_FORBIDDEN, '403 Forbidden');
$iFlights = ItineraryFlight::with(['booking', 'supplier', 'airline'])->where('booking_id', '=',$id)->get();
return view('admin.modalCore.frameView.Flight', compact('iFlights'));
}
Route
Route::get('iflight/view/{id}', 'IFlightController#frameIndex');
Then I tried to include this view into another view file as following
#include('admin.modalCore.frameView.i-flight', [$bookingCore->id])
But it's not working. Any solutions for this situation?
You pass the parameter like this
#include('admin.modalCore.frameView.i-flight', ['id' => $bookingCore->id])
#include('admin.modalCore.frameView.i-flight', ['iFlights' => App\Models\ItineraryFlight::with(['booking', 'supplier', 'airline'])->where('booking_id', $bookingCore->id)->get()])

How to pass id from (form) of view blade to route (web.php) :Route

I need to pass an id to the route (web.php) from the form. My application has comment section at opporunities/id (id in value) , Whenever non-Auth user submits comment , my app will ask login and redirects to /opportunities but i need /opportunities/id. In the form of comment i have submitted page id. I have setup my route as
Route::post('/opportunities', 'OpportunitiesController#postPost')->name('posts.post'); Now if i can pass that id to as /opportunities/id then after login user will automatically lands on that page. I have manually tested and attached id and it works. Do I need to use "use Illuminate\Http\Request;" to get form data to web.php (route)? to get request post id? All i need is to add id after Route:post('/opportunites/'). Any suggestion and help will be appropriated.
What I did was and figured out is that
action="{{route('opportunities',['returnvalue'=> $post['id']]) }}" I still got error #bipin answer but i passed it with as parameter and solved. Thanks bipin for suggestion though!
One solution could be, pass your post id inside the form
View Blade
{{ Form::hidden('post_id', 'value', array('id' => 'post_id')) }}
Controler
use Illuminate\Http\Request;
public function comment(Request $request)
{
//Validation
$this->validate($request, [
'post_id' => 'required'
]);
//Inside variable
$input = $request->all();
// OR
$request->post_id;
}

Laravel undefine variable in view

I'm new to laravel. Using version 5.4 and tried to search but don't see what I'm doing wrong. I keep getting an "Undefined variable: post" in my view. I'm also doing form model binding. Model binding works properly when manually entering URL. Just can't click on link to bring up edit view.
My routes:
Route::get('test/{id}/edit','TestController#edit');
My controller:
public function edit($id)
{
$post = Post::find($id);
if(!$post)
abort(404);
return view('test/edit')->with('test', $post);
}
My form:
{{ Form::model($post, array('route' => array('test.update', $post->id), 'files' => true, 'method' => 'PUT')) }}
You're assigning the post value to 'test', so should be accessible with $test rather than $post.
You probably want to do either of these two things instead:
return view('test/edit')->with('post', $post);
or
return view('test/edit', ['post' => $post]);
https://laravel.com/docs/5.4/views
Your controller is sending a variable named "test", but your error says that your blade file doesn't have the $post variable passed into it. This can be fixed by changing "test" to "post" in your controller.

How to change From Name in Laravel Mail Notification

This is the problem:
The name associated with the email shows up as "Example"
In config/mail.php
set from property as:
'from' => ['address' => 'someemail#example.com', 'name' => 'Firstname Lastname']
Here, address should be the one that you want to display in from email and name should be the one what you want to display in from name.
P.S. This will be a default email setting for each email you send.
If you need to use the Name as a variable through code, you can also call the function from() as follows (copying from Brad Ahrens answer below which I think is good to mention here):
return $this
->from($address = 'noreply#example.com', $name = 'Sender name')
->subject('Here is my subject')
->view('emails.view');
You can use
Mail::send('emails.welcome', $data, function($message)
{
$message->from('us#example.com', 'Laravel');
$message->to('foo#example.com')->cc('bar#example.com');
});
Reference - https://laravel.com/docs/5.0/mail
A better way would be to add the variable names and values in the .env file.
Example:
MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example#example.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls
MAIL_FROM_NAME="My Name"
MAIL_FROM_ADDRESS=support#example.com
Notice the last two lines. Those will correlate with the from name and from email fields within the Email that is sent.
In the case of google SMTP, the from address won't change even if you give this in the mail class.
This is due to google mail's policy, and not a Laravel issue.
Thought I will share it here.
For anyone who is using Laravel 5.8 and landed on this question, give this a shot, it worked for me:
Within the build function of the mail itself (not the view, but the mail):
public function build()
{
return $this
->from($address = 'noreply#example.com', $name = 'Sender name')
->subject('Here is my subject')
->view('emails.welcome');
}
Happy coding :)
If you want global 'from name' and 'from email',
Create these 2 keys in .env file
MAIL_FROM_NAME="global from name"
MAIL_FROM_ADDRESS=support#example.com
And remove 'from' on the controller. or PHP code if you declare manually.
now it access from name and from email.
config\mail.php
'from' => [
'address' => env('MAIL_FROM_ADDRESS', 'info#example.com'),
'name' => env('MAIL_FROM_NAME', 'write name if not found in env'),
],
ON my controller.
$conUsBody = '';
$conUsBody .= '<h2 class="text-center">Hello Admin,</h2>
<b><p> '.trim($request->name).' Want some assesment</p></b>
<p>Here are the details:</p>
<p>Name: '.trim($request->name).'</p>
<p>Email: '.trim($request->email).'</p>
<p>Subject: '.trim($request->subject).'</p>';
$contactContent = array('contactusbody' => $conUsBody);
Mail::send(['html' => 'emails.mail'], $contactContent,
function($message) use ($mailData)
{
$message->to('my.personal.email#example.com', 'Admin')->subject($mailData['subject']);
$message->attach($mailData['attachfilepath']);
});
return back()->with('success', 'Thanks for contacting us!');
}
My blade template.
<body>
{!! $contactusbody !!}
</body>
I think that you have an error in your fragment of code. You have
from(config('app.senders.info'), 'My Full Name')
so config('app.senders.info') returns array.
Method from should have two arguments: first is string contains address and second is string with name of sender. So you should change this to
from(config('app.senders.info.address'), config('app.senders.info.name'))

flash message in auth controller

I'm looking for better solution to show flash message after login/logout/register. These methods are stored in AuthController through trait AuthenticatesAndRegistersUsers. My second condition is not to edit AuthenticatesAndRegistersUsers.
My actually hack is below, but i'm not happy for that.
Have you got better idea?
app/http/controllers/auth/authcontroller.php
public function postLoginwithFlash(Request $request)
{
return $this->postLogin($request)->with('flash_message','You are logged');
}
and routes.php
Route::post('login', ['as' => 'login', 'uses' => 'Auth\AuthController#postLoginWithFlash']);
and views ofc
#if (Session::has('flash_message'))
{{ Session::get('flash_message') }}
#endif
There is no 'native' way to do it. Either way you will have to change/edit the route.
Either you implement everything in the routes.php, or do it the way you already proposed – create a new method in AuthController. Essentially, it's the same thing.
However, I would recommend you to do proper manual check instead of returning postLogin(), eg.:
if (Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])) {
// Authentication passed...
return redirect()->route('dashboard');
} else {
return redirect()->refresh()->with('error', 'Those are not correct credentials!');
}
This way, you can add different flash messages to success and error cases while your proposed code will show the same message irrespective of result.
You can edit the language file ./resources/lang/en/auth.php, then change this line
'failed' => 'Your custom login error message',

Resources