How to pass route parameter in a href using Alpine.js - laravel

I am using Alpine.js for the first time inside of a Laravel project and would like to add an a href tag to a component however I am unsure how to route the parameter correctly
<template x-for="(post, index) in posts" :key="index">
<a x-bind:href="post.id">
<p class="mt-2 block text-sm font-medium text-gray-900 truncate pointer-events-none">
<span x-text="post.text"></span>
</p>
</a>
</template>
does redirect to
/id
and passes the id but I need to go to the named route posts and expect
/posts/id
if I add
<a x-bind:href="posts/post.id">
the result is a redirect with
/posts/NaN

You can try this: <a x-bind:href="posts + '/' + post.id">

Related

Thymeleaf/SB How do I pass in a variable from an object to a link element's href attribute?

I want to pass in the project.url variable into the th:href attribute of the element, but currently the URL that is navigated to is - /project/project.url/contribute which is obviously not what I wanted to happen.
The list above the button displays the project object's information and the URL is shown correctly.
<ul>
<span>Title: </span>
<li th:text="${project.url}">Title</li>
<span>Started: </span>
<li th:text="${project.publishedOn}">Started</li>
<span>Target: </span>
<li th:text="${project.targets}">Target</li>
<span>Description: </span>
<li th:text="${project.description}">Description</li>
<span>Amount Contributed: </span>
<li th:text="${project.amountContributed}">Amount Contributed</li>
</ul>
<div class="contributeButton">
<button>
<!-- Contribute -->
<a th:href="#{/project/${project.url}/contribute(action='show_all')}">Contribute</a>
</button>
</div>
What href do I need to put inside the link for this to work correctly? Thanks.
You can use concatenation to do this. I haven't tested this but give it a try:
<a th:href="#{'/project/' + ${project.url} + '/contribute(action=\'show_all\')'}">view</a>
You should be taking advantage of Thymeleaf's standard URL syntax for this:
<a th:href="#{/project/{url}/contribute(url=${project.url})}">view</a>

Why does a link with route set to posts.destroy in practice routes to posts.show

#extends('layouts.app')
#section('content')
<ul>
#foreach ($posts as $post)
<li>
<a href="{{route('posts.show', $post->id)}}">
{{$post->title}}
</a>
 
<a href="{{route('posts.edit', $post->id)}}">
Edit
</a>
 
<a href="{{route('posts.destroy', $post->id)}}">
Delete
</a>
#endforeach
</ul>
#endsection
I am new to laravel and in the process of learning. In the code above i have made a simple Unordered list of posts that is in the database. Next to each post is a edit and delete links. The edit link works just fine and invokes edit method in posts controller. But the delete link doesn't work. I think it runs the show method instead of destroy method in posts controller. Why is this the case?
To be more clear i have already somewhat solved the problem with the code bellow inside the foreach directive
<form action="/posts/{{$post->id}}" method="post">
#csrf
<input type="hidden" name="_method" value=" DELETE">
<input type="submit" name="delete" value="Delete" id="">
</form>
But why do this in the first place. I understand when we use form since a html form doesn't support DELETE method i had to add the hidden input. But why doesn't the hyper link tag work. This is the list of routes:
Links always perform GET method by default.
You can mimic DELETE method behavior using some javascript.

Laravel Request hasFile returns false despite file actually attached

I'm using a nice HTML template which features a preview of the picture selected by my HTML file input. This is the HTML:
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new img-thumbnail text-center">
<img src="#" data-src="holder.js/100%x100%" alt="not found"></div>
<div class="fileinput-preview fileinput-exists img-thumbnail"></div>
<div class="m-t-20 text-center">
<span class="btn btn-primary btn-file">
<span class="fileinput-new">choose</span>
<span class="fileinput-exists">change</span>
<input type="file" id="pb" name="pb" accept="image/*" class="form-control"></span>
<a href="#" class="btn btn-warning fileinput-exists"
data-dismiss="fileinput">Entfernen</a>
</div>
</div>
I chose pb as name for the actual file input. This is the controller function:
public function addUser(Request $request) {...}
Everything fine so far, echo $request->pb also echoes the filename as desired. HOWEVER, Laravel doesn't recognise this as a file apparently:
$request->file('pb')
returns null (yes, I have tried to display all files of $request, also $request->hasFile() returns false/null). I wonder why? Unfortunately, I cannot directly access $_FILE which would make things easier despite not being in the right order with Laravel in general.
Make sure your form has:
enctype="multipart/form-data"

