Having a strange problem with my laravel 5.2 build, I'm trying to send data from a form to my database and for some reason an error keeps being thrown on submit which states "Argument 1 passed to App\Http\Controllers\PostsController::postStatus() must be of the type array, none given" and I'm not sure why as I am sending data into an array. My code is shown below.
timeline.blade.php @extends('layouts.app')
@section('content')
<div class="container-fluid">
<div class="row">
<div class="col-md-2">
<div class="row notifications-section">
<div class="col-md-4">
<div class="dropdown">
<a class="dropdown-toggle" type="button" data-toggle="dropdown">
<i class="fa fa-caret-square-o-down"></i></a>
<ul class="dropdown-menu">
<li><a href="#">option 1</a></li>
</ul>
</div>
</div>
<div class="col-md-4">
<div class="dropdown">
<a class="dropdown-toggle" type="button" data-toggle="dropdown">
<i class="fa fa-comments"></i></a>
<ul class="dropdown-menu">
<li><a href="#">option 1</a></li>
</ul>
</div>
</div>
<div class="col-md-4">
<div class="dropdown">
<a class="dropdown-toggle" type="button" data-toggle="dropdown">
<i class="fa fa-users"></i></a>
<ul class="dropdown-menu">
<li><a href="#">option 1</a></li>
</ul>
</div>
</div>
</div>
</div>
<div class="col-md-5">
<form role="form" method="POST" action="{{ url('/home') }}" class="facebook-share-box">
{!! csrf_field() !!}
<div class="share">
<div class="panel panel-default">
<div class="panel-body">
<div class="">
<input type="hidden" name="user_name" value="{{ Auth::user()->firstName }} {{ Auth::user()->lastName }}">
<textarea name="body" cols="40" rows="10" id="status_message" value="{{ old('body') }}" class="form-control message" style="height: 62px; overflow: hidden;" placeholder="What's on your mind ?"></textarea>
</div>
<hr>
<div class="row">
<div class="col-md-7">
<div class="form-group">
<div class="btn-group">
<button type="button" class="btn btn-default"><i class="icon icon-map-marker"></i> Location</button>
<button type="button" class="btn btn-default"><i class="icon icon-picture" ></i> Photo</button>
</div>
</div>
</div>
<div class="col-md-5">
<div class="form-group">
<div class="row">
<div class="col-md-8">
<select name="visibility" class="form-control privacy-dropdown pull-left input-sm" value="{{ old('visibility') }}">
<option value="1" selected="selected">Public</option>
<option value="2">Only my friends</option>
<option value="3">Only me</option>
</select>
</div>
<div class="col-md-3">
<input type="submit" name="submit" class="btn btn-primary btn-small">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</form>
</div>
<div class="col-md-4 profile-section">
<div class="row">
<div class="col-md-8">
<h2>{{ Auth::user()->firstName }} {{ Auth::user()->lastName }}</h2>
<h4>{{ Auth::user()->currentLocation }}</h4>
<p>{{ Auth::user()->bio }}</p>
</div>
<div class="col-md-4">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-2 connect-section">
<div class="row">
<div class="col-md-4">Section</div>
<div class="col-md-4">Section</div>
<div class="col-md-4">Section</div>
</div>
</div>
<div class="col-md-9 posts-section">
<div class="row">
@foreach($posts as $post)
<div class="col-md-4">
<h2>{{ $post->user_name }}</h2>
{{ $post->created_at }}<br />
{{ $post->body }}
{{ $post->visibility }}<br />
<div class="row like_comment_share">
<div class="col-md-4"><a href="#">Like</a></div>
<div class="col-md-4"><a href="#">Comment</a></div>
<div class="col-md-4"><a href="#">Share</a></div>
</div>
</div>
@endforeach
</div>
</div>
</div>
</div>
@endsection
PostController.php
<?php
namespace App\Http\Controllers;
use App\Posts;
use App\Http\Controllers\Controller;
use Illuminate\Routing\Controller as BaseController;
class PostsController extends BaseController {
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
}
public function display()
{
return view('users/timeline')
->with('user_name', 'body', 'photo', 'visibility', 'created_at')
->with('posts', Posts::all());
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
protected function postStatus(array $data)
{
return Posts::create([
'user_name' => $data['user_name'],
'body' => $data['body'],
'photo' => $data['photo'],
'visibility' => $data['visibility'],
]);
}
Post.php
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Posts extends Model
{
protected $fillable = [
'user_name', 'body', 'photo', 'visibility', 'created_at',
];
}
No data is passed to the controller when you post to a route. Instead, you access the data through the Request
object by injecting it into the method, or by using the Request::
or Input::
facades.
protected function postStatus(Illuminate\Http\Request $request)
{
$data = $request->all();
return Posts::create([
'user_name' => $data['user_name'],
'body' => $data['body'],
'photo' => $data['photo'],
'visibility' => $data['visibility'],
]);
}