Okay so I've got a view composer; that basically pulls a row from my table 'heros'. This then allow's me to obviously use the object $hero>whatever; on a page. The problem is this obviously only works for someone who is logged in.
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
As its using the
Auth::user()->
to find the correct row in the 'heros' table, once the user is no longer logged in it cannot access this. As no one is set to Auth.
So the error when I logout (or anyone no logged in) is this:
ErrorException
Trying to get property of non-object
));
//Authenticated group
Route::group(array('before' => 'auth'), function() {
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
How does one go about this?
Note: Still learning, I DID try and search but I found nothing probably because Im not sure how to put my issue into the correct sentance..
EDIT: Included home.blade.php
#extends('layout.main')
#section('content')
#if(Auth::check())
<p>Hello, {{{ Auth::user()->username }}}.</p>
#if($herodata)
Your hero:<br>
<b>{{ $herodata->hero; }}</b><br>
<b>Stats:</b>
LvL: {{ $herodata->level }}
Exp: {{ $herodata->exp }}
HP: {{ $herodata->hp }}/{{ $herodata->max_hp }}
Str: {{ $herodata->str }}
Atk: {{ $herodata->atk }}
Def: {{ $herodata->def }}
Int: {{ $herodata->int }}
Blk: {{ $herodata->blk }}
<form action="{{ URL::route('hero-delete') }}" method="POST">
<input type="submit" value="Delete"><br>
</form>
<form action="{{ URL::route('rest') }}" method="POST">
<input type="submit" value="Rest">
</form>
<form action="{{ URL::route('attack') }}" method="POST">
<input type="submit" value="Random attack">
</form>
<form action="{{ URL::route('hero-died') }}" method="POST">
<input type="submit" value="Died">
</form>
#else
<form action="{{ URL::route('hero-create') }}" method="POST">
Hero:<br>
You do not have a hero, create one!
<input type="text" name="hero">
<input type="submit" value="Create hero">
#if($errors->has('hero'))
{{ $errors->first('hero')}}
#endif
{{ Form::token()}}
</form>
#endif
#else
<p>You are not signed in.</p>
#endif
#stop
Check if the user is logged in before setting the hero data:
if (Auth::check())
{
View::composer(array('home'), function($view)
{
$view->with('herodata', Hero::where('owner_id', Auth::user()->id)->first());
});
}
Related
I want delete a comment, but when i click, this redirect me to laravel error page and display "" that error. Why is happen ?
web.php
Route::post('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
commentcontroller
public function destroy($id){
$comment=Comment::where('id',$id)->first();
$comment->delete();
return redirect()->back();
}
blade
#foreach($comment as $comments)
<form action="{{route('comment.destroy',$comments->id )}}" method="post">
<input type="hidden" name="_method" value="DELETE">
<button type="submit" class="btn"><i class="fa fa-trash-o"></i></button>
{{ csrf_field() }}
{{ method_field('DELETE') }}
<div class="comment-body"><p>{{ $comments->body }}</p></div>
</form>
#endforeach
If you add {{ method_field('DELETE') }} on the html form, you must change the route method to delete:
Route::delete('/comments/destroy/{id}', 'CommentController#destroy')->name('comment.destroy');
But if you want to use POST method on the route, you must remove {{ method_field('DELETE') }} from your form.
When I want to edit a specific category I want to see the previous value of that category, but I don't get previous value of the category in the form. I have used route model binding in CategoryController. The main part of the code is given bellow:
Controller:
public function edit(Category $category){
return view('admin.editcategory',compact('category'));
}
View:
<form class="form-horizontal" action="{{ route('Category.update',['category'=>$category]) }}" method="POST">
#method('PATCH')
<input type="text" name="category_name" value="{{ $category->category_name }}">
{{ $errors->first('category_name') }}
<textarea name="category_description" row="3">
{{ $category->category_description }}
</textarea>
{{ $errors->first('category_description') }}
<button type="submit" class="btn btn-primary">Edit category</button>
#csrf
</form>
Route:
Route::resource('Category','CategoryController');
In edit function passing the id of a category
public function edit($id){
$category=Category::find($id);
return view('admin.editcategory',compact('category'));
}
I am trying to pass a form through. I am using request method to get variables. here is my blade of a form:
<div class="add_photo">
<h1>Add a photo</h1>
<form action="{{Route('postPhoto')}}">
<span>Name: </span>
<input type="text" name="title">
<span>File: </span>
<input type="text" name="file">
<input type="submit" value="Add">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
Routes involved:
Route::get('/admin/gallery', 'GalleryController#manageGallery')->name('manageGallery');
Route::post('/admin/gallery', 'GalleryController#postPhoto')->name('postPhoto');
And this is my controller for it:
class GalleryController extends Controller
{
public function manageGallery() {
return view('home.manageGallery');
}
public function postPhoto(Request $request) {
die("works");
}
}
It does not throw error at me. It just does nothing. So my question is: am I using this method wrong or do I need something more? Thanks in advance.
Firstly make sure that the form you are using is using the correct method for your route
<div class="add_photo">
<h1>Add a photo</h1>
<form action="{{Route('postPhoto')}}" method="post">
<span>Name: </span>
<input type="text" name="title">
<span>File: </span>
<input type="text" name="file">
<input type="submit" value="Add">
<input type="hidden" name="_token" value="{{ Session::token() }}">
</form>
</div>
In your controller, put the following in the postPhoto function
public function postPhoto(Request $request)
{
dd($request);
}
You should now get a Request object output to the screen when you submit the form
You may wanna use Blade Forms in order to make Forms in a more natural way for Laravel
{{ Form::open(['route' => '/admin/gallery', 'method' => 'post', 'files' => true]) }}
{{ Form::text('title') }}
{{ Form::label('title', 'Name :') }}
{{ Form::file('file') }}
{{ Form::label('file', 'File :') }}
{{ Form::submit('Add') }}
{{ Form::close() }}
It reduces the overhead of adding the token by yourself as it is automatically added when using the Form facade.
And then, in your controller, you would do something like that to debug when sending the form :
<?php
use Request; /* do not forget this line */
class GalleryController extends Controller
{
public function postPhoto(Request $request)
{
dd($request->toArray());
}
}
I already had a post that lead to this question but now I isolated one problem and so its better to make a new clean post :
Symfony 2.1.3 with jordillonch/CrudGeneratorBundle
I used this CrudGenerator on an entity in which I have 2 timestampable fields that need to be updated automatically :
I installed Gedmo for the timestampable funtionnality.
The fields are :
cree_le (in english created_at) : #Gedmo\Timestampable(on="create")
and modifiele (in english updated_at) - #Gedmo\Timestampable(on="update")
The problem is that I want to customize the form to show only the fields to update by the user.
When I use the not customized edit.html.twig using {{ form_widget(edit_form) }} the update is working but the values of updated_at are not changed because the old value is submitted with the form.
I tried several things to customize the form but I did not yet find the solution.
This version of customized edit.html.twig is not working because the submitted form is not valid :
<form class="well" action="{{ path('employee_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
<input type="hidden" name="_method" value="PUT" />
<div>
{{ form_label(edit_form.nom) }}
{{ form_errors(edit_form.nom) }}
{{ form_widget(edit_form.nom) }}
</div>
<div>
{{ form_label(edit_form.email) }}
{{ form_errors(edit_form.email) }}
{{ form_widget(edit_form.email) }}
</div>
<div>
{{ form_label(edit_form.telephone, 'Téléphone') }}
{{ form_errors(edit_form.telephone) }}
{{ form_widget(edit_form.telephone) }}
</div>
<div>
{{ form_label(edit_form.actif) }}
{{ form_errors(edit_form.actif) }}
{{ form_widget(edit_form.actif) }}
</div>
<input type="hidden" id="too_employeebundle_employee_cree_le" name="too_employeebundle_employee[cree_le]" value="{{ entity.creele|date('Y-m-d H:i:s') }}">
<input type="hidden" id="too_employeebundle_employee_modifie_le" name="too_employeebundle_employee[modifie_le]" value="{{ entity.modifiele|date('Y-m-d H:i:s') }}">
{{ form_widget(edit_form._token) }}
<p>
<button type="submit" class="btn btn-success">{{ 'views.edit.editbutton'|trans({}, 'JordiLlonchCrudGeneratorBundle') }}</button>
</p>
</form>
Sure I'm doing something wrong but who could tell me how to go on ?
Remove cree_le and modifiele from your Form Class and dont render them on your template.
Then let Timestampable do his job! when you create something the cree_le value will be automatic set. When you change something the modifiele value will be automatic set too.
I'm using Symfony 2.1 for a project and FOSUserBundle to manage the users.
I'm trying to customize the change password form and I can't display well the error messages. Indeed, when an input is wrong filled, the error message is printed between the label and the input (with a list structure). But I like to display it after the input or below it.
Moreover, I'd like to display my change password form within a settings page so I need to display some other forms. How can I integrate this form in a precise place in a page?
Thanks in advance,
Valentin
For my first question, I succeeded with this form for the change_password:
<form action="{{ path('fos_user_change_password') }}" {{ form_enctype(form) }} method="POST" class="fos_user_change_password">
<div class="form_errors_change_pwd">
{{ form_errors(form) }}
</div>
<div>
{{ form_label(form.current_password) }}
{{ form_widget(form.current_password) }}
<span class="form_error_field">{{ form_errors(form.current_password) }}</span>
</div>
<div>
{{ form_label(form.new.first) }}
{{ form_widget(form.new.first) }}
<span class="form_error_field">{{ form_errors(form.new.first) }}</span>
</div>
<div>
{{ form_label(form.new.second) }}
{{ form_widget(form.new.second) }}
<span class="form_error_field">{{ form_errors(form.new.second) }}</span>
</div>
{{ form_rest(form) }}
<div>
<input type="submit" value="{{ 'change_password.submit'|trans({}, 'FOSUserBundle') }}" />
</div>
But I'm still trying to integrate this form inside another page with multiple forms...