0
votes

when i want to create new post it is return this error Object of class Illuminate\Database\Eloquent\Collection could not be converted to int (View: C:\xampp\htdocs\new-project\resources\views\admin\posts\create.blade.php

public function create() {

    $categories = Category::all('id','name');

    return view('admin.posts.create')->with('categories', $categories);
}

my view is

<div class="row">
    <div class="col-md-8 col-md-offset-2 panel panel-default">
    <h1>ایجاد پست</h1>

    {!! Form::open(['method' => 'POST','action'=>'AdminPostsController@store','files'=>true]) !!}
    <div class="form-group">
        {!! Form::label('title','عنوان:') !!}
        {!! Form::text('title',null,['class'=>'form-control']) !!}
    </div>
    {{--<div class="form-group">--}}
        {!! Form::label('category_id','بخش:') !!}
        {{--{!! Form::select('category_id',[''=>'زیر مجموعه مورد نظر را انتخاب کنید']+$categories,null,['class'=>'form-control']) !!}--}}
        {{--</div>--}}
        {!! Form::select('category_id',[''=>'زیر مجموعه مورد نظر را انتخاب کنید']+$categories,null,['class'=>'form-control']) !!}
    <div class="form-group">
        {!! Form::label('photo_id','عکس:') !!}
        {!! Form::file('photo_id',['class'=>'form-control']) !!}
    </div>
        <div class="form-group">
            {!! Form::label('excerpt','خلاصه:') !!}
            {!! Form::text('excerpt',null,['class'=>'form-control']) !!}
        </div>
    <div class="form-group">
        {!! Form::label('title','متن:') !!}
        {!! Form::textarea('body',null,['class'=>'form-control']) !!}
    </div>
    <div class="form-group">

        {!! Form::submit('ایجاد پست',['class'=>'btn btn-success btn-block']) !!}
    </div>
    {!! Form::close() !!}
        @include('partcial.form-error')
    </div>
</div>



<div>



        <script>
            $(document).ready(function() {
                $('.selection').select2();
            });
        </script>


</div>
1
$categories is a collection (array of objects) and you're trying to add it as a integer in your form select. if you want total number of categories then use $categories->count()Nehal Hasnayeen
i actully use pluck('id',name) for $categories and want to have list of category for selecthussain

1 Answers

0
votes

you can do this:

<select>
    <option value=0 > زیر مجموعه مورد نظر را انتخاب کنید  </option>
    @foreach($categories as $category)
       {
        <option value={{$category->id}} >  {{$category->name}} </option>
        }
</select>