question mark in the url with laravel - php

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

Related

laravel view doesn't show data after adding new route

It works really good, but when I've created new page something goes wrong and now my blog view doesn't show data, but blogs view still correctly show data . I am trying to show detailed data of each blog when user click on button "Details"
MainController:
public function blog(Blogs $blog)
{
return view('blog', compact('blog'));
}
public function blogs()
{
return view('blogs',['blogs' => Blogs::all(),]);
}
blogs.blade.php:
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<div class="row">
#foreach($blogs as $blog)
#include('layouts.cardBlog', compact('blog'))
#endforeach
</div>
#endsection
and cardBlog.blade.php:
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{{($blog->image) }}">
<div class="caption">
<h3>{{ $blog->title }}</h3>
<p>{{ $blog->body }}</p>
<p>
<a href="{{route('blog', $blog->id) }}"
class="btn btn-default"
role="button">#lang('main.more')</a>
#csrf
</p>
</div>
</div>
</div>
blog.blade.php
#extends('layouts.master')
#section('title', __('main.blogs'))
#section('content')
<h1>{{ $blog->title}}</h1>
<img src="{{$blog->image }}">
<p>{{ $blog->body }}</p>
#endsection
web.php:
Route::get('/', 'MainController#index')->name('index');
Route::get('/categories', 'MainController#categories')->name('categories');
Route::get('/about', 'MainController#aboutus')->name('about');
Route::get('/contact-us', 'ContactUSController#contactUS')->name('contact-us');
Route::post('contactus', ['as'=>'contactus.store','uses'=>'ContactUSController#contactSaveData']);
Route::get('/contacts', 'MainController#contacts')->name('contacts');
Route::get('/blogs', 'MainController#blogs')->name('blogs');
Route::get('/blog/{id}', 'MainController#blog')->name('blog');
Route::get('/intership', 'MainController#intership')->name('intership');
Route::get('/{category}', 'MainController#category')->name('category');
Route::get('/{category}/{product}/{skus}', 'MainController#sku')->name('sku');
Route::post('subscription/{skus}', 'MainController#subscribe')->name('subscription');
What's is the error that it shown?
I suggest use "compact" in this line code
return view('blogs',['blogs' => Blogs::all(),]);
Something like this
public function blogs()
{
$blogs = Blogs::all();
return view('blogs',compact('blogs'));
}
Try this
public function blog($id)
{
$blog = Blogs::findOrFail($id)
return view('blog', compact('blog'));
}

What can cause a laravel blade template to not be processed?

