Laravel: Undefined variable content - php

I have setup a Route:
Route::resource('conferences', 'ConferencesController)
Artisan therefore shows me a route:
POST conferences | conferences.store | ConferencesController#store
When I submit a Form from the create View, I get the error that a variable in my layout file has not been defined.
Undefined variable: content is shown, nothing has been posted.
I opened my form like this:
{{ Form::open(array('url' => '/conferences', 'class' => 'conference-form')) }}
And finally, my store method in ConferencesController looks like this:
public function store()
{
$validator = Validator::make(Input::all(), Conference::$rules);
if($validator->passes()){
$conference = new Conference();
$conference->title = Input::get('title');
$conference->description = Input::get('description');
$conference->location = Input::get('location');
$conference->plannedTime = Input::get('plannedTime');
$conference->save();
Mail::pretend();
Mail::send('emails.conference.create', ['title' => Input::get('title'), 'location' => Input::get('location'), 'plannedTime' => Input::get('plannedTime')], function($message){
$message->to('email')->subject('Een nieuw evenement is gemaakt.');
});
Redirect::to('/conferences')->with('message', 'Nieuw event is aangemaakt!');
} else {
Redirect::to('/')->with('message', 'Iets ging mis');
}
}
How do I fix this error?
** EDIT: Added create method **
public function create(){
$this->layout->content = View::make('conferences.create');
}

This should be really straight forward. In your views directory we usually have a folder called layouts where we put the page structure something like:
// default.blade.php
<html>
<head> </head>
<body>
#yield('content'); // this is where your views will be loaded
</body>
</html>
then in your case you should create a conferences folder and then a file create.blade.php in it.
#extends('layouts.default')
#section('content')
// your forms etc
#stop
And in your create method inside ConferencesController
public function create() {
return View::make('conferences.create');
}
And one last thing, when you try to send the email you should be passing an email address inside the to() function and you are passing a string

Related

Undefined variable problem on Laravel 9.x

I'm trying to get a my title variable from my control page and display it on the about page.
I don't think I have a typo, but it might me. I'm not sure.
Here is my control page code;
class PagesController extends Controller
{
public function index(){
$title = 'Welcome to Laravel';
return view ('pages.index')->with('title', $title);
}
public function about(){
$title = 'About us';
return view ('pages.about')->with('title', $title);
}
public function services(){
$title = 'The services';
return view ('pages.services')->with('title', $title);
}
}
In this page, the index and services functions work fine, but I can't get the about page.
Here is my display pages;
This is Index page
#extends('layouts.app')
#section('content')
<h1>{{$title}}</h1>
<p>This is the Laravel Application</p>
#endsection
This is the about page:
#extends('layouts.app')
#section('content')
<h1>{{$title}}</h1>
<p>This is the About page</p>
#endsection
The error I have
Do this:
return view ('pages.index', compact('title'));
or:
return view ('pages.index', [
'title' => $title
]);
Since you are returning just the title, there isn't any need to call any verbs. Rather you should directly call the view:
route::view('/about', 'Pagecontroller#about');
or
pass the parameter by compact:
return view ('pages.index', compact('title'));
or
return view ('pages.index', ['title' => $title]);
Since this is a test application from a lesson, I forgot to delete some extra code in my route file.
This is my route file:
Route::get('/', 'App\Http\Controllers\PagesController#index');
Route::get('/about', 'App\Http\Controllers\PagesController#about');
Route::get('/services', 'App\Http\Controllers\PagesController#services');
The commented area shouldn't be here. That was the whole problem over here...
// Route::get('/about', function(){
// return view ('pages.about');
// });
This form of passing variables is a short-lived entry of a variable into the session. Then accessing the variable on the page should look like this:
{{ session('title') }}
If you want to pass data to the view, then you need to use the method
return view('pages.services', ['title' => $title]);
Laravel views

Display JSON Data store in JSON in blade (Laravel 8)

I have produce notifications through email and manage to send the email.
Now I want to change the template provided by Laravel so I create a new customize blade that will be displayed.
I have a problem on transferring the data to the blade view.
Below is my code:
public function __construct($offerData)
{
$this->offerData = $offerData;
}
public function toMail($notifiable)
{
/*This is the original code with default template */
// return (new MailMessage)
// ->greeting($this->offerData['name'])
// ->line($this->offerData['body'])
// ->action($this->offerData['offerText'], $this->offerData['offerUrl'])
// ->line($this->offerData['thanks']);
/*This is the code use to display the customize template*/
return (new MailMessage)->view(
'email_notification',
['data' => $this->offerData]
);
Below is How I display the code:
<!DOCTYPE html>
<html>
<head>
<title>Hi Awak</title>
</head>
<body>
#foreach($data as $data_)
<p>{{ $data_ }}</p>
#endforeach
</body>
</html>
This is the result:
I cannot do any specific modification for each data as you can see it only repeat the p tag here.
Thank you for help....
I manage to found the solution...
I just store array in array...
public function toMail($notifiable)
{
return (new MailMessage)->view(
'email_notification',
['name' => [$this->offerData['name']],
'body' => [$this->offerData['body']]
]
);
}

Laravel4: Call to a member function update() on a non-object

Following are my codes:
Model:
class Slide extends \Eloquent {
// Add your validation rules here
public static $rules = [
'title' => 'required|between:3,100',
'image' => 'required',
'url' => 'url',
'active' => 'integer'
];
// Don't forget to fill this array
protected $fillable = ['title', 'image', 'url', 'active'];
}
Controller Update Method:
public function update($id)
{
$slide = Slide::find($id);
$validator = Validator::make($data = Input::all(), Slide::$rules);
if ($validator->fails())
{
return Redirect::back()->withErrors($validator)->withInput();
}
$slide->update($data);
return Redirect::route('admin.slides.index')
->with('message', 'Slide has been updated.')
->with('message-type', 'alert-success');
}
Route:
Route::group(array('prefix' => 'admin'), function() {
# Slides Management
Route::resource('slides', 'AdminSlidesController', array('except' => array('show')));
});
Form in View:
{{ Form::model($slide, array('route' => 'admin.slides.update', $slide->id, 'method' => 'put')) }}
#include('admin/slides/partials/form')
{{ Form::close() }}
Partial Form is simple form, not sure if I need to share it here or not. Let me know.
Error:
Edit page loads perfectly and populates data from db, but when I submit the edit form, I get following error:
Call to a member function update() on a non-object
The following line seems to be creating problems:
$slide->update($data);
I have searched over the internet for solution but nothing is working. Have tried composer dump_autoload, even tried doing everything from scratch in a new project, still same issue. :(
Help please!!
---- Edit ----
Just quickly tried following:
public function update($id)
{
$slide = Slide::find($id);
$slide->title = Input::get('title');
$slide->save();
return Redirect::route('admin.slides.index')
->with('message', 'Slide has been updated.')
->with('message-type', 'alert-success');
}
Now the error:
Creating default object from empty value
----- Solution: -----
The problem was with my form as suggested by #lukasgeiter
I changed my form to following at it worked like a charm:
{{ Form::model($slide, array('route' => array('admin.slides.update', $slide->id), 'method' => 'put')) }}
use $slide->save(); instead of $slide->update($data);
to update a model please read the laravel doc here
To update a model, you may retrieve it, change an attribute, and use the save method:
EX :
$user = User::find(1);
$user->email = 'john#foo.com';
$user->save();
The actual problem is not your controller but your form.
It should be this instead:
{{ Form::model($slide, array('route' => array('admin.slides.update', $slide->id), 'method' => 'put')) }}
This mistake causes the controller to receive no id. Then find() yields no result and returns null.
I recommend besides fixing the form you also use findOrFail() which will throw a ModelNotFoundException if no record is found.
$slide = Slide::findOrFail($id);

Splitting form into two pages causes error

I have setup the laravel resource controller and utilized the edit and update methods to edit user profiles. My profile form turned out to be too long, so I would like to split it into two forms.
The trouble is that the update function appears to be built into the resource controller - I tried just copy the method, add in my inputs and rename it. I updated the routes and view, but received an error. I also tried to have both forms call the same function, but the information that wasn't included in the form was delete from my db.
My question is, how do I split my form into two, so I can update my user profile from two forms instead of one? Any help would be appreciated. Thank you
For reference, here is my ContractorController
public function edit($id)
{
//
// get the contractor
$contractor = Contractor::find($id);
// show the edit form and pass the contractor
return View::make('contractors.edit')
->with('contractor', $contractor);
}
public function update($id)
{
//
// validate
// read more on validation at http://laravel.com/docs/validation
$rules = array(
);
$validator = Validator::make(Input::all(), $rules);
// process the login
if ($validator->fails()) {
return Redirect::to('contractors/' . $id . '/edit')
->withErrors($validator)
->withInput(Input::except('password'));
} else {
// store
$contractor = Contractor::find($id);
$contractor->name = Input::get('name');
$contractor->tag_line = Input::get('tag_line');
$contractor->contact_name = Input::get('contact_name');
//would like to split items below into a separate form:
$contractor->public_email = Input::get('public_email');
$contractor->phone = Input::get('phone');
$contractor->address_1 = Input::get('address_1');
$contractor->city = Input::get('city');
$contractor->state = Input::get('state');
$contractor->zip = Input::get('zip');
$contractor->website = Input::get('website');
$contractor->story = Input::get('story');
$contractor->save();
// redirect
Session::flash('message', 'Successfully updated profile!');
return Redirect::to('contractors');
}
}
Start of form in edit.blade.php
{{ Form::model($contractor, array('route' => array('contractors.update', $contractor->id), 'class' => 'form-horizontal', 'method' => 'PUT')) }}

Missing argument Laravel controller function edit

I have a problem with this
I have un list of articles, and each element has a button to edit, how the next code:
<p>modifier l'article</p>
and I'm sending to the file route:
Route::get('/edit', 'ArticleController#edit');
to the file ArticleController method edit:
public function edit($idarticle)
{
$artic=article::find($idarticle);
if(is_null ($artic))
{
App::abort(404);
}
$form_data = array('route' => array('article.update', $artic->idarticle), 'method' => 'PATCH');
$action = 'modifier';
return View::make('article.create')->with('artic', $artic);
}
then I don't understand my error
Probably change Route::get('/edit', 'ArticleController#edit'); to Route::get('/edit/{idarticle}', 'ArticleController#edit');
Also
<p>modifier l'article</p>
needs to be
<p>modifier l'article</p>
The parameter in the router is not passed as an html parameter, but rather a part of the URL. So combines these two changes, it should be working.

Categories