How to use $_GET variable from AJAX - LARAVEL - laravel

I need to use the $_GET variable into the blade.
The AJAX code work good, i received an alert with the correct value but i can't use it into the blade.
Route:
/*
|--------------------------------------------------------------------------
| Routes per gestire la pagina "Home"
|--------------------------------------------------------------------------
*/
Route::get('/home', 'HomeController#index');
Route::get('/home', array('as' => 'success', 'uses' => 'StatisticheController#totaleRichiesteAnnue'));
home.blade.php
#if(isset($_GET['tipologia_evento_id']))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
<script>
var tipologia_evento_id = event.tipologia_evento_id;
$.ajax({
type: "GET",
url: '/home',
data: { tipologia_evento_id: tipologia_evento_id},
success: function(msg)
{
alert(tipologia_evento_id);
$('#modal-event').modal('show');
}
});
</script>

In laravel 5 request() helper is available globaly
#if(request()->tipologia_evento_id)
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
request()->tipologia_evento_id is not set it will return null
Hope this helps

You can use the Request facade:
Request::get('tipologia_evento_id');
More details on: https://laravel.com/docs/5.6/requests

You should try below code:
#if(app('request')->input('tipologia_evento_id'))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
OR
#if(Request::get('tipologia_evento_id'))
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif

As you already have route and controller, why not pass it from controller and use it in view?
class HomeController extends Controller
{
public function index(Request $request)
{
$getData = $request->all();
return view('home', compact('getData'));
}
}
in your view you can easily access as $getData
So your home.blad.php will look something like this
#if($getData->tipologia_evento_id)
<div class="form-group">
{!! Form::label('responsabile', 'Responsabile') !!}
{!! Form::text('responsabile', old('_responsabile'), ['class' => 'form-control','disabled'=>'disabled']) !!}
</div>
#endif
EDIT:
If you want to get json response using ajax
class HomeController extends Controller
{
public function index(Request $request)
{
$getData = $request->all();
return response()->json($getData);
}
}

Related

Laravel 8 with laravelcollective/html 6.2, Action Controller#function not defined

