storing image in a existing table column and display it using laravel 4

馋奶兔 提交于 2020-01-05 08:33:25

问题


I would like to store image in a existing table. How can i do that in laravel 4 .I also want to display that image in my website.How i tried it always shows Call to undefined method Symfony\Component\HttpFoundation\File\UploadedFile::save() .

//my show blade
@extends ('layouts.default')

@section('content')

@foreach($posts as $post)

<div >
<div class="media col-md-8">
<a href="#" class="pull-left paddingTop ">
{{ HTML::image($post->image($destinationPath) }}
</a>
<div>
<h3><a href="#"><strong>{{$post->ad_title}}</strong></a><strong class="pull-right">{{ $post->price}} Tk</strong></h3>

     </div>
     <div>
        <h5>{{ $post->description}}</h5>
        </div>
</div>
</div>

@endforeach


@stop    

this is my table storing method

public function store()
{

    $post=new Post;
    $post->user_id=Auth::id();
    $post->category=Input::get('category');
    $post->subcategory=Input::get('subcategory');
    $post->add_type=Input::get('add_type');
    $post->brand=Input::get('brand');
    $post->screen_type=Input::get('screen_type');
    $post->condition=Input::get('condition');
    $post->ad_title=Input::get('ad_title');
    $post->fuel=Input::get('fuel');
    $post->transmission=Input::get('transmission');
    $post->registration_year=Input::get('registration_year');
    $post->engine_capacity=Input::get('engine_capacity');
    $post->mileage=Input::get('mileage');
    $post->description=Input::get('description');
    $post->price=Input::get('price');
    $post->mobile=Input::get('mobile');
    $post->ad_title=Input::get('ad_title');
    $post->image = Input::file('image');    
    //$post->image=Input::get('image');
    $destinationPath = public_path() . '/images/';
    $post->image->save($destinationPath);   
    $post->area=Input::get('area');
    $post->save();
    return 'your ad has been published :)';
}    

my image input form

<div>{{ Form::file('image', ['class' => 'form-group']) }}</div>

回答1:


Edit store function like below

public function store()
{
    $post=new Post;
    $post->user_id=Auth::id();
    $post->category=Input::get('category');
    $post->subcategory=Input::get('subcategory');
    $post->add_type=Input::get('add_type');
    $post->brand=Input::get('brand');
    $post->screen_type=Input::get('screen_type');
    $post->condition=Input::get('condition');
    $post->ad_title=Input::get('ad_title');
    $post->fuel=Input::get('fuel');
    $post->transmission=Input::get('transmission');
    $post->registration_year=Input::get('registration_year');
    $post->engine_capacity=Input::get('engine_capacity');
    $post->mileage=Input::get('mileage');
    $post->description=Input::get('description');
    $post->price=Input::get('price');
    $post->mobile=Input::get('mobile');
    $post->ad_title=Input::get('ad_title');

    $name = Input::file('image')->getClientOriginalName();
    Input::file('image')->move('public/images',$name);
    $destinationPath = '/images/'.$name;

    $post->image=$destinationPath;  
    $post->area=Input::get('area');
    $post->save();
    return 'your ad has been published :)';
}

and edit blade template like below

@extends ('layouts.default')

@section('content')

@foreach($posts as $post)

<div clss="row">
<div class="media col-lg-8">
<a href="#" class="pull-left paddingTop ">
{{ HTML::image($post->image) }} 
 </a>
 <div>        
 <h3><a href="#"><strong>{{$post->ad_title}}</strong</a><strong      
     class="pull-right">{{ $post->price}} Tk</strong></h3>

     </div>
     <div>
        <h5>{{ $post->description}}</h5>
        </div>
</div>
</div>
@endforeach
<div class="">{{$posts->links()}}</div>



@stop


来源:https://stackoverflow.com/questions/28906118/storing-image-in-a-existing-table-column-and-display-it-using-laravel-4

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!