In my Laravel application, when I am trying to link the username of the person who has posted in the website with his profile page, with the code:
<div class="media-body">#{{ post.user.name }}
It is giving the error:
Sorry, the page you are looking for could not be found.
1/1 NotFoundHttpException in RouteCollection.php line 161:
But, when I am trying to print #{{ post.user.profileUrl }} it is giving the right address, also in the json response, it is giving the right address, and going to the address is also reaching the specific location.
So, I don’t think it is some problem with post.user.profileUrl, as it seems to work fine, it seems to be some problem with using it with href, the address of the error in Google Chrome is:
http://localhost:8000/%7B%7B%20post.user.profileUrl%20%7D%7D
and the address should have been
http://localhost:8000/users/2 where 2 refers to the id of the user, which I am passing to the user through Vue.js
The problem is #{{ post.user.profileUrl }} is not parsing. %7B%7B%20post.user.profileUrl%20%7D%7D is ASCII representation of {{ post.user.profileUrl }}
Check if the code is in .blade.php file. If it is, you should look into JS template engine if you're using any.
https://laravel.com/docs/5.3/blade#blade-and-javascript-frameworks
The # symbol will be removed by Blade; however, {{ name }} expression will remain untouched by the Blade engine, allowing it to instead be rendered by your JavaScript framework
I think you have a $post model in your view. When you setup relationships between the model Post and User you can link to them through the following:
#{{ $post->user->name }}
This will turn the users profileUrl into a valid link. But therefor you had to setup relationships in the Models.
Post model:
public function user() {
return $this->hasMany('App\Users', 'id', 'user_id');
//first parameter is the Model class
//send is the id of the user table
//thirth is the user_id in the post table
}
And in the User model:
public function posts() {
return $this->hasMany('App\Posts', 'user_id', 'id');
}
Hope this works!
Try this... this should definitely work for you
#verbatim
<div class="media-body">
<a href="{{ post.user.profileUrl }}">
{{ post.user.name }}
</a>
</div>
#endverbatim
I got my problem, the issue is with the javascript library, I am using, known as vue.js, it used to allow usage inside quotes, but in vue 2, they don't, so there is a turnaround for this,
<a :href="post.user.profileUrl">#{{ post.user.name }}</a>
and this works fine.
Thanks to all the people who contributed by answering...
Related
I have a weird situation, where my route seems to be not defined.
I am using Laravel's starter kit Breeze, and I am adding top left navigational links. This is my 4th link already, but it seems that It's the first time I face such issue.
Controller:
public function index(Request $request)
{
$user = $request->user();
$data = $user->campaigns;
return view('subscribes.index', ['data' => $data]);
}
Route:
Route::resource('/my_campaigns', App\Http\Controllers\SubscribeController::class);
Navigational link code:
<div class="hidden space-x-8 sm:-my-px sm:ml-10 sm:flex">
<x-nav-link :href="route('subscribes.index')" :active="request()->routeIs('subscribes.index')">
{{ __('My Campaigns') }}
</x-nav-link>
</div>
Error message:
Route [subscribes.index] not defined.
I have spent 2 hours trying to check every single spot, where the issue may lay, but to no avail. I have already cleared route:cache.
I'd like to note, that the page /my_campaigns opens without any issues, and is working, until I try to add it within the navigational links.
My folders and files seems to be created correct as well, due to the fact, that /my_campaigns is working.
Any idea, where I missed something?
The issue was within my navigational link code. Where the href route is defined, I have used incorrect URL, therefore, this issue was caused.
I am not sure where to look / what to look for as I am new to HTML / CSS and Laravel / PHP.
I have a function that is returning a collection (of just one item in this case, but I wanted to keep in the count it uses for use later), and I access the object I want in my view as such:
Top Vulnerability: #foreach ($tvuln as $object)
{{ $object->cve }}
#endforeach
I want to turn the CVE that is returning into a link and have that link, when pressed take me to another page that runs a query to pull up a list of all hosts with the given CVE.
I am trying to wrap my head around how to do this and could use some guidance in terms of what to search for or how to implement this with Laravel / Eloquent.
You could try something like:
#foreach ($tvuln as $object)
<a href="{{ route(routename..., parameters...) }}">
{{ $object->cve }}
</a>
#endforeach
For some more info about routes check here.
Hi I have a hyperlink from a page:
<h3>hitest</h3>
the route:
Route::get('hitest', function(){ return 'hitest message';});
There is an error:
No query results for model [App\User2].
hyper link is from a page with this url
/userpage/1
the 1 is a model object.
Shouldn't the hyperlink route to /hitest ?
Please see my other post: Strange behavior with routing and hypertext.
I'm new at web development. Is there configurations for routing? The app is hosted (not local).
As bytesarelife already mentioned, you can use the url()-function, like so:
{{ url('your/url/') }}
A better way in terms of maintainability would be to give your routes names, so you would not have to replace every url in every template once you want to change it. You can do so, by adding the name in your routes:
Route::get('hitest', function(){ return 'hitest message';})->name('getHittest');
And then you can use the route function in your view:
{{ route('getHittest') }}
I'm trying to create a page dynamically for each courses that I'm adding in a database.
I have a CoursesController who is taking care of adding, displaying the courses.
So, when I click on a course, it should dynamically create a page for that course and show details in that course page.
In the route.php page, I have
Route::get('courses/{code}', [ 'as'=>'course-show', 'uses'=>'CoursesController#getShow']);
and in the
CoursesController.php
public function getShow($code){
return $code;
}
And in the index.blade.php for CoursesController,
<h4>{{ $course->name }}</h4>
Now, It create the link with a unique code (saved in database) and upon clicking there, it takes me to the course page with an error:
BadMethodCallException
Method [show] does not exist.
What might be the problem? Can anyone help me?
The getShow() function in your controller should be show().
Also URL::action() goes to a controller action.
You probably want URL::route()
<h4> {{ $course->name }} </h4>
or you could do this
<h4> {{ $course->name }} </h4>
On a dashboard page, I've created a select list in a form that lists the names of components; the value that's passed from the select list is obviously the component id. On pressing submit, the user is routed to a page that displays the data about that component. Should be dirt simple...
Controller:
public function showDashboard()
{
$components = Component::lists('name','id'); ...
return View::make('dashboard', array('components'=>$components, ...))
}
dashboard.blade.php:
{{ Form::open(array('route' => array('components.show', $components->id), 'method'=>'get')) }}
{{ Form::Label('id','Component:') }}
{{ Form::select('id', $components) }}
{{ Form::submit('Show Component', array('class'=>'button')) }}
{{ Form::close() }}
I've tried various ways of doing this, and get a different error every time. The above code doesn't even let me display the dashboard page -- I get a "Trying to get property of non-object" error. Clearly, it's not liking $components because that was passed as a list array and not an object. As I said, I'm sure this is dirt simple, I just can't figure out the proper syntax, and Laravel docs aren't giving me the answer. Thanks!
The problem isn't the dropdown, or the lists method, but rather in your form opening. Here, you have $components->id as an argument to the route, but $components is an array and you can't access an id property on it.
Finally figured this out. I had posted a similar question here subsequent to this one, and rather than repeat the answer, it is here:
How to pass id value from select list to controller when controller expects an object? laravel-4
The very short version: change Route::get to Route::post. Details with code in the link above. Problem solved!