I am building a QA, and when i try to show a question, I am getting this error. FatalErrorException Call to a member function except() on null in web.php line 10
Here is the web.php code
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('questions', 'QuestionsController')->except('show');
Route::get('/questions/{slug}', 'QuestionsController#show')->name('questions.show');
in the QuestionsController.php file the show function is
public function show(Question $question)
{
$question->increment('views');
return view('questions.show', compact('question'));
}
in addition, here is the view part when the question is displayed
<div class="card">
<div class="card-header">
<div class="d-flex align-items-center">
<h>{{ $question->title }} </h1>
<div class="ml-auto">
Back to all Questions
</div>
</div>
</div>
<div class="card-body">
{{ !! $question->body_html !! }}
</div>
</div>
so if my question is clear, how can I get rid of this error? Thank you for your help, Sirs!
Try to below code:
Route::resource('questions', 'QuestionsController', ['except' => ['show']]);
i think so it is because you should use find() or something elese first.
for example you should find question by id or slug or anything, then you can increment the field you want.
for example:
$question = Question::find(5)->increment('views');
or
$question = Question::where('url', '=', 'some value')->increment('views');
Correct your route. by following this sample code. Here is the official document you can check it [https://laravel.com/docs/5.4/controllers#resource-controllers]
Route::resource('questions', 'QuestionsController')->except(['show']);
Route::get('/questions/{slug}', 'QuestionsController#show')->name('questions.show');
Related
I'm making a "teacher's" app, and I want to make a log-in page which changes depending if there's registered users in the database or not.
I want to make a redirection button to a create user page if there aren't auth users in database, and to make a select user view if the database have one or more users.
The problem is that I don't know how to exactly do this, 'cause the view always shows me the first statement (what I've got in the if), also if in the database are registered users. Can anyone help me with this please?
This is the blade file:
#if (empty(Auth::user()->id))
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
Here you have the controller index method:
public function index()
{
$users = User::all();
return view('/', compact('users'));
}
And finally here you have the page:
The following code is the sample for it, kindly replace code accordingly
#if(!$user)
//show button
#else
//dont show button
#endif
I think your question is you want to check if there is user in database.
So no need to check if the user authenticated but to check if there is user on the database.
In your controller
public function index() {
return view('/', ['users' => User::all()]);
}
and in your blade file
#if(!$users)
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Welcome</h1>
<p>We see there aren't users</p>
</div>
<div id="loginForm">
<button type="button" onclick="window.location='{{ url("/newUser") }}'">Button</button>
</div>
</div>
#else
<div class="grid-item" id="grid-item5">
<div id="title">
<h1>Select an user</h1>
</div>
<div id="loginForm"></div>
</div>
#endif
This function will get the current authenticated user: Auth::user(). I guess what you are trying to achieve is #if(empty($users)) where $users is the variable you are passing on controller.
If you want to verify if the user that accessed to that view is authenticated you can simply use #auth and #guest.
Also i would suggest you to change your button to an <a> tag and your href would be <a href="{{ route('route.name') }}" where route.name would be defined in your routes file.
in your controller:
you can create a folder inside views called users and then the index.blade.php (views/users/index.blade.php)
public function index()
{
$users = Users::all();
return view('users.index')->with('users', $users);
}
in your view:
#if(count($users) < 1)
...
#else
...
#endif
count is validating if the $users array length is less then 1 (in other words if the array is empty).
Alternative you can you isEmpty()
#if($users->isEmpty())
...
#else
...
#endif
`i am having a problem with my show.blade.php template, everything works fine but when I click on a post in the index page it directs me to the /post/1 page without showing the post content only the extended layout. please help
Web.php
Route:: resource('best-practices' , 'BestpracticesController');
*bestpracticescontroller.php
public function index()
{
$bestpractices = Bestpractices::all();
return view('bp.index',compact('bestpractices'));
}
public function show(Bestpractices $bestpractices)
{
return view('bp.show',compact('bestpractices'));
}
bp.show view template
#extends('layouts.front')
#section('content')
<div class="blog-details pt-95 pb-100">
<div class="container">
<div class="row">
<div class="col-12">
<div class="blog-details-info">
<div class="blog-meta">
<ul>
<li>{{$bestpractices->Date}}</li>
</ul>
</div>
<h3>{{$bestpractices->title}} </h3>
<img src="{{asset('storage/'.$bestpractices->cover_img)}}" alt="">
<div class="blog-feature">
{{$bestpractices->body}}
</div>
</div>
</div>
</div>
</div>
</div>
#endsection
Thats because when you register routes via
Route::resource('best-parctices', BestparcticeController');
//Generated show route is equivalent to
Route::get(
'/best-practices/{best_practice}',
[BestpracticeController::class, 'show']
);
//route parameter is best_practice
Hence to achieve implicit route model binding the route parameter name must match the parameter name in the controller method
public function show(Bestpractices $bestpractices)
{
//here $bestpractices will be an int and not an object with
//model record as implicit route model binding doesn't work
return view('bp.show',compact('bestpractices'));
}
public function show(Bestpractices $best_practice)
{
//here implicit route model binding works so $best_practices is an object
//with model record
return view('bp.show',['bestpractices' => $best_practices]);
}
Or if you don't want to change the method parameter name in the controller methods then you need to override the route parameter name in the Route:resource() call when you define routes
Route::resource('best-practices', BestpracticesController::class)
->parameters([
'best-practices' => 'bestpractices'
]);
Laravel docs: https://laravel.com/docs/8.x/controllers#restful-naming-resource-route-parameters
i'm learning laravel for now , i'm trying to build a crud application how i got the url with a question mark how i can remove it from the url
the url that i got is like ..../blogs?1
here is the view
#extends ('layouts.app')
#section('content')
<div class="row">
#foreach($blogs as $blog)
<div class="col-md-6">
<div class="card">
<div class="card-header">
{{$blog -> title}}
</div>
<div class="card-body">
{{$blog->content}}
</div>
</div>
</div>
</div>
#endforeach
#endsection
<?php
Route::get('/', function () {
return view('welcome');
});
Route::name('blogs_path')->get('/blogs','BlogController#index');
Route::name('create_blog_path')->get('/blogs/create','BlogController#create');
Route::name('store_blog_path')->post('/blogs','BlogController#store');
Route::name('blogs_path1')->get('/blogs/{id}','BlogController#show');
Route::name('edit_blog_path')->get('/blogs/{id}/edit','BlogController#edit');
how can i fix this , thank you in advance
Because the second argument in route('blogs_path', $blog->id) is parameter.
try this:
Routes:
Route::name('blogs_path')->get('/blogs/{id}/','BlogController#index');
Controller:
public function index(Request $request, $id)
{
...
}
You made a mistake in the routing of the template Blade.
{{ route('blogs_path1', ['id' => $blog->id]) }}
Is the method "latest" used in Controllers removed in newest version of Laravel?
In PHP Storm I get follow error: Method latest() not found in App/Thread.
public function index()
{
//
$threads = Thread::latest()->get();
return view('threads.index', compact('threads'));
}
I'm following a LaraCasts tutorial, and browsing to said page gives me following error. -> forum.test/threads.
ErrorException (E_ERROR)
Method Illuminate\Database\Query\Builder::path does not exist. (View: D:\xampp\htdocs\forum\resources\views\threads\index.blade.php)
As per requested, my view: it is in resources/views/threads/index.blade.php
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading">Forum Threads</div>
<div class="panel-body">
#foreach ($threads as $thread)
<article>
<h4>
<a href="{{ $thread->path() }}">
{{ $thread->title }}
</a>
</h4>
<div class="body">{{ $thread->body }}</div>
</article>
<hr/>
#endforeach
</div>
</div>
</div>
</div>
</div>
#endsection
Also, my routes.
<?php
Route::get('/', function () {
return view('welcome');
});
Route::resource('threads', 'ThreadController');
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
The error is not related to the code you posted. Method Illuminate\Database\Query\Builder::path does not exist.. You are calling somewhere path method which does not exist.
To answer your question, method latest() is still present in the (currently) newest version of Laravel 5.6:
https://laravel.com/api/5.6/Illuminate/Database/Query/Builder.html#method_latest
My guess would be you have an incorrect config of the Thread model relationships. Most probably you did not define path() relationship.
See this answer to similar question: https://stackoverflow.com/a/37934093/1885946
I am trying to update my data, but i keep getting this error message:
"Trying to get property of non-object (View: C:\xampp\htdocs\blog\resources\views\update.blade.php)".
This is my update.blade.php file
#extends ('layout')
#section ('title')
Update page
#stop
#section ('content')
<div class="row">
<div class="col-lg-12">
<form action="/todo/save" method="post">
{{ csrf_field() }}
<input type="text" class="form-control input-lg" name="todo"
value="{{ $todo->todo }}" placeholder="Type in to create a new todo">
</form>
</div>
</div>
<hr>
#foreach ($todo as $todo)
{{ $todo->todo }} <a href="{{ route('todo.update', ['id' =>$todo->id])
}}" class="btn btn-info btn-sm">Update</a> <a href="{{ route('todo.delete',
['id' =>$todo->id]) }}"class="btn btn-danger">Delete</a>
<hr>
#endforeach
#stop
my controller:
public function update($id){
//dd($id);
$todo = Todo::find($id);
return view('update')->with('todo', $todo);
}
and finally my update route:
Route::get('/todo/update/{id}', 'TodosController#update')-
>name('todo.update');
This is just some basic stuff, but im stuck in here for couple of hours now, and any help is highly appreciated!
Use findOrFail method on your controller to throw an Exception if $todo is empty
public function update($id){
$todo = Todo::findOrFail($id);
return view('update', compact('todo'));
}
The problem is also on your update.blade.php file. foreach $todo as todo, $todo has collection of eloquent model or eloquent model ? I think it's a eloquent model. So a loop doesnt have any sense.
That error would happen if todo were FALSE or some other non-object.
You can inspect it with a var_dump($todo);die(); in the controller.
find() will return either null or the model that is found. Assuming that the model is found, you're doing a for each on that model (foreach ($todo...)), which will iterate over the model's public properties. This is obviously not what you intend to do.
It looks to me like you're trying to loop over a list of your todos and print out edit/delete links. If this is the case, you need to get the list of your todos in your controller, pass it into your view, and fix your foreach statement.
Controller:
$todos = Todo::get();
// pass to view
View:
foreach ($todos as $todo)