Laravel - 尝试获取非对象形式错误的属性

时间:2017-06-05 20:25:40

标签: php mysql laravel

我正试图找到一个错误,但我不能有人可能会帮助我。这是应该将数据传递给mysql的普通形式。我添加“create.blade”,它给了我这个错误“试图得到非对象的属性”。我真的不知道出了什么问题。谢谢你的帮助

路线是资源:

Route::resource('users', 'UsersController');

控制器:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

use Carbon\Carbon;

use Illuminate\Support\Facades\Input;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $users = User::all();
        $dt = Carbon::today();
        $ends = $dt->addDays(30);

        return view ('users.index', ['users'=>$users, 'dt'=>$dt, 'ends'=>$ends,]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view ('users.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
      $this->validate($request, [

       'name' => 'required',
       'email' => 'required',
    ]);
    $user = new User();

     $user->name = $request->name;
     $user->email = $request->phone;


  $user->save();

    return redirect()->route('users')

                    ->with('success','Pilot dodany poprawnie');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}

创建视图:

{!! Form::open(array('route' => 'users.store','method'=>'POST')) !!}
    <form>
    <div class="form-group">
      {{ Form::label('name', 'Name', array('class' => 'pull-left labelfont')) }}
      {{ Form::text('name',null, array('class' => 'form-control')) }}

    </div>
    <div class="form-group">
      {{ Form::label('email', 'Email', array('class' => 'pull-left labelfont')) }}
      {{ Form::email('email',null, array('class' => 'form-control')) }}
    </div>
    <div class="form-group">
      <label for="exampleInputEmail1" class="pull-left labelfont">Funkcja</label>
      <select class="form-control">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
</select>
    </div>
    <div class="form-group">
      <label for="exampleInputEmail1" class="pull-left labelfont">Opłata ważna do</label>
      <input type="date" class="form-control" id="exampleInputEmail1" placeholder="Opłata">
    </div>
    <button class="btn m-b-xs btn-sm btn-primary btn-addon pull-right"> Dodaj </button>

    <!--  {{ Form::submit('Dodaj', array('class' => 'btn m-b-xs btn-sm btn-primary btn-addon pull-right')) }} -->

  </form>
  {{ Form::close() }}

1 个答案:

答案 0 :(得分:0)

就像你在评论中所说的那样。 {{ Auth::user()->name }}崩溃的行。这意味着您尚未登录。请尝试这样做。

如果您想在用户未登录时显示创建表单(以及使用该行的导航),请将代码更改为以下内容:

@if(Auth::check())
    <span class="hidden-sm hidden-md">{{Auth::user()->name}}</span> <b class="caret"></b>
@endif

或者适合您设计的东西。