I'm working with Laravel 5.1
I have a template for a page that was previously working fine. I could hit my route and the page would be rendered as expected. At some point the page started just displaying the text contained in the blade template rather than rendering my page. To my knowledge nothing had changed in my routes file, controller or blade. Is there anything that is known to cause this to happen? I've tried updating permissions, using a different method to call the view and creating a new view file all together.
The route:
Route::group(['prefix' => '/admin'], function() {
Route::group(['prefix' => '/reactor-rules'], function() {
Route::get('/visual-rules/{track_id?}', [
'as' => 'broker_visual_reactor_rules',
'uses' => 'ReactorRulesController#visualRules'
]);
});
});
ReactorRulesController#visualRules:
public function visualRules(IReactorTrackDAO $reactorTrackDAO, $id = 0)
{
$export = new ReactorExport();
$tracks = $export->getReactorTracks();
if ($id == 0) {
$eventsArray = [];
}
else {
$eventsArray = $export->getArrayForTrack($id);
}
return $this->renderView('enterpriseBroker::reactor-rules.visual-rules', compact('eventsArray','tracks','reactorTrackDAO'));
}
My blade:
#extends($layout)
{{ SEO::setPageTitle(trans('enterpriseBroker::reactorRules.title'), false) }}
#section('content')
<br>
<br>
#if(empty($eventsArray))
<p>Please select a track below to view/export rules.</p>
#foreach($tracks as $track)
{{$track->name}}<br>
#endforeach
#else
#foreach($eventsArray as $track => $events)
<h4>{{$track}}</h4>
<a href="{{url().'/admin/visual-rules/'.$reactorTrackDAO->findByName($track)->id.'/download'}}" class="btn btn-primary" download>Export</a><br>
#foreach($events as $id => $event)
#if (count($event['rules']) > 0)
<div id="{{$track.'-'.str_replace(' ','_',$event['name'])}}">
<div class="col-lg-12">
<hr/>
<div class="col-lg-2">
<h4>{{$event['name']}}</h4>
</div>
<div class="col-lg-10 border border-primary">
#foreach($event['rules'] as $ruleId => $rule)
<h5>{{$rule['name']}}</h5>
<div class="col-lg-6">
<h6>Conditions to be met:</h6>
#foreach($rule['conditions'] as $condition)
<p>{{$condition}}</p>
#endforeach
</div>
<div class="col-lg-6">
<h6>Actions to run:</h6>
#foreach($rule['actions'] as $action)
<p>{!!$action!!}</p>
#endforeach
</div>
#endforeach
</div>
</div>
</div>
#endif
#endforeach
#endforeach
#endif
#endsection
#section('scripts')
#endsection
This is how it originally appeared:
This is how it currently appears:
It seems to be error with $layout
Try to test it using a hard coded value as below:
#extends('layouts.app')
and run below command
php artisan view:clear
I resolved the issue by creating a new blade file, pasting the contents of the old one in and pointing my controller to the new view. I'm not sure how this resolved the issue.

How to Pass Data From One View to Another in Laravel?

I am quite new to Laravel
I have two views
Book
Read
The Book View displays a single book
<section class="cont-readingone">
<div class="container">
<div class="row row-grid">
<div class="col-md-6">
<div class="row">
<div class="col-md-6">
<div class="cont-reading-image">
<img src="{{ $book->image_url }}" alt="trending image" />
</div>
</div>
<div class="col-md-6">
<div class="out-box">
<h2>{{ $book->name }}</h2>
<h3>{{ $book->author->name }}</h3>
<br>
Start Reading<br><br>
<img src="\images\cart-buy.png" width="13px"/> Buy
</div>
</div>
</div>
</div>
In my controller, I was able to achieve it using
public function show(Book $book) {
$relatedBooks = Book::where('author_id', $book->author_id)
->where('id', '!=', $book->id)
->get();
return view('book')->with('book', $book)->with('relatedBooks', $relatedBooks);
}
In my web.php
Route::get('/books/{book}', [BooksController::class, 'show'])->name('book');
What I am trying to achieve is that, when I click Start Reading on
the Single Book Page, it takes me to another view page (Read) but it takes the book id that I clicked.
In the Read View I have this code,
<script>
"use strict";
document.onreadystatechange = function () {
if (document.readyState == "complete") {
window.reader = ePubReader("{{ $book->epub_url }}", {
restore: true
});
}
};
</script>
My problem is that I don't know how to take the id of the book that I
click and Pass it to the Read View
I will be glad if someone can explain the logic to me as I am confused.
To do via POST
//Book View
//change Start Reading to
<form action="{{ route("your.route.to.read") }}" method="POST">
#csrf
<input name="book_id " value ={{$book->id}} hidden>
<button type="submit">Start Reading</button>
</form>
//your Route will be
Route::get('/read',YourReadController#yourFunction)->name('your.route.to.read');
//your controller will be
public function yourFunction(Request $request)
{
//book id is in $$request->book_id
//your operation here
return view('read')->with('data',$dataYouWantToSend);
}
To do via GET
//Book View
//change Start Reading<br><br> to
Start Reading
//route for get will be
Route::get('/read/{book_id}',YourReadController#yourFunction)->name('your.route.to.read');
//your countroller will be
public function yourFunction($book_id)
{
//book id is in $book_id
//your operation here
return view('read')->with('data',$dataYouWantToSend);
}

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

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

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