This is the ArticlesController code
<?php
namespace App\Http\Controllers;
use App\Models\Article;
use Illuminate\Http\Request;
class ArticlesController extends Controller
{
public function index()
{
$articles = Article::latest()->get();
return view('articles.index', ['articles' => $articles]);
}
public function show($id)
{
$article = Article::find($id);
return view('articles.show', ['article' => $article]);
}
}
And this is the web.php code
Route::get('/articles', 'App\Http\Controllers\ArticlesController#index');
and this is the layout.blade.php code
<li class={{Request::path() === 'articles' ? 'current_page_item' : ''}}><a href="/articles"
accesskey="4"
I tried so many different ways and it's working but in laravel 8 it's not.
public function index()
{
$articles = Article::query()->latest()->get();
return view('articles.index', ['articles' => $articles]);
}
Related
I'm having the following error when I try to check a permission using policies:
Too few arguments to function App\Policies\AnswerPolicy::view(), 1 passed in /Users/georgio/Projects/Laravel/municipality-app/vendor/laravel/framework/src/Illuminate/Auth/Access/Gate.php on line 710 and exactly 2 expected (View: /Users/georgio/Projects/Laravel/municipality-app/resources/views/layouts/loggedin.blade.php)
AnswerPolicy
<?php
namespace App\Policies;
use App\Answer;
use App\User;
use Illuminate\Auth\Access\HandlesAuthorization;
class AnswerPolicy
{
use HandlesAuthorization;
public function view(User $user, Answer $answer)
{
return true;
}
public function edit(User $user)
{
return true;
}
public function create(User $user)
{
return true;
}
public function delete(User $user)
{
return true;
}
public function update(User $user)
{
return true;
}
}
AnswersController
<?php
namespace App\Http\Controllers;
use App\Question;
use App\Answer;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class AnswersController extends Controller
{
public function index()
{
$this->authorize('view');
return view('answers.show')->with('questions', Question::all());
}
public function create($questionId)
{
$this->authorize('view-submit-answer');
return view('answers.create')->with('question',Question::find($questionId));
}
public function store()
{
$this->authorize('');
$this->validate(request(),[
'answer' => 'required',
]);
$data = request()->all();
$answer = new Answer();
$answer->answer = $data['answer'];
$answer->question_id = $data['question_id'];
$answer->user_id = Auth::id();
$answer->save();
return redirect('/questions/answers');
}
public function edit($answerId)
{ $this->authorize('view-edit-answer');
return view('answers.edit')->with('answer', Answer::find($answerId));
}
public function update($answerId)
{
$this->validate(request(),[
'answer' => 'required',
]);
$data = request()->all();
$answer = Answer::find($answerId);
$answer->answer = $data['answer'];
$answer->save();
return redirect('/answers-question');
}
public function destroy($questionID)
{
$this->authorize('delete-answer');
$answer = Answer::where('question_id', $questionID)->where('user_id', Auth()->id());
$answer->delete();
return redirect('/questions/answers');
}
}
loggedin.blade.php
This is only the part of my code that is causing the error
#can('view', App\Answer::class)
<li>
<a href="/questions/answers">
<span><i class="fa fa-pencil"></i></span>
<span>Answer Question</span>
</a>
</li>
#endcan
I even tried replacing $answer with App\Answer::class and I still had the exact same error.
You can check the full error stack at:
https://flareapp.io/share/xPQQQXP1#F66
The documentation says
When defining policy methods that will not receive a model instance, such as a create method, it will not receive a model instance. Instead, you should define the method as only expecting the authenticated user
I don't see an Answer instance inside your #can statement there. Does one exist? If so, you should be doing this:
#can('view', $answer)
Or, if one doesn't exist at that point, define your method like this
public function view(User $user)
And call it like this:
#can('view', \App\Answer::class)
I'm trying to make tags filter on my page, but I have an error says Call to a member function links() on null.
Thank you for anything you can do to help.
This is my controller:
<?php
public function index(Request $request)
{
if($request->has('cari')) {
$forums = \App\Forum::where('title','LIKE','%'.$request->cari.'%')->paginate(5);
} elseif($request->has('tags')) {
$tag = Tag::find($request->tags);
$forums = $tag->forums;
} else {
$forums = Forum::paginate(5);
}
$populars = DB::table('forums')
->join('comments','forums.id','=','comments.commentable_id')
->select(DB::raw('count(commentable_id) as count'),'forums.id','forums.title','forums.slug')
->groupBy('id','title','slug')
->orderBy('count','desc')
->take(5)
->get();
return view('forum.index', compact('forums','populars'));
}
And this is my link in view:
<div class="row justify-content-center">
{!! $forums->links() !!}
</div>
This is my forum model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class forum extends Model
{
public function tags()
{
return $this->belongsToMany('App\Tag');
}
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->morphMany('App\Comment', 'commentable');
}
}
I'm trying to pass my article data to the single page article named article.blade.php although all the data are recorded into the database but when I tried to return them in my view, nothing showed and the [ ] was empty. Nothing returned.
this is my articleController.php
<?php
namespace App\Http\Controllers;
use App\Article;
use Illuminate\Http\Request;
class ArticleController extends Controller
{
public function single(Article $article)
{
return $article;
}
}
this is my model:
<?php
namespace App;
use Cviebrock\EloquentSluggable\Sluggable;
use Illuminate\Database\Eloquent\Model;
class Article extends Model
{
use Sluggable;
protected $guarded = [];
protected $casts = [
'images' => 'array'
];
public function sluggable()
{
return [
'slug' => [
'source' => 'title'
]
];
}
public function path()
{
return "/articles/$this->slug";
}
public function comments()
{
return $this->morphMany(Comment::class, 'commentable');
}
}
and this is my Route
Route::get('/articles/{articleSlug}' , 'ArticleController#single');
Change your code to
class ArticleController extends Controller
{
public function single(Article $article)
{
return view('article', compact('article'));
}
}
change route to
Route::get('/articles/{article}' , 'ArticleController#single');
And model
public function getRouteKeyName()
{
return 'slug';
}
See docs https://laravel.com/docs/5.7/routing#route-model-binding
You might not be getting any data because you have not specified that you're using title_slug as the route key for model binding in your model.
Add this to your model class and it should give you the data
public function getRouteKeyName()
{
return 'slug';
}
Then you can return the data in json, view or other format.
Depending on what you try to archive, you need to either ...
return $article->toJson(); // or ->toArray();
.. for json response or ..
return view(..., ['article' => $article])
for passing a the article to a certain view
I'm trying Laravel 5.4 (i usually work with 5.1) and im actually copypasting most of the code, so i dont understand what is the trouble, maybe is because there is a better way to do it but yeah, its been 1 hour and i cant get past this;
Hope you can help me with this..
In case this isn't enough i'll be posting my views and routes. Thank to everyone.
This is my Model:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
protected $fillable = ['user_id', 'user_name', 'user_birthday'];
public static $rules = [
'user_name' => 'required|max:255',
'user_birthday' => 'required'
];
public $timestamps = false;
}
Controller:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class UsersController extends Controller
{
public function index()
{
$users = User::with('user_id')->orderBy('user_id', 'ASC')->paginate(10);
return view('admin.users.index')->with("user", $users);
}
public function create()
{
return view('admin.users.create');
}
public function store(Request $request)
{
$users = new User($request->all());
$users->save();
return redirect()->route('admin.users.index');
}
public function show($id)
{
$users = User::find($id);
}
public function edit($id)
{
$users = User::find($id);
return view('admin.users.edit')->with('user', $user);
}
public function update(Request $request, $id)
{
$users = User::find($id);
$users->user_name = $request->user_name;
$users->user_birthday = $request->user_birthday;
$users->save();
return redirect()->route('admin.users.index');
}
public function destroy($id)
{
$users = User::find($id);
$users->delete();
return redirect()->route('admin.users.index');
}
}
Your error is from the following line of code. When you use with on a model is to load children relationships or sub-models. That is why the application is looking for the relationship user_id in the User Model thinking that it's a sub-model of the User model but it's not, so it return an error.
wrong
$users = User::with('user_id')->orderBy('user_id', 'ASC')->paginate(10);
correct
$users = User::orderBy('user_id', 'ASC')->paginate(10);
I have 2 controllers in laravel 5.2
1) Apiauth controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Api_auth;
class Apiauth extends Controller
{
public function checkauth($reqauthkey)
{
$authkey=Api_auth::orderBy('id', 'desc')->first();
if($authkey->authkey!=$reqauthkey)
return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);
}
}
2) MobileregistrationController
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Controllers\Apiauth;
use App\Http\Requests;
use App\Mobile_registration;
use App\Api_auth;
use App\Http\Requests\CreateMobileRegistrationRequest;
class MobileregistrationController extends Controller
{
public function index(Request $request)
{
App\Http\Controllers\Apiauth->checkauth($request->authkey);
// $authkey=Api_auth::orderBy('id', 'desc')->first();
// if($authkey->authkey!=$request->authkey)
// return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);
$mobileregistration=Mobile_registration::all();
if($mobileregistration->isEmpty())
return response()->json(['response'=>'false','message'=>'No data found','code'=>404],404);
else
return response()->json(['response'=>'true','data'=>$mobileregistration],200);
}
public function show($id,Request $request)
{
$authkey=Api_auth::orderBy('id', 'desc')->first();
if($authkey->authkey!=$request->authkey)
return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);
$mobileregistration=Mobile_registration::find($id);
if(!$mobileregistration)
return response()->json(['response'=>'false','message'=>'No data found','code'=>404],404);
else
return response()->json(['response'=>'true','data'=>$mobileregistration],200);
}
public function store(CreateMobileRegistrationRequest $request)
{
$values =$request->only(['mobile_imei','mobile_number','application_type','version','isverified','reg_date_time','authkey']);
$authkey=Api_auth::orderBy('id', 'desc')->first();
if($authkey->authkey!=$request->authkey)
return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);
Mobile_registration::create($values);
return response()->json(['response'=>'true','message'=>'Values Inserted','code'=>201],201);
}
public function update($id,CreateMobileRegistrationRequest $request)
{
$mobileregistration=Mobile_registration::find($id);
if(!$mobileregistration)
return response()->json(['response'=>'false','message'=>'No matching data found for editing','code'=>404],404);
$authkey=Api_auth::orderBy('id', 'desc')->first();
if($authkey->authkey!=$request->authkey)
return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);
$mobileregistration->mobile_imei=$request->get('mobile_imei');
$mobileregistration->mobile_number=$request->get('mobile_number');
$mobileregistration->application_type=$request->get('application_type');
$mobileregistration->version=$request->get('version');
$mobileregistration->isverified=$request->get('isverified');
$mobileregistration->reg_date_time=$request->get('reg_date_time');
$mobileregistration->save();
return response()->json(['response'=>'true','message'=>'Mobile Registration details updated successfully','code'=>200],200);
}
public function destroy($id,Request $request)
{
$mobileregistration=Mobile_registration::find($id);
if(!$mobileregistration)
return response()->json(['response'=>'false','message'=>'No matching data found for deleting','code'=>404],404);
$authkey=Api_auth::orderBy('id', 'desc')->first();
if($authkey->authkey!=$request->authkey)
return response()->json(['response'=>'false','message'=>'Authentication Failed','code'=>403],403);
$mobileregistration->delete();
return response()->json(['response'=>'true','message'=>'Provided details are deleted sucessfully','code'=>200],200);
}
}
Now in MobileregistrationController, for every function I want to call this function
public function checkauth($reqauthkey){}
of Apiauth controller
But when I used this code to call this function I am gettig error message
App\Http\Controllers\Apiauth->checkauth($request->authkey);
Error message
FatalErrorException in MobileregistrationController.php line 20:
Class 'App\Http\Controllers\App\Http\Controllers\Apiauth' not found
I have searched lot on Stackoverflow for the answers but none of solution worked for me correctly. Someone help me to rectify this.
I have route
Route::get('/test/index', 'TestController#index');
Apiauth with function test()
<?php
namespace App\Http\Controllers;
class Apiauth extends Controller {
public function test() {
return "abc";
}
}
and i have another controller TestController
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Apiauth;
class TestController extends Controller {
public function index() {
$controller = new Apiauth;
return $controller->test();
}
}
call url /test/index it print abc . You should try my anwser and solove your problem