Can't get id from previous page Laravel - laravel

I have a button to see detail about user using button in each row datatable, the button will call a route detail.show and open link with user_id with it (ex:http://127.0.0.1:8000/detail/7),
here is the button code
->addColumn('action', function ($post) {
$link = "route('detail.show',$post->user_id)";
$btn = ' <span class="fas fa-eye"></span>';
return $btn;
})
the problem is I cant get any data from it, is there something wrong?
here is my route
Route::resource('detail', DetailController::class);
Here is my controller
public function show($user_id)
{
$post = Post::where('user_id', $user_id)->latest()->get();
return view('detail', compact('post'));
}
and here is my view
#extends('layouts.app')
#section('content')
<div class="row mt-5 mb-5">
<div class="col-lg-12 margin-tb">
<div class="float-left">
<h2> Show Post</h2>
</div>
</div>
</div>
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>User ID:</strong>
{{ $post->user_id }}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Name:</strong>
{{ $post->name }}
</div>
</div>
</div>
#endsection
I cant show anything using code as shown above. Is there something wrong? thanks in advance

Problems like this can often be solved by structuring your logic to be cleaner, more straightforward and use constructs of the framework as intended (documented).
In this case I would treat a user as resource and not the "detail".
I would rather create a specific route for showing the specific details of the user or just use partial resource routes.
Anyway, if you would like to keep your current build:
If I remember correctly, parameter passed to the show function in the controller should match the parameter name in your route.
When you generate routes with
Route::resource('detail', DetailController::class);
it creates something like
/details/{detail}
for the show method.
As described in this part of the docs.
Try replacing
$user_id
with
$detail
in your show method.

Related

why in search-bar using laravel livewire doesn't work

i want to show the user but it doesn't work and i wrote
#livewireScripts and #livewireStyles
i dont know why i found search tutorial at livewire docs
and doing the same but don't work also and not showing up any name of users
here is my code
my test.blade.php
#extends('layouts.master')
#section('css')
#livewireStyles
#section('title')
empty
#stop
#endsection
#section('page-header')
<!-- breadcrumb -->
<div class="page-title">
<div class="row">
<div class="col-sm-6">
<h4 class="mb-0"> </h4>
</div>
<div class="col-sm-6">
<ol class="breadcrumb pt-0 pr-0 float-left float-sm-right ">
<li class="breadcrumb-item">Home</li>
<li class="breadcrumb-item active">Page Title</li>
</ol>
</div>
</div>
</div>
<!-- breadcrumb -->
#endsection
#section('content')
<!-- row -->
<div class="row">
<div class="col-md-12 mb-30">
<div class="card card-statistics h-100">
<div class="card-body">
#livewire('counter')
</div>
</div>
</div>
</div>
<!-- row closed -->
#endsection
#section('js')
#livewireScripts
#endsection
my counter.blade.php
<ul>
#foreach($users as $user)
<li>{{ $user->name }}</li>
#endforeach
</ul>
my route when i show the component
Route::view('test', 'test');
my Counter.php of livewire
<?php
namespace App\Http\Livewire;
use App\User;
use Livewire\Component;
class Counter extends Component
{
public $search = '';
public function render()
{
return view('livewire.counter', [
'users' => User::where('name', $this->search)->get(),
]);
}
}
You have two issues here, one is that you never update your search property, and the other is - because you never update the search-property - you always search for those users who have an empty name (which is not null).
You should be making three changes, one to create an input-element that is bound to the $search proeprty, one to make the search a "like" operator (meaning that if you search for an empty string, all results will be shown, and if you have a user called Foobar and you search for Foo, it will show up in the results) and finally - because your results can change when you change your search criteria - you should be putting a key the list-elements, so Livewire know which row is which. The key should be something that is unique across your page.
First, add a new input-element to your counter view. This will be the search-property in your component. Because Livewire requires you to have only one root element in the view, we wrap it all in a <div>. This is also where we add the key to the different results.
<div>
<input wire:model="search" placeholder="Search users.." />
<ul>
#foreach($users as $user)
<li wire:key="user-{{ $user->id }}">
{{ $user->name }}
</li>
#endforeach
</ul>
</div>
Then, we need to update your retrieval of the results, in order to implement the like functionality. We put SQL wildcards % around the search to make this happen.
return view('livewire.counter', [
'users' => User::where('name', 'like', '%'.$this->search.'%')->get(),
]);
Take a deep look into what you are doing in your Livewire Component. You have initialized $search=''
Next, you are calling Users::where('name', $this->search)->get(). But at this point $this->search is an empty string. So it's either not returning users from your database or retrieving users without a name.
Finally, you are trying to display {{$user->name}} which even if there were users in your database, so either you don't have any users in your database without a name or you won't see anything because those users don't have a name.
You are missing a Search Input whose value is bound to the $search variable.
Add the following to your counter.blade.php
<input type="text" wire:model="search" />
And when you type a name in that field it should begin displaying in your list.

How to use pagination on ajax data

I want to use default pagination in laravel in view fetched by ajax function. put I get an error.
ajax function
public function get_products($id){
$products = Product::where('category_id',$id)->where('is_hidden','0')->paginate(9);
$category = Category::find($id);
$returnHTML = view('products_ajax')->with('products', $products)->with('category', $category)->render();
return response()->json(array('success' => true, 'html'=>$returnHTML));
}
returned view by ajax
<h3>{{$category->name}}</h1>
<hr>
<div class="cards">
#foreach($products as $product)
<div class="card" data-item-id="{{$product->id}}">
<img style="width:50%;" src="{{asset('storages/images/products/'.$product->image)}}">
<div class="card-details">
<p class="card-brand">{{$product->brand->name}}</p>
<p class="card-name" title="Food Name Here Food Name Here Food Name Here Food Name Here">
{{$product->code}}
</p>
<p class="card-price" hidden> {{$product->price}}</p>
<p hidden class="card-full-des">
{{strip_tags(html_entity_decode($product->description))}}
</p>
<p class="card-packing">
<span>{{$product->packing}}</span>
</p>
{{-- <p class="card-packing">
<span>Packing: 12-8 oz (225g)</span>
</p> --}}
<div class="card-foot">
<button class="mbtn5" onclick="CardAddToCartOrDetails(this, true)">Add to Cart</button>
<button class="mbtn4" onclick="CardAddToCartOrDetails(this, false)">Details</button>
</div>
</div>
</div>
#endforeach
{{$products->links()}}
</div>
</div>
but I get this error:
so, can anyone help me?
The MethodNotAllowedHttpException is caused by a calling a route with a wrong method.
i.e. defining a GET route for /users and calling it with POST would result in this error.

Display nothing when insert third record laravel

I'm developing a web app project of my client where people can ask there questions, like stack-overflow, I inserted 1 record manually into database it showed on the page and then I inserted 2nd record, it showed two records on the page but when i inserted 3rd record into database, nothing is showing on the page I checked the console there is no error there then I inspected the page inside body tag this is showing only <!----> I viewed the page source there code is displaying fine, then I removed the 3rd record everything is fine after that I inserted third record again the content of page gone again, this is my code of page...
#extends('layouts.app')
#section('content')
<div class="container">
<div class="row">
<div class="col-sm-8 offset-md-2">
#foreach ($questions as $question)
<div class="card mb-2">
<div class="card-header">
<h5 class="bold">{{ $question->question_title }}</h5>
</div>
<div class="card-body">
<p class="card-text">{{ (strlen($question->question_body) > 50) ? substr($question->question_body, 0, 50) : $question->question_body }}</p>
Continue Reading
<p class="text-muted float-right">{{ date('M j, Y h:ia', strtotime($question->created_at)) }}</p>
</div>
</div>
#endforeach
</div>
</div>
</div>
#endsection
and here is the QuestionController:
public function index()
{
$questions = Question::all();
return view('questions.index', compact('questions'));
}
To find out the issue, do this two things:
use dd() function in your controller and check the data
dd($questions)
check if there are any error in the Laravel log
storage/logs/laravel.log
Hope that help you find the problem and possible fix.
I think there is an issue with your html.Take your #foreach statement out and place it just before <div class="row">. If that works, you'll know that its in html.
Oh and adjust #endforeach accordingly.

Laravel 5.0 paginate jump to page section

I'm using Laravel 5.0 and paginate along with the render() function in my view. Rather than go back to the top of the screen after I select the next page I want it to jump to #msgs.
Here is some of my code:
<h1 class="centered" id="msgs">Announcements</h1>
#foreach($messages->getCollection()->all() as $message)
<div class="row">
<div class="row">
<div class="col-sm-9">
<h3>{{$message->title}}</h3>
</div>
<div class="col-sm-3">
<h4>From: {{$message->author_name}}</h4>
</div>
</div>
<div class="row">
<p>{{$message->text}}</p>
</div>
</div>
<hr class="featurette-divider">
#endforeach
<div class="centered">
{!! $messages->render() !!}
</div>
I'm still getting a grasp on the flow of things in laravel so I'm not really sure what to do. I tried to use append->('#msgs') but realized that its appending an array in the parameters, not appending a path to the url. I there a simple way to achieve this without overriding anything?
You might want to use fragment() method:
{!! $messages->fragment('msgs')->render() !!}
Here is the link to the documentation: Pagination - Laravel

Sending data from function to view template?

I have a blade template that looks something like this:
<div class="comment">
<div class="username"></div>
<div class="message"></div>
<div class="children">
</div>
</div>
I need to be able to call this view from a function and insert data into it.
I have a class, Helpers.php, with a recursive function that looks like this:
function getComments(array $comments)
{
foreach ($comments as $comment)
{
echo $comment;
if (!empty($comment->children))
{
getComments($comment->children);
}
}
}
What this is meant to do is print out a comment, and then check if that comment has any children comments and recurse if it does.
How can I modify my view/function so that I can send data to the view, such that I end up with something like this:
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="comment">
<div class="username">Username1</div>
<div class="message">Hello, world!</div>
<div class="children">
</div>
</div>
Thanks!
You don't have to use a function in this particular case.
To achieve your goal, create a view comments.blade.php. Depending on your Laravel version and setup you can put it into app/views or resources/views directory.
The content of this file has to be like follows:
#foreach ($comments as $comment)
<div class="comment">
<div class="username">{{ $comment->username }}</div>
<div class="message">{{ $comment->message }}</div>
<div class="children">
#include('comments', ['comments' => $comment->children])
</div>
</div>
#endforeach
Then render this view and pass the same argument you were passing to your getComments() function.
This will hopefully give the desired result.
Good luck!
you can send the data to view from function you can use by three method compact,with and session. compact of with method while your are redirecting of loading any view you can use compact or ->with(array());

Resources