Laravel4: upload a photo - php

I'm stuck..
I want to add to give the possibity to add a new photo through a form.
I have the Photo resource properly set up.
The table photo with: id, title, caption, path:string.
This is the form that i made until now in the view photo.create:
{{ Form::open(array('route' => 'photos.store'),'image/save', array('files'=> true)) }}
{{ Form::label('title', 'Title: ') }}
{{ Form::text('title') }}
{{ Form::label('caption', 'Caption: ') }}
{{ Form::textarea('caption') }} <br>
{{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}
{{ Form::close() }}
How can i add a button thanks to which select a new file?
Thank you!!
EDIT:
This is the simple store method i made until now:
public function store()
{
$input = Input::all();
$rules = array('title' => 'required', 'path' => 'required');
$validation = Validator::make($input, $rules);
if ($validation->passes())
{
$photo = new Photo();
$photo->title = $input['title'];
$photo->caption = $input['caption'];
$photo->path = $input['path'];
$photo->save();
return Redirect::route('photos.index');
}
return Redirect::back()->withInput()->withErrors($validation)->with('message','There were validation message');
}
How can i put correctly there that implementation to retrieve the file and store in the folder located in public/img ?
And then how can i save that path and put into $photo->path ?
Thank you very much!!

Use Form::file('image')
Then, you can retrieve your uploaded file with Input::file('image') and move it to a destination with Input::file('file')->move(YOUR_DESTINATION_PATH);
References : http://laravel.com/docs/requests#files and http://laravel.com/docs/html#file-input
edit :
To store your uploaded file to public/img : Input::file('file')->move(base_path() . '/public/img');
Storing path in database :
$photo->path = base_path() . '/public/img' . Input::file('file')->getClientOriginalName(); // if you want your real path on harddrive
or
$photo->path = URL::to('img/' . Input::file('file')->getClientOriginalName()); // if you want an exploitable path for http render
second edit
See my correction on your form http://paste.laravel.com/KX9
#extends('master')
#section('blog')
<div class="span12 well">
{{ link_to_route('photos.index', 'Back to index') }}
</div>
<div class="span12 well">
{{ Form::open(array('route' => 'photos.store', 'files'=> true)) }}
{{ Form::label('title', 'Title: ') }}
{{ Form::text('title') }}
{{ Form::label('caption', 'Caption: ') }}
{{ Form::textarea('caption') }}
{{ Form::label('image', 'Image: ') }}
{{ Form::file('image') }}
<br>
{{ Form::submit('Add Photo', array('class' => 'btn btn-primary' )) }}
{{ Form::close() }}
<br><br>
#if($errors->any())
{{ implode('', $errors->all('<li class="error">:message</li>')) }}
#endif
</div>
#stop

you can use Form::file('image');
see http://laravel.com/docs/html#file-input

{{ Form::open(array('action' => 'HomeController#upload', 'files'=>true)) }}
{{ Form::label('image', 'Upload Image')}}
{{ Form::file('image') }}
{{ Form::submit('Submit') }}
{{ Form::close() }}
Step2:
public function upload(){
$image = Input::file('image');
$savepath = 'public/uploads/';
$filename = $image->getClientOriginalName();
Input::file('image')->move($savepath, $filename);
}
Final Step:
Route::get('/upload' function()
{
return View::make('upload');
});
Route::post('/upload', 'HomeController#upload');

Related

MethodNotAllowedHttpException laravel 4.2 error

here is my ContactController.php:
public function destroy($id){
$contact = Contact::find($id);
$contact->delete();
return Redirect::to('http://localhost:8000/contactsview');
}
Here is my rountes.php
Route::delete('/contactsview/destroy/{id}', array('uses'=>'ContactController#destroy'));
Here is my index.blade.php:
{{ Form::open(array('url'=>'/contactsview/delete/'.$contact->id, 'method'=>'DELETE', 'style'=>'display:inline;')) }}
<!-- {{ Form::hidden('id', $contact->id) }} -->
{{ Form::submit('Delete') }}
{{ Form::close() }}
What did I do wrong?
Try the form with this instead, passing in the $contact->id as a param rather than directly in the URL:
{{ Form::open(array('method' => 'DELETE', 'action' => array('ContactController#destroy', $contact->id )) }}

Getting a NotFoundHttpException() Error in laravel

I just have a very simple product category creation form in laravel , like so:
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
For the create method i have the following code:
public function postCreate() {
$validator = Validator::make(Input::all() , Category::$rules);
if($validator->passes()) {
$category = new Category;
$category->name = Input::get('name');
$category->save();
return Redirect::to('admin/categories/index')
->with('message' , 'Category created');
}
return Redirect::to('admin/categories/index')
->with('message' , 'something went wrong')
->withError($validator)
->withInput();
}
Now when i click on the submit button, i get the following error:
C:\xampp\htdocs\ecomm\bootstrap\compiled.php
if (!is_null($route)) {
return $route->bind($request);
}
$others = $this->checkForAlternateVerbs($request);
if (count($others) > 0) {
return $this->getOtherMethodsRoute($request, $others);
}
throw new NotFoundHttpException();
}
protected function checkForAlternateVerbs($request)
You can see the error more visvually HERE.
What am i doing wrong ?
Instead of
{{ Form::open(array('url'=>'admin/category/create')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
try this:
{{ Form::open(array('route'=>'post.homes')) }}
<p>
{{ Form::label('name') }}
{{ Form::text('name') }}
</p>
{{ Form::submit('Create Category' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
In routes.php:
Route::post('aboutus', array('as' => 'post.homes', 'uses' => 'HomeController#postContactUs'));

viewing my database in the view at laravel

sorry if this is a very newbie Q..
but please help me to solve this problem. plus give me the reason about why this error happened..
this is my edit view
new.blade.php
#section('content')
#include('common.show_error')
{{Form::open(array('url'=>'author/update', 'method'=>'PUT'))}}
<p>
{{ Form::label('name', 'Name: ') }}</br>
{{ Form::text('name', $author->name) }}
</p>
<p>
{{ Form::label('bio', 'Biography: ') }}</br>
{{ Form::textarea('bio', $author->bio) }}
</p>
{{ Form::hidden('id', $author->id) }}
<p>{{ Form::submit('Edit Data') }}</p>
#stop
this is my show view
show.blade.php
#extends('layouts.default')
#section('content')
<h1>{{ $author->name }}</h1>
<p>{{ $author->bio }}</p>
<p>{{ $author->updated_at }}</p>
<span>
{{ HTML::linkRoute('authors', 'Home') }} |
{{ HTML::linkRoute('edit_author', 'Edit', array($author->id)) }} |
{{ Form::open(array('url'=>'author/destroy', 'method'=>'DELETE', 'style'=>'display: inline;')) }}
{{ Form::hidden('id', $author->id) }}
{{ Form::submit('Delete') }}
{{ Form::close() }}
</span>
#stop
this is my controller
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
Member::where('id','=',$id)->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated');
}
}
the case: when I try to edit data using edit button. it said:
"Trying to get property of non-object laravel"
and when I check at the error log. it refers to
<h1>{{ $author->name }}</h1>
public function update($id)
{
$id = Input::get('id');
$validator = Member::validate(Input::all());
if($validator->fails()){
return Redirect::route('members.edit', $id)->withErrors($validator);
} else {
$author = Member::find($id);
$author->update(array(
'name' => Input::get('name'),
'bio' => Input::get('bio')
));
return Redirect::route('members.show', $id)
->with('message', 'Data Succesfully Updated')
->with('author', $author);
}
}
Little changes in your controller, try it :) In your code, you are not send variable "author" into your view.

Laravel 'Form::model' does not populate my edit form

I am in the progress of making a form to edit an existing entry in the database. I am using the Form::model approach to do this, however it doesn't seem to work. The fields just stay empty.
ServerController.php
/**
* Editing servers
*/
public function edit($name)
{
$server = Server::find($name);
$keywords = ($server->getKeywords()) ? $server->getKeywords() : array();
$countries = $this->getCountries();
return View::make('server/edit', array('server' => $server, 'countries' => $countries));
}
public function update($name)
{
$server = Server::find($name);
// Did it succeed?
if($server->save()) {
Session::flash('success', 'You server was edited!');
return Redirect::route('server.view', array($name));
}
// Did not validate
if(Input::get('keywords')) {
$keywords = Input::get('keywords');
Session::flash('keywords', $keywords);
}
Session::flash('danger', "<b>Oops! There were some problems processing your update</b><br/>" . implode("<br/>", $server->errors()->all()));
return Redirect::route('server.edit', array($name))->withInput()->withErrors($server->errors());
}
The Form
{{ Form::model($server, array('route' => array('server.update', $server->name), 'class' => 'form-horizontal', 'role' => 'form', 'files' => true)) }}
<div class="form-group {{ $errors->has('email') ? 'has-error' : '' }}">
{{ Form::label('email', 'Email', array('class' => 'control-label col-md-4')) }}
<div class="col-md-4">
{{ Form::text('email', '', array('class' => 'form-control')) }}
{{ $errors->first('email', '<br/><div class="alert alert-danger">:message</div>') }}
</div>
</div>
(some more fields)
{{ Form::close() }}
The problem here is that you're passing in an empty string as the default field value. As the documentation states here, any explicitly passed values will overrule the model attribute data. Try using null instead of '':
{{ Form::text('email', null, array('class' => 'form-control')) }}

laravel 4 Auth::attempt allways returns true?

Earlier today I had the exact same problem with Auth::attempt always retuning false. I realized that Auth checks for a hashed password, so by doing so I was able to get it to return true, but now it always does. Even if I type asdadfasdfaf in my form, the if statement loads the page. Any suggestions?
Controller:
class userController extends \BaseController
{
public function login()
{
$user = array(
'username' => Input::get('username'),
'password' => Input::get('password')
);
if(Auth::attempt($user))
{
return Redirect::route('home');
}
else
{
return View::make('login');
}
}
}
Form
{{ Form::open(array('url' => 'home' )) }}
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}
The Routes file:
Route::post('home', 'userController#login');
No matter what I enter it always directs me to my "home" page?
The action url should be login
{{ Form::open(array('url' => 'login' )) }}
^^^^ as you used Auth::attempt to this URL
{{ Form::label('username', 'Username: ') }}
{{ Form::text('username') }}
</br>
{{ Form::label('password', 'Password: ') }}
{{ Form::password('password') }}
</br>
{{ Form::submit() }}
{{ Form::close() }}

Categories