dpmopn8542 2017-04-17 21:11
浏览 25
已采纳

用户提交的帖子使用Laravel存储功能

So in addition to my prior topic (thank you guys for helping) routes with compact

I am now facing troubles with my store function, I am getting the error : "Trying to get property of non-object.

This is my store function in my Controller

    public function store(Request $request)
{
    // validate the data
    $this->validate($request, array(
            'title' => 'required|max:255',
            'body'  => 'required'
        ));
    // store in the database
    $userpost = new Usp;
    $userpost->title = $request->title;
    $userpost->body = $request->body;
    $userpost->save();
    Session::flash('success', 'The blog post was successfully saved!');
    return redirect()->route('admin.userposts.show', $userpost->id);
}

This is the view to create userpost(to make it more clear, p.s. the same form with different route ofcourse, works for my simple posts but not for my user submitted posts)

@extends('index')
@section('index-stylesheets')

    {!! Html::style('css/parsley.css') !!}

@endsection
@section('content')
            <h1>Create New User Post</h1>
            <hr>

            {!! Form::open(array('route' => 'admin.userposts.store', 'data-parsley-validate' => '')) !!}
                {{ Form::label('title', 'Title:') }}
                {{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255')) }}

                {{ Form::label('body', "Post Body:") }}
                {{ Form::textarea('body', null, array('class' => 'form-control', 'required' => '')) }}

                {{ Form::submit('Create Post', array('class' => 'btn btn-success btn-lg btn-block', 'style' => 'margin-top: 20px;')) }}
            {!! Form::close() !!}
@endsection

@section('index-scripts')

    {!! Html::script('js/parsley.min.js') !!}

@endsection

Method of showing the post:

    public function show($id)
{
    $userpost = Usp::find($id);
    return view('admin.userposts.show', compact('userpost'));
}
  • 写回答

1条回答 默认 最新

  • douke3442 2017-04-17 21:23
    关注

    So the fact is that the problem was not the store method but the routes.

    Route::get('/userposts/{id}', 'UserPostsController@show')->name('admin.userposts.show'); 
    Route::get('/userposts/create', 'UserPostsController@create')->name('admin.userposts.create'); 
    Route::post('/userposts/store', 'UserPostsController@store')->name('admin.userposts.store');
    

    When registering the routes in that order, when laravel will iterate over your routes, it will first encounter the show one, and it will therefore take "create" as the id. Therefore, it will go into the show method and it won't find any post that matches, the post being null, you get the error.

    So, there are two ways of fixing this.

    The first one (the easiest, works in all cases, maybe not the best) is to put the create route before the show route.

    The second one, the best in my opinion, is to add a condition to the id (doesn't work in the case of a slug). As the ids are only integers, you get :

    Route::get('/userposts/{id}', 'UserPostsController@show')->name('admin.userposts.show')->where('id', '[0-9]+');
    

    Therefore, create won't match the regular expression and it won't go in the show method.


    For "resource creations" (storing in database), I wouldn't use a "field-by-field" method.

    Instead, I'd do something like this :

    $userpost = Usp::create($request->only('title', 'body'));
    

    I feel this is more talkative.

    But, it won't work, laravel protects* us against such things. To make it work, you have two options.

    1. (The best option in my opinion)
      In your model, add a protected variable called $fillable with all your columns that you allow to mass assign*. In this case you would put :

      protected $fillable = ['name'];

    2. (The option if you are sure of what are you doing) In your model, you can say, hey, I know what I'm doing here, just let me do my stuff without guarding me. In this case you would put :

      protected $guarded = [];

    Notes :

    1. $request->only('field1', ...) gives you an array of the fields that you want with the fields name as keys, in this case it gives you ['field1' => $request->field1]. In your case it will give you ['title' => $request->title, 'body' => $request->body].

    2. Mass assignment is when you give an array to the model and it puts all attributes to the fields of the array. More informations here https://laravel.com/docs/5.4/eloquent#mass-assignment

    3. When I mean laravel protects us against those things, it does't really protect us because it isn't a bad practice (instead, I find it more readable), but because it does allow you to make mistakes (for exemple, setting fields that don't exist).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了