lundi 8 mars 2021

Error: $comments = $post->comments()->paginate( env( 'COMMENTS_PER_PAGE' ) );

i am trying to make access on link http://localhost/newsapp_api/public/api/comments/posts/167 ,after this this error shows me. error in this line: $comments = $post->comments()->paginate( env( 'COMMENTS_PER_PAGE' ) ); after this this error shows me.

error in this line: $comments = $post->comments()->paginate( env( 'COMMENTS_PER_PAGE' ) ); after this this error shows me.

**api.php**

<?php
namespace App\Database\routes\api;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Illuminate\Routing\Controller;
use App\Http\Controllers\Api;

Route::get( 'posts' , 'App\Http\Controllers\Api\PostController@index' );
Route::get( 'posts/{id}' , 'App\Http\Controllers\Api\PostController@show' );
Route::get( 'comments/posts/{id}' , 'App\Http\Controllers\Api\PostController@comments');

**PostController** 
<?php
namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;



use App\Http\Resources\CategoriesResource;
use App\Http\Resources\CommentsResource;
use App\Http\Resources\PostResource;
use App\Http\Resources\PostsResource;


use App\Models\Post;

class PostController extends Controller
{

    public function index()
    {   $posts = Post::paginate( env('POSTS_PER_PAGE') );
        return new PostsResource( $posts );
    }
    public function store(Request $request) {}
    public function show($id)
    {        return Post::whereId($id)->first();  }

    public function update(Request $request, $id) {   }

    public function destroy($id){ }
    public function comments( $id ){
        // $post = Post::find( $id );
        $post = Post::whereId($id)->first();
        $comments = $post->comments()->paginate( env( 'COMMENTS_PER_PAGE' ) );
        return new CommentsResource( $comments );
    }
}
**Post.php**
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class post extends Model
{use HasFactory;
    protected $fillable = [
        'title' , 'content' , 'date_written' ,
        'featured_image' , 'votes_up' , 'votes_down' ,
        'user_id' , 'category_id'
    ];

    public function author(){
        return $this->belongsTo( User::class );
    }

    public function comments(){
        return $this->hasMany( Comment::class );
    }

    public function category(){
        return $this->belongsTo( Category::class );
    }
}


from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/3t0Vmnd
via IFTTT

Aucun commentaire:

Enregistrer un commentaire