I am new to Laravel and Stackoverflow
When Laravel7 is released, I started to learn Laravel.
The new version 8.0 was introduced not long ago and I started to try it.
I cannot define that the problem is caused by a newer version of Laravelor any misconfiguration.
When I try the following (edit.blade.php)
{!! Form::open(['action' => ['ProductController#update', $product->id], 'method' => 'POST']) !!}
or
{!! Form::open(['action' => [[ProductController::class, 'update'], $product->id], 'method' => 'POST']) !!}
an error occurred
Action ProductController#update not defined
then I tried to replace the controller name with a path like
{!! Form::open(['action' => ['App\Http\Controllers\ProductController#update', $product->id], 'method' => 'POST']) !!}
or
{!! Form::open(['action' => [[App\Http\Controllers\ProductController::class, 'update'], $product->id], 'method' => 'POST']) !!}
It works!
so I think this is about namespace
but I have a namespace heading
namespace App\Http\Controllers; in my App\Http\ProductController.php
although I can solve the problem by typing the full path of the controller in the collective form,
I am worried that my code has configuration error or syntax errors, etc.
This is a change to Laravel 8. There is no namespace prefix applied to your routes by default and there is no namespace prefix used when generating URLs to "actions".
To have the namespace prefixed to the Controller you are trying to reference when generating URLs to actions you would need to define the $namespace property on your App\Providers\RouteServiceProvider:
protected $namespace = 'App\Http\Controllers';
Now you can reference your action with that namespace prefix:
Form::open(['action' => ['ProductController#update', $product->id], 'method' => 'POST'])
In Laravel 8: use this code I think your problem will be solved
web.php
Route::middleware(['auth:sanctum', 'verified'])->group(function () {
Route::get('create', 'Product\ProductController#Create')->name('product.create')->middleware('can:Add Product');
Route::post('create', 'Product\ProductController#Store')->name('product.store')->middleware('can:Add Product');
});
resources/views/product:
<form method="post" action="{{ route('product.store') }}" enctype="multipart/form-data">
#csrf
{{-- START - PRODUCT INFORMATION --}}
{{-- END- PRODUCT INFORMATION --}}
</form>
app/Http/Controllers/Products:
<?php
namespace App\Http\Controllers\Products;
use App\Http\Requests\Product\ProductRequest;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
class ProductController extends Controller {
//---Constructor Function
public function __construct() {
$this->middleware('auth:sanctum');
}//---End of Function Constructor
//---Create Product
public function create() {
return view('product.product_add');
}//---End of Function create
//---Store Product in Database
public function store(ProductRequest $request) {
//---Add Product Details in DB
}
?>
Laravel 8 : Collective form - This is using to create a Laravel view page
{!! Form::open(['route' => '', 'id'=>'form','class' => 'needs-validation',]) !!}
<div class="form-group row">
{!! Form::label('employee_id', 'Employee ID:', ['class'=>'col-md-1 col-form-label custom_required']) !!}
<!-- Employee ID -->
<div class="form-group row">
{!! Form::label('employee_name', 'Employee Name:', ['class'=>'col-md-1 col-form-label custom_required']) !!}
<div class="col-lg-8">
{!! Form::text('employee_name', #$employee_id, ['class' => 'form-control', 'required', 'placeholder' => 'Employee Name', 'pattern'=> '^[a-z A-Z0-9_.-]*$']) !!}
</div>
<!-- Employee number -->
<div class="form-group row">
{!! Form::label('phone_number', 'Number:', ['class'=>'col-md-1 col-form-label custom_required']) !!}
<div class="col-lg-8">
{!! Form::text('phone_number', #$phone_number, ['class' => 'form-control', 'required','placeholder' => 'number']) !!}
</div>
{!! Form::close() !!}

update user avatar with laravel5

From the look of it seems to me very logic but I am missing something, it doesn't work, nothing changes!
Here is my profile controller file:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Auth;
use Image;
class ProfileController extends Controller
{
public function profile()
{
$user = Auth::user();
return view('profile')->with('user', $user);
}
public function edit()
{
$user = Auth::user();
return view('edit')->with('user', $user);
}
public function update(Request $request)
{
if($request->hasFile('avatar'))
{
$avatar = $request->file('avatar');
$filename = time().'.'.$avatar->getClientOriginalExtension();
Image::make($avatar)->resize(300, 300)->save(public_path('/uploads/users_avatars/'.$filename));
$user = Auth::user();
$user->avatar = $filename;
$user->save();
}
return redirect('profile')->with('user', Auth::user());
}
}
and here is my edit.blade.php
#extends('layouts.app')
#section('content')
<div class="col-md-6">
{!! Form::model($user, ['method'=>'PATCH', 'action'=>'ProfileController#update', 'file'=>'true']) !!}
<div class="form-group">
{!! Form::label('name', 'Name') !!}
{!! Form::text('name', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('email', 'Email') !!}
{!! Form::email('email', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('number', 'Phone') !!}
{!! Form::text('number', null, ['class'=>'form-control']) !!}
</div>
<div class="form-group col-md-5">
{!! Form::label('avatar', 'Avatar') !!}
{!! Form::file('avatar', ['class'=>'form-control']) !!}
</div><br><br><br><br>
<div class="form-group">
{!! Form::submit('Update', null, ['class'=>'btn btn-primary']) !!}
</div>
{!! Form::close() !!}
</div>
#stop
but when I edit the user it doesn't change..plz help
It seems like you have made a mistake while using Form from Laravel HTML Collective. You should use "files" => true instead of "file" => true
{!! Form::model($user, ['method'=>'PATCH', 'action'=>'ProfileController#update', 'file'=>'true']) !!}

Update data with Laravel Collective forms

I have an edit form with Laravel Collective but when clicking the button, the data do not update. Below are my codes.
Form:
{!! Form::model($post, ['route' => ['/post/update/', $post->id]]) !!}
{{ method_field('PATCH') }}
<div class="form-group">
<div class="row">
<div class="col-lg-6">
{!! Form::label('title', 'Title') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="col-lg-6">
{!! Form::label('category_id', 'Category') !!}
{!! Form::select('category_id', $categories, null, ['class' => 'form-control']) !!}
</div>
</div>
</div>
<div class="form-group">
{!! Form::label('content', 'Content') !!}
{!! Form::textarea('content', null, ['class' => 'form-control', 'rows' => 10]) !!}
</div>
<hr/>
<div class="form-group">
{!! Form::submit('Update', ['class' => 'btn btn-success pull-right']) !!}
</div>
{!! Form::close() !!}
Controller:
public function edit($id)
{
return \View::make('admin/post/edit')->with([
'post' => \DB::table('posts')->find($id),
'categories' => \App\Category::lists('category', 'id')
]);
}
public function update(Request $request, Post $post)
{
$post->update($request->all());
return \Redirect::to('/admin/posts');
}
Routes:
Route::get('/admin/post/edit/{id}', 'Admin\PostController#edit');
Route::patch('/post/update/', [
'as' => '/post/update/',
'uses' => 'Admin\PostController#update'
]);
It's a bit different from the Laracast, and it's confusing me. Framework is new to me and the lack of code to do something is confusing.
I solved it. Mass Assignment. explains what to do if using update or create
So, the update method is:
public function update(Request $request, Post $post)
{
$post->title = $request->title;
$post->category_id = $request->category_id;
$post->content = $request->content;
$post->save();
return \Redirect::to('/admin/posts');
}

Method is not allowed

I am trying to create simple form with post. But when i submit my form i get this error - MethodNotAllowedHttpException in RouteCollection.php line 219:
My route file:
Route::get('articles', 'ArticlesController#index');
Route::get('articles/create', 'ArticlesController#create');
Route::get('articles/{id}', 'ArticlesController#show');
Route::post('articles', 'ArticlesController#store');
Form:
{!! Form::open(['url' => 'articles', 'method' => 'post']) !!}
<div class="form-group">
{!! Form::label('title', 'Title:') !!}
{!! Form::text('title', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::label('body', 'Body:') !!}
{!! Form::textarea('body', null, ['class' => 'form-control']) !!}
</div>
<div class="form-group">
{!! Form::submit('Add article', ['class' => 'btn btn-primary form-control']) !!}
</div>
{!! Form::close() !!}
Controller class:
public function store(Request $request) {
$input = $request->all();
return $input;
}
Thanks for your attention. I dont get where is the problem.
Found answer. Just type php artisan route:clear in terminal.
You have the same url both in Route::get('articles', 'ArticlesController#index'); and Route::post('articles', 'ArticlesController#store');
Use action() instead of url can solve this problem. EX:
{!! Form::open(['action' => 'ArticlesController#store', 'method' => 'post']) !!}
Change the route: Route::get('articles', 'ArticlesController#index'); to Route::post('articles', 'ArticlesController#index');

Laravel 5 MethodNotAllowedHttpException issues

I am trying to make a simple CRUD for Clients. At the moment I can view clients and get to the edit page. However, if I try to update or create a client, I get a MethodNotAllowedHttpException.
My routes look like the following
Route::model('clients', 'Client');
Route::bind('clients', function($value, $route) {
return App\Client::whereSlug($value)->first();
});
Route::resource('clients', 'ClientsController');
My controller is like so
<?php
namespace App\Http\Controllers;
use App\Client;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Input;
use Redirect;
use Illuminate\Http\Request;
class ClientsController extends Controller {
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$clients = Client::all();
return view('clients.index', compact('clients'));
}
/**
* Show the form for creating a new resource.
*
* #return Response
*/
public function create()
{
return view('clients.create');
}
/**
* Store a newly created resource in storage.
*
* #return Response
*/
public function store()
{
$input = Input::all();
Client::create( $input );
return Redirect::route('clients.index')->with('message', 'Client created');
}
/**
* Show the form for editing the specified resource.
*
* #param \App\Client $client
* #return Response
*/
public function edit(Client $client)
{
return view('clients.edit', compact('client'));
}
/**
* Update the specified resource in storage.
*
* #param \App\Client $client
* #return Response
*/
public function update(Client $client)
{
$input = array_except(Input::all(), '_method');
$client->update($input);
return Redirect::route('clients.index', $client->slug)->with('message', 'Client updated.');
}
/**
* Remove the specified resource from storage.
*
* #param \App\Client $client
* #return Response
*/
public function destroy(Client $client)
{
$client->delete();
return Redirect::route('clients.index')->with('message', 'Client deleted.');
}
}
I then have a form partial like so
<div class="form-group">
{!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('clientName', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('contactEmail', 'Contact Email:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('contactEmail', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('slug', 'Slug:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('slug', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::submit($submit_text, ['class'=>'btn btn-default']) !!}
</div>
And my edit.blade.php is like so
<h2>Edit Client</h2>
{!! Form::model($client, ['class'=>'form-horizontal'], ['method' => 'PATCH', 'route' => ['clients.update', $client->slug]]) !!}
#include('clients/partials/_form', ['submit_text' => 'Edit Client'])
{!! Form::close() !!}
I have researched this error and a lot of people refer to javascript, but I am not using any javascript at the moment.
Why would I be getting this error? As I say, I get it when I try to create a client as well.
Thanks
As an update, if I remove the form partial and add this to ed.it.blade.php instead, it seems to work
{!! Form::model($client, [
'method' => 'PATCH',
'route' => ['clients.update', $client->slug]
]) !!}
<div class="form-group">
{!! Form::label('clientName', 'Client Name:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('clientName', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('contactEmail', 'Contact Email:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('contactEmail', null, array('class' => 'form-control')) !!}
</div>
</div>
<div class="form-group">
{!! Form::label('slug', 'Slug:', array('class' => 'col-sm-5 control-label blue')) !!}
<div class="col-sm-7">
{!! Form::text('slug', null, array('class' => 'form-control')) !!}
</div>
</div>
{!! Form::submit('Update Client', ['class' => 'btn btn-primary']) !!}
{!! Form::close() !!}
Why is that?

Resources