Laravel по-русски

Русское сообщество разработки на PHP-фреймворке Laravel.

Ты не вошёл. Вход тут.

#1 02.10.2016 11:10:26

не могу передать $request в Mailable в Laravel 5.3

не могу понять как передать объект $request в Mailable Laravel 5.3
я всё время получаю ошибку, что $request is not defined.


web.php

    Route::get('/',  			['as' => 'home', 		'uses' => 'HomeController@index']);
    Route::get('/callback', 		['as' => 'callback.show', 	'uses' => 'PostController@callbackshow']);
    Route::post('/callback', 		['as' => 'callback.send', 	'uses' => 'PostController@callbacksend']);

Controllers\PostController.php

    namespace App\Http\Controllers;
    use App\Mail\Callback;
    use Illuminate\Http\Request;
    
    
    class PostController extends Controller {
    
    
        public function __construct()
        {
        }
    
        public function callbackshow() 
        {
            return view('callback');   
        }
    
        public function callbacksend(Request $request) 
        {
            $email = new Callback($request);
            \Mail::to('test@gmail.com')->send($email);
        }

Mail\Callback.php

    namespace App\Mail;
    use Illuminate\Http\Request;
    use Illuminate\Bus\Queueable;
    use Illuminate\Mail\Mailable;
    use Illuminate\Queue\SerializesModels;
    use Illuminate\Contracts\Queue\ShouldQueue;
    
    class Callback extends Mailable
    {
        use Queueable, SerializesModels;
    
        public function __construct(Request $request)
        {
            $this->request = $request->all();
        }
    
        public function build()
        {
            return $this->view('emails.callback');
        }
    }

emails\callback.blade.php

    Name: {{ $request->name }}<br>
    Email :  {{ $request->email }}<br>

вот ошибка

    Whoops, looks like something went wrong.
    2/2 ErrorException in 76aac56b59bc685390113940ddc975544836b896.php line 2: Undefined variable: request (View: D:\OpenServer\domains\blog2\resources\views\emails\callback.blade.php)
    
        in 76aac56b59bc685390113940ddc975544836b896.php line 2
        at CompilerEngine->handleViewException(object(ErrorException), '0') in PhpEngine.php line 44
        at PhpEngine->evaluatePath('D:\OpenServer\domains\blog2\storage\framework\views/76aac56b59bc685390113940ddc975544836b896.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in CompilerEngine.php line 59
        at CompilerEngine->get('D:\OpenServer\domains\blog2\resources\views/emails/callback.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in View.php line 149
        at View->getContents() in View.php line 120
        at View->renderContents() in View.php line 85
        at View->render() in Mailer.php line 430
        at Mailer->getView('emails.callback', array('connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in Mailer.php line 310
        at Mailer->addContent(object(Message), 'emails.callback', null, null, array('connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in Mailer.php line 186
        at Mailer->send('emails.callback', array('connection' => null, 'queue' => null, 'delay' => null), object(Closure)) in Mailable.php line 117
        at Mailable->send(object(Mailer)) in Mailer.php line 173
        at Mailer->send(object(Callback)) in MailableMailer.php line 99
        at MailableMailer->send(object(Callback)) in PostController.php line 24
        at PostController->callbacksend(object(Request))
        at call_user_func_array(array(object(PostController), 'callbacksend'), array(object(Request))) in Controller.php line 55
        at Controller->callAction('callbacksend', array(object(Request))) in ControllerDispatcher.php line 44
        at ControllerDispatcher->dispatch(object(Route), object(PostController), 'callbacksend') in Route.php line 190
        at Route->runController() in Route.php line 144
        at Route->run(object(Request)) in Router.php line 642
        at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
        at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
        at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
        at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
        at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
        at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
        at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
        at Pipeline->then(object(Closure)) in Router.php line 644
        at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 618
        at Router->dispatchToRoute(object(Request)) in Router.php line 596
        at Router->dispatch(object(Request)) in Kernel.php line 267
        at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
        at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
        at Pipeline->then(object(Closure)) in Kernel.php line 149
        at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
        at Kernel->handle(object(Request)) in index.php line 54
    
    1/2 ErrorException in 76aac56b59bc685390113940ddc975544836b896.php line 2: Undefined variable: request
    
        in 76aac56b59bc685390113940ddc975544836b896.php line 2
        at HandleExceptions->handleError('8', 'Undefined variable: request', 'D:\OpenServer\domains\blog2\storage\framework\views\76aac56b59bc685390113940ddc975544836b896.php', '2', array('__path' => 'D:\OpenServer\domains\blog2\storage\framework\views/76aac56b59bc685390113940ddc975544836b896.php', '__data' => array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message)), 'obLevel' => '0', '__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in 76aac56b59bc685390113940ddc975544836b896.php line 2
        at include('D:\OpenServer\domains\blog2\storage\framework\views\76aac56b59bc685390113940ddc975544836b896.php') in PhpEngine.php line 42
        at PhpEngine->evaluatePath('D:\OpenServer\domains\blog2\storage\framework\views/76aac56b59bc685390113940ddc975544836b896.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in CompilerEngine.php line 59
        at CompilerEngine->get('D:\OpenServer\domains\blog2\resources\views/emails/callback.blade.php', array('__env' => object(Factory), 'app' => object(Application), 'errors' => object(ViewErrorBag), 'connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in View.php line 149
        at View->getContents() in View.php line 120
        at View->renderContents() in View.php line 85
        at View->render() in Mailer.php line 430
        at Mailer->getView('emails.callback', array('connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in Mailer.php line 310
        at Mailer->addContent(object(Message), 'emails.callback', null, null, array('connection' => null, 'queue' => null, 'delay' => null, 'message' => object(Message))) in Mailer.php line 186
        at Mailer->send('emails.callback', array('connection' => null, 'queue' => null, 'delay' => null), object(Closure)) in Mailable.php line 117
        at Mailable->send(object(Mailer)) in Mailer.php line 173
        at Mailer->send(object(Callback)) in MailableMailer.php line 99
        at MailableMailer->send(object(Callback)) in PostController.php line 24
        at PostController->callbacksend(object(Request))
        at call_user_func_array(array(object(PostController), 'callbacksend'), array(object(Request))) in Controller.php line 55
        at Controller->callAction('callbacksend', array(object(Request))) in ControllerDispatcher.php line 44
        at ControllerDispatcher->dispatch(object(Route), object(PostController), 'callbacksend') in Route.php line 190
        at Route->runController() in Route.php line 144
        at Route->run(object(Request)) in Router.php line 642
        at Router->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 53
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in SubstituteBindings.php line 41
        at SubstituteBindings->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in VerifyCsrfToken.php line 65
        at VerifyCsrfToken->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in ShareErrorsFromSession.php line 49
        at ShareErrorsFromSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in StartSession.php line 64
        at StartSession->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in AddQueuedCookiesToResponse.php line 37
        at AddQueuedCookiesToResponse->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in EncryptCookies.php line 59
        at EncryptCookies->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
        at Pipeline->then(object(Closure)) in Router.php line 644
        at Router->runRouteWithinStack(object(Route), object(Request)) in Router.php line 618
        at Router->dispatchToRoute(object(Request)) in Router.php line 596
        at Router->dispatch(object(Request)) in Kernel.php line 267
        at Kernel->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
        at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
        at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
        at Pipeline->Illuminate\Routing\{closure}(object(Request)) in Pipeline.php line 104
        at Pipeline->then(object(Closure)) in Kernel.php line 149
        at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116
        at Kernel->handle(object(Request)) in index.php line 54

Не в сети

#2 02.10.2016 13:57:11

Re: не могу передать $request в Mailable в Laravel 5.3

Ты не передаешь переменные во вьюху. https://laravel.ru/docs/v5/views#данные

Не в сети

#3 02.10.2016 15:56:21

Re: не могу передать $request в Mailable в Laravel 5.3

Ну формально да, не передаю. Но если ты посмотришь видео на ларакасте https://laracasts.com/series/whats-new- … episodes/6
То в нем Джефри точно также не передаёт переменные во вьюху.

Не в сети

#4 02.10.2016 21:17:29

Re: не могу передать $request в Mailable в Laravel 5.3

сейчас попробовал сделать так

        public function build()
        {
            return $this->view('emails.callback')->withRequest($request);
        }

и так

        public function build(Request $request)
        {
            return $this->view('emails.callback')->withRequest($request);
        }

результат тот же Undefined variable: request

Не в сети

#5 05.10.2016 13:16:08

Re: не могу передать $request в Mailable в Laravel 5.3

А в методе build переменная $request чему равна? (второй способ)

Изменено DBR (05.10.2016 13:16:42)

Не в сети

#6 05.10.2016 13:23:16

Re: не могу передать $request в Mailable в Laravel 5.3

Скорее всего в классе Callback не хватает явно заданного публичного свойства $request.

Посмотрел каст, там данные берутся через рефлексию (-2:12). Скорее всего она не читает динамически (магически) заданные свойства.

Не в сети

#7 07.10.2016 23:19:10

Re: не могу передать $request в Mailable в Laravel 5.3

вот так я тоже пробовал
в ответ получаю Undefined variable: name

namespace App\Mail;
use Illuminate\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class Callback extends Mailable
{
    use Queueable, SerializesModels;

    public $request;

    public function __construct(Request $request)
    {
        $this->request = $request->all();
    }

    public function build(Request $request)
    {
        return $this->view('emails.callback');
    }
}

Не в сети

#8 08.10.2016 11:29:05

Re: не могу передать $request в Mailable в Laravel 5.3

Теперь у тебя нет переменной name, а это уже другая переменная. Или удали ее из шаблона или так же добавь как свойство класса.

Не в сети

#9 08.10.2016 13:58:09

Re: не могу передать $request в Mailable в Laravel 5.3

получилось!!!
1) сначала надо объявить пустую публичную переменную $input
2) потом ей в __construct присвоить значение $request->all(). Но обязательно надо добавить $this
и уже тогда она попадает в шаблон


use Illuminate\Http\Request;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class Callback extends Mailable
{
    use Queueable, SerializesModels;

    public $input;

    public function __construct(Request $request)
    {
        $this->input = $request->all();
    }


    public function build()
    {
        return $this->from('info@expertphp.in')
                    ->view('emails.callback');
    }
}

Не в сети

#10 11.10.2016 03:42:44

Re: не могу передать $request в Mailable в Laravel 5.3

Поздравляю smile

Не в сети

Подвал раздела