How can I add reply to in Laravel aplication? - laravel

I started with the following code:
Mail::to('mail#gmail.com')->send(new ContactFormMail($email_data));
This works fine, but I am not sure how can I add 'reply to' to this code.
Next thing I tried is:
Mail::send('emails.contact-form', $email_data, function ($message) {
$message->from('admin#laravel.com', 'Laravel');
$message->to('admin#laravel.com');
$message->replyTo('admin#laravel.com', 'Laravel');
});
This has the following problem:
No hint path defined for [mail]...
In this case, if I remove #component('mail::message') from the mail view, the message is sent as it should, but it has no styling.
Finally, I do have this piece of code in my Mailable class:
public function build()
{
return $this->markdown('emails.contact-form');
}
Any assistance would be appreciated.
Thanks

The problem you have is that the first argument of Mail::send only accepts either a view email template or a mailable class. however, you passed in a markdown file. so it's not happy with it.
A simple solution is
public function build()
{
return $this->markdown('emails.contact-form')
->replyTo('admin#laravel.com', 'Laravel');
}
Mail::to('mail#gmail.com')->send(new ContactFormMail($email_data));

Related

Laravel Nova - Observer Update Method Causes 502

When trying to update a resource in Laravel Nova that has a Observer the update loads for a while and then ends with a 502 error. The observer is registered correctly (the created method works fine) and I'm not trying to do anything special in the updated method. Any ideas?
public function updated(Model $model)
{
//
$model->title = 'test';
$model->save();
}
If I try this without the $model->save(), there is no 502 error but the change I want to happen also doesn't happen. I get the green success message, and any change I make on the form prior to updating occurs, but not the change I'm trying to make during the updated method.
Any help troubleshooting this would be appreciated
I am not very good at Laravel, but I think, that you should to try this:
In your model file add method:
public function saveQuietly(array $options = [])
{
return static::withoutEvents(function () use ($options) {
return $this->save($options);
});
}
Then, in your updated method in observer do something like this:
public function updated(Model $model)
{
$model->title = 'test';
$model->saveQuietly();
}

Add method to Controller

