Laracasts tutorial: Is method 'Latest()' removed? - php

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

Related

Error: 'The GET method is not supported for this route. Supported methods: PATCH, DELETE.' accessing a route [duplicate]

This question already has answers here:
The POST method is not supported for this route. Supported methods: GET, HEAD. Laravel
(19 answers)
Closed 9 months ago.
I'm working on a Laravel project where I want to click an object (meeting) and see its details but I get this error:
laravel error
This is my home.blade.php file:
<!-- list all the meetings -->
<h3>#lang('Following meetings of '){{ $userName }}</h3>
<div class="row container-fluid">
<div class="row flex-row flex-nowrap position-relative">
#forelse ($meetings as $meeting)
<div class="col-sm-3">
<div class="card card-block zoom">
#csrf
<div class="card-body bg-dark">
{{ $meeting['name'] }}
</div>
</div>
</div>
#empty
<h4>No tienes reuniones programadas.</h4>
#endforelse
</div>
</div>
</div>
My web.php:
// GET
Route::get('/home', [MeetingController::class, 'index'])->name('home');
Route::get('/meeting/{id}', [MeetingController::class, 'show'])->name('meeting.show')->whereNumber('id');
Route::get('/lectures', [LectureController::class, 'index'])->name('lectures');
Route::get('/lectures/{id}', [LectureController::class, 'show'])->name('lectures')->whereNumber('id');
// POST
Route::get('/meeting/create', [MeetingController::class, 'create'])->name('create_meeting');
Route::post('/home', [MeetingController::class, 'store'])->name('meeting.store');
Route::get('/lectures/create', [LectureController::class, 'create'])->name('create_lecture');
Route::post('/lectures', [LectureController::class, 'store'])->name('lecture.store');
// PUT or patch
Route::get('/meeting/edit/{id}', [MeetingController::class, 'edit'])->name('meeting.edit');
Route::patch('/meeting/{id}', [MeetingController::class, 'update'])->name('meeting.update');
// DELETE
Route::delete('/meeting/{id}', [MeetingController::class, 'destroy'])->name('meeting.delete');
And my MeetingController.php
public function show($id)
{
//$meeting = Meeting::find($id);
return $id;
}
I think the meeting id is detected as a string. removes the where() to match it and it's better to use route() to generate a route.
file web.php:
...
Route::get('/meeting/{id}', [MeetingController::class, 'show'])->name('meeting.show')
...
the blade:
{{ $meeting['name'] }}

How to make dynamic #yield in laravel?

I want to make #yield('dynamic') to load different pages like single page application in laravel.
Route:
Route::get(
'/front-office-setup/{setup_type}',
'AdmissionFrontOfficeSetupController#index'
)->name('frontofficesetupview');
Controller:
public function index($setup_type)
{
$data['setup_type'] = $setup_type;
return view('frontoffice::frontofficesetup.frontofficesetup', $data);
}
View:
<div class="row">
<div class="col-md-3">asdf</div>
<div class="col-md-4">asdf</div>
<div class="col-md-5"> #yield('{{$setup_type}}')</div>
</div>
Section:
#extends('frontoffice::frontofficesetup.frontofficesetup')
#section('visitor-purpose')
sdfasd
#endsection
But it doesn't render or show in #yield('{{$setup_type}}')
Is there any way to do this?
Edit part*
Also i have already included a #yield type of things in view file
#extends('backend.master.master')
#section('content')
<div class="row">
<div class="col-md-3">asdf</div>
<div class="col-md-4">asdf</div>
<div class="col-md-5"> #yield($setup_type)</div>
</div>
#endsection
#yield is already started PHP tag <?php. SO no need to mention braces again{{}}. Simply try #yield($setup_type) It will work perfectly.

FatalErrorException Call to a member function except() on null

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');

question mark in the url with laravel

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]) }}

Laravel compact is not working

I'm trying to make a posting system, with a resource routing combination on the posts. When I try to run the app to view the posts, it returns an error stating that the posts could not be found within the view. I have the controller code for the index and the show functions:
public function index()
{
$posts = Post::latest()->get();
return view('view', compact('posts'));
}
public function show(Post $post)
{
return view('posts.show', compact('post'));
}
The view that I have for the app uses the post variable to display the posts:
<div class="container">
<div class="row">
<div class="col-md-6 col-md-offset-2">
<div class="panel panel-default">
<!-- Posts will be displayed on the same panel -->
<div class="panel-body" id="view">
#foreach($posts as $post)
<article id="post">
<a href="/view/posts{{ $post->id }}">
{{ $post->title }}
</a>
<div class="body">
{{ $post->body }}
</div>
<!-- Footer for posts will include interaction features -->
</article>
#endforeach
</div>
</div>
</div>
</div>
</div>
Is there something that the laravel installation isn't doing correctly? Is the compact function set up correctly?
Your controller index function should be something like this:
public function index()
{
$posts = Post::get();
return view('posts.index', compact('posts'));
}
The return of the index action is wrong
return view('view', compact('posts'));
Change 'view' with 'posts'
Use compact('posts')
If you're a beginner checkout the laracasts video series to get a good understanding of the Laravel framework.

Categories