Laravel 5.2 Eloquent not working in godaddy server

I developed a website in local using Laravel 5.2. All functions are working perfectly. But when I have uploaded it in godaddy server, all functions are working except eloquent. Insert, Update, Delete all works. but in database engine it is showing MYISAM. I change it in INNODB. But still eloquent are not working. Is it the problem of foreign_key constrains?? Error is eloquent variables are not getting data. How to fix it??
Error:
<body>
<div id="sf-resetcontent" class="sf-reset">
<h1>Whoops, looks like something went wrong.</h1>
<h2 class="block_exception clear_fix">
<span class="exception_counter">1/1</span>
<span class="exception_title"><abbr title="Symfony\Component\Debug\Exception\FatalErrorException">FatalErrorException</abbr> in <a title="/home/abhaymilestogo/public_html/floorstyler/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php line 797" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Model.php line 797</a>:</span>
<span class="exception_message">Class '\App\Model\organization' not found</span>
</h2>
<div class="block">
<ol class="traces list_exception">
<li> in <a title="/home/abhaymilestogo/public_html/floorstyler/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php line 797" ondblclick="var f=this.innerHTML;this.innerHTML=this.title;this.title=f;">Model.php line 797</a></li>
</ol>
</div>
</div>
</body>
agency.blade.php
<tr>
<th>{{$agency['id']}}</th>
<th>{{$agency['name']}}</th>
<th>{{$agency->organization->name}}</th>
<th>{{$agency['status']}}</th>
<th>{{$agency['updated_at']}}</th>
<th>{{$agency['created_at']}}</th>
<th>
<a style="font-size: medium;" class="fa fa-pencil-square-o" href="agency/edit/{{$agency['name']}}/{{Crypt::encrypt($agency['id'])}}"></a>
<a style="font-size: medium;" class="fa fa-trash-o" id="{{Crypt::encrypt($agency['id'])}}"></a>
#if($agency['status'] == 'ALIVE')
<a style="font-size: medium;" class="fa fa-times" id="{{Crypt::encrypt($agency['id'])}}"></a>
#else
<a style="font-size: medium;" class="fa fa-check" id="{{Crypt::encrypt($agency['id'])}}"></a>
#endif
</th>
</tr>
#endforeach
the problem is in {{$agency->organization->name}}
The error clearly states that Class \App\Model\organization not found.
Please check if you have defined the namespace in the Model class 'organization' and verify that the path you are specifying is correct.
Moreover, it is a good practice to always name your classes with the first letter capitalized.
If this does not solve the error, kindly post the file agency.blade.php.

How capture dynamically populated dropdown list ids in Capybara

I am using Capybara,Rspec and Ruby for my development. I am having drop down list which is populated with values with dynamic ids because of which I am not able locate the element correctly. Here is HTML code. I tried to get the solution from this site as well as by googling. But I couldn't get the correct solution
when I am doing the firebug I am getting Xpath as //[#id='select2-result-label-8'] and every time I am getting new id in the last meaning, next time if I go I will get it as //[#id='select2-result-label-11']. How to resolve this issue. I am having similar issues with my group of check box also. Hope this solution will help there also
<ul id="Values" class="check" role="listbox">
<li class="Reports " role="presentation">
<div id="select2-result-label-3" class="history" role="option">
<span class="same"/>
Add New
</div>
</li>
<li class="Reports-highlighted" role="presentation">
<div id="select2-result-label-4" class="history" role="option">
<span class="same"/>
Multiple
</div>
</li>
<li class="Reports" role="presentation">
<div id="select2-result-label-5" class="history" role="option">
<span class="same"/>
Last
</div>
</li>
</ul>

Resources