Sorry about restating my other question but the people that commented wanted more information, like uploading snapshot or the log file I don't know how to upload here.
I wan't to add a method(not function my mistake) to my PhotoController
public function search(){
return view('photos.search');
}
My route
Route::get('/photos/search','PhotosController#search');
I have created the file search.blade.php in the /photos in that file is 1 word "search"
Here is the error I get when I try it in the browser.
Facade\Ignition\Exceptions\ViewException
Trying to get property 'title' of non-object (View:/Web/PhotoAlbum/resources/views/photos/show.blade.php)
Illuminate\Foundation\Bootstrap\HandleExceptions::handleError
#section('content')
<h3>{{$photo->title}}</h3>
/{{$photo->photo}}" alt="{{$photo->title}}">
/{{$photo->photo}}" alt="{{$photo->title}}">
{!!Form::open(['action'=> ['PhotosController#destroy', $photo->id],'method' => 'POST'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete',['class'=> "btn btn-primary"])}}
{!!Form::close()!!}
#endsection
Note that this error is on another page. I have not edited this page, what I stated above is all I have done also note that the program work well and this error only shows when I try to access the search page.
Thank you
The $photo variable doesn't exist in your view nor was it provided by the controller.
Try this:
public function search(){
$photo = ... // retrieve the photo
return view('photos.search', ["photo" => $photo]);
}
It is because, you have not compact the variable. write the function like below:
Use Your model name at the top of your controller.
use App\YourModelName;
public function search(){
$photo = YourModelName::all();
return view('photos.search', compact('photo'));
}
Hope it will work.

Returning same variable to every controller in laravel

I need to send the same result to almost every view page, so I need to bind the variables and return with every controller.
My sample code
public function index()
{
$drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
$locations = Location::get();
return view('visitor.index', compact('drcategory','locations'));
}
public function contact()
{
$drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
$locations = Location::get();
return view('visitor.contact', compact('drcategory','locations'));
}
But as you see, I need to write same code over and over again. How can I write it once and include it any function whenever I need?
I thought about using a constructor, but I cannot figure out how I can implement this.
You are able to achieve this by using the View::share() function within the AppServicerProvider:
App\Providers\AppServiceProvider.php:
public function __construct()
{
use View::Share('variableName', $variableValue );
}
Then, within your controller, you call your view as normal:
public function myTestAction()
{
return view('view.name.here');
}
Now you can call your variable within the view:
<p>{{ variableName }}</p>
You can read more in the docs.
There are a few ways to implement this.
You can go with a service, a provider or, like you said, within the constructor.
I am guessing you will share this between more parts of your code, not just this controller and for such, I would do a service with static calls if the code is that short and focused.
If you are absolutely sure it is only a special case for this controller then you can do:
class YourController
{
protected $drcategory;
public function __construct()
{
$this->drcategory = DoctorCategory::orderBy('speciality', 'asc')->get();
}
// Your other functions here
}
In the end, I would still put your query under a Service or Provider and pass that to the controller instead of having it directly there. Maybe something extra to explore? :)
For this, you can use View Composer Binding feature of laravel
add this is in boot function of AppServiceProvider
View::composer('*', function ($view) {
$view->with('drcategory', DoctorCategory::orderBy('speciality', 'asc')->get());
$view->with('locations', Location::get());
}); //please import class...
when you visit on every page you can access drcategory and location object every time
and no need to send drcategory and location form every controller to view.
Edit your controller method
public function index()
{
return view('visitor.index');
}
#Sunil mentioned way View Composer Binding is the best way to achieve this.

Laravel - Action not defined but it is defined

I get this error when i try loading blade.php
Action App\Http\Controllers\InventoryItemController#change not defined.
I have change function in InventoryItemController
public function change($new_status)
{
//
}
This started when I wanted to make button
Confirm Change
I did everything same when i made Edit button and that button works normally.
UPDATE 1
My button looks like this now
<a href="{{route('change', [$inventoryitem['new_status'],
$inventoryitem['asset_id']])}}"class="btn btn-info">Confirm Change</a>
and my change function is this
public function change($new_status, $asset_id)
{
$asset = Asset::find($asset_id);
$asset->status = $new_status;
return redirect('showasset', compact('asset','asset_id'));
}
and my route in web is like this
Route::get('change/{$new_status}/{$asset_id}','InventoryItemController#change')->name('change');
But after i click button it just redirect me to url .../change/4/1 and that's it. Nothing changes.
Using Action is deprecated in Laravel
You can use routes instead.
Define Routes in your routes files (/routes/web.php) like.
Route::get('change/{status}','InventoryItemController#change')->name('change');
and then in your view
Confirm Change
In your controller use.
public function change ($status){
// rest of the function.
}
Hope this helps
Define your controller's method in route file as following:
Route::get('url/{new_status}',InventoryItemController#change);
Answer on UPDATE 1
public function change($new_status, $asset_id)
{
$asset = Asset::find($asset_id);
$asset->status = $new_status;
$asset->save();
return view('your_view_path',compact('variable1','variable2'));
}
Final error was in my route
Route::get('change/{$new_status}/{$asset_id}','InventoryItemController#change')->name('change');
It should be like this
Route::get('change/{new_status}/{asset_id}','InventoryItemController#change')->name('change');
After that change everything is working flawlessly. Thank you for your help guys!

Extending Form Validation in Codeigniter call to a member function on a non-object error

I know this has been asked before, and I've looked through every answer posted, but to no avail. Basically, I am trying to extend the Codeigniter form validation library on a CI2+ project, but every single time I get the error:
Fatal error: Call to a member function get_errors_array() on a non-object
Below is my code:
application/core/CB_Form_validation.php
class CB_Form_validation extends CI_Form_validation{
function __construct($rules = array()) {
parent::__construct($rules);
log_message('debug', "CB_Form_validaiton class initialized");
}
public function get_errors_array() {
return $this->_error_array;
}
}
and then in my ajax file I have the construct etc.
public function save(){
if($this->form_validation->run() == FALSE){
}
}
and inside that if statement I have tried the following:
echo $this->form_validation->get_errors_array();
echo $this->cb_form_validation->get_errors_array();
echo get_errors_array();
I have also tried placing the CB_Form_validation.php in application/libraries as well. Just as well, I have also tried this in my construct of my ajax controller
$this->load->library('CB_Form_validation');
$this->load->library('core/CB_Form_validation');
And I have set CB_ as my custom class prefix
Turns out that to fix this, you should do the following:
Move your custom form validation class to the application/libraries folder
You can keep the construct of your custom class how it is
Use $this->form_validation->method() to access the function you would like
As long as your loading the form_validation library, you don't need to perform $this->load->library('custom_class') because CI picks up on your custom prefix anyways
Hope this helps someone

Resources