Laravel Отображение / возврат данных в формате JSON вместо отображения / возврата данных в формате HTML - PullRequest
0 голосов
/ 20 марта 2019

У меня странная ситуация, когда данные отображаются в формате JSON вместо HTML!На маршруте http://127.0.0.1:8000/addpost отображается форма для добавления сообщения и недавно добавленное сообщение.

Здесь отображается форма для добавления сообщения, и при нажатии кнопки «Отправить» возвращаются данные в формате json вместо отображения формы ипост добавлен.1) почему он возвращает данные JSON?2) как отобразить данные в формате html?Вот мне маршрут

web.php
Route::get('addpost','PostController@index');
Route::post('addpost','PostController@store');

Контроллер ->

PostController.php 
class PostController extends Controller
{
     public function index()
    {
        $posts = Post::latest()->first();
        return view('addpost',compact('posts'));

    }

    public function store(Request $request)
    {
        $post =new Post();
        $post->title=   $request->input('title');
        $post->body=    $request->input('body');

        if($request->hasfile('postimage'))
        {
            $file=$request->file('postimage');
            $extention=$file->getClientOriginalExtension();
            $filename=time().'.'.$extention;
            $file->move('uploads/post',$filename);
            $post->postimage=$filename;
        }
        else{
            return $request;
            $post->postimage='';
        }

        $post->save();
        return back()->with('success', 'Your images has been successfully Upload'); 
    }

   }

и вид

<div class="container">
        <div class="jumbotron">
            @if(Auth::user())
            <h3> Add new post with images</h3>
            <form action="{{url('addpost')}}" method="POST" enctype="multipart/form-data">
                {{csrf_field()}}
            <div class="form-group">
                <label>Title</label>
                <input type="text" name="title" class="form-control" >
            </div>
            <div class="form-group">
                <label>Body</label>
                <input type="text" name="body" class="form-control" >
            </div>
            <div class="input-group">
                <div class="custom-file">
                    <input type="file" name="image" class="custom-file-input">
                    <label class="custom-file-label">Choose file</label>

                </div>
            </div>
            <button type="submit" name="submit" class="btn-brn-primary"> Save Post</button>
            @if($posts)
            @foreach($posts as $post)
             <tr>
                        <td>{{$post->id}}</td>
                        <td>{{$post->title}}</td>
                        <td>{{$post->body}}</td>
                        <!-- <td><img src="{{asset('uploads/post/'.$post->image)}}" style="height: 150px;"></td>
                        <td><a href="">EDIT</a></td> -->
                    </tr>
            @endforeach
            @endif
            @else
            <h1>no</h1> 
            @endif 
        </form>
        </div>  
    </div>

1 Ответ

1 голос
/ 21 марта 2019
if($request->hasfile('postimage')) // -> you are checking for postimage, in your html 
                                   // markup the file filed is called "image", 
                                   // therefore you will never enter this if block
        {
            $file=$request->file('postimage');
            $extention=$file->getClientOriginalExtension();
            $filename=time().'.'.$extention;
            $file->move('uploads/post',$filename);
            $post->postimage=$filename;
        }
        else{
            return $request; // here you are returning the $request object that 
                             // causes your unformatted response
            $post->postimage='';
        }
...