Laravel problem with passing variables from controller to view - laravel

I am working on a laravel project and having a problem passing variables from the controller to the view:
this is the controller method:
public function index($id){
$data = [];
$company = DB::table('companies')->where('id',$id)->first();
return view('searchResultAdmin.index', ['company'=>$company, 'Data'=>$data]);
}
and this is the view :
{{ Form::open(array('url' => '/SearchResultAdmin', 'method' => 'post')) }}
<div class="radio-buttons display-inline">
<fieldset class="display-inline group1">
<label class="radio-inline">
<input type="radio" name="SearchLocation" id="value1" value="Actuals" checked>Actuals
</label>
<label class="radio-inline">
<input type="radio" name="SearchLocation" id="value2" value="Forecasts">Forecasts
</label>
</fieldset>
{{ Form::hidden('id', ($company->id)) }}
<fieldset class="display-inline">
<label class="radio-inline">
<input type="radio" name="Order" id="value3" value="Ascending" checked>Ascending
</label>
<label class="radio-inline">
<input type="radio" name="Order" id="value4" value="Descending">Descending
</label>
</fieldset>
</div>
<div class="dropdown-header-table display-inline">
<h5>Search By:
<select name="SearchBy">
<option value="Account">Account</option>
<option value="Name">Name</option>
<option value="Year">Year</option>
<option value="Description">Description</option>
<option value="Amount">Amount</option>
</select>
</h5>
</div>
<div class="search-input display-inline">
<div class="table-form-header">
<input type="text" class="form-control" name="SearchInputs" placeholder="Search...">
<span class="input-group-btn">
<button class="btn btn-default" type="submit">
<i class="fa fa-search"></i>
</button>
</span>
</div>
</div>
{{ Form::close() }}
the error says: undefined variable company on view

You get the error because of
{{ Form::hidden('id', ($company->id)) }}
Add dd($company) or dd($company->id) in your controller before the returning the view and see what you get.
Good luck!

Related

Error "The PATCH method is not supported for this route. Supported methods: GET, HEAD, POST. " update method

Well hello there, I am trying to update a register of my table assistants, but when I push the button submit of the form, this error appear, I am using a table pivot between assistants and events, but i am only try to edit a assistant.This is the error
This is my code AssistantController.php file and the methods edit and update
edit and update methods
public function edit(Assistant $assistant)
{
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return view('assistants.edit',compact('assistant','event'));
}
public function update(Request $request, Assistant $assistant)
{
#updating
$assistant->update($request->all());
//$assistants->user()->associate(Auth::user());
//
#obtain id assistant
$assistant_id = $assistant->id;
#get data event of the assistant to pass to the form
$event = Assistant::find($assistant_id)->events()->get();
return redirect('assistants.index',compact('assistant','event'));
}
This is my form to assistants
input form assistants
#csrf
<div class="form-group">
<label for="id">Document:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('id') ?? $assistant->id }}" name="id" autofocus>
<div>{{ $errors->first('id') }}</div>
</div>
<div class="form-group">
<label for="name">Name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('name') ?? $assistant->name }}" name="name" placeholder="First Name assistant">
<div>{{ $errors->first('name') }}</div>
</div>
<div class="form-group">
<label for="last_name">Last name:</label>
<small class="text-muted">Required(*)</small>
<input type="text" class="form-control form-control-sm " value="{{ old('last_name') ?? $assistant->last_name }}" name="last_name" placeholder="Last name assistant">
<div>{{ $errors->first('last_name') }}</div>
</div>
<div class="form-group">
<label for="phone">Phone:</label>
<small class="text-muted">Required(*)</small>
<input type="number" class="form-control form-control-sm " value="{{ old('phone') ?? $assistant->phone }}" name="phone">
<div>{{ $errors->first('phone') }}</div>
</div>
<div class="form-group">
<label for="email">Email:</label>
<small class="text-muted">Required(*)</small>
<input type="mail" class="form-control form-control-sm " value="{{ old('email') ?? $assistant->email }}" name="email">
<div>{{ $errors->first('email') }}</div>
</div>
<div class="form-group">
<label for="observations">Observations:</label>
<input type="text" class="form-control form-control-sm " value="{{ old('observations') ?? $assistant->observations }}"name="observations">
<div>{{ $errors->first('observations') }}</div>
</div>
This is my edit page
#extends('layouts.back')
#section('title','Edit assistants')
#section('content')
<div class="container p-4">
<div class="row">
<div class="card">
<div class="card-header">
<div class="card-title">
<h3>Edit details to assistant:</h3><br>
<h4><strong> {{$assistant->name}} {{$assistant->last_name}}</strong> </h4>
</div>
</div>
<div class="card-body">
<form action="/assistants" method="POST">
#method('PATCH')
#include('assistants.form')
<button type="submit" class="btn btn-primary">Update</button>
Cancel
</form>
</div>
<div class="card-footer">
</div>
</div>
</div>
</div>
#endsection
There are my routes
routes
Auth::routes();
Route::get('/', 'HomeController#index')->name('home');
Route::resource('events', 'EventController');
Route::resource('assistants', 'AssistantController');
Route::resource('certificates', 'CertificateController');
Route::resource('signers', 'SignerController');
I am beginner.
thank you all.
In your edit page, change your form action from
<form action="/assistants" method="POST">
to
<form action="/assistants/{{ $assistant->id }}" method="POST">
Without the assistant id in the action field, Laravel thinks you are trying to update the base assistants route, which is an invalid action for that route.

How to add permission for a user on specific page

I have view, edit and delete permissions to a specific page, so I need to get these values:
page_id, user_id, edit, view, delete
I have a form with page name and 3 checkboxes for each page
<div class="container">
<form action="/test" method="POST">
#csrf
<div class="form-group">
#foreach( $tmp as $key=>$val )
<li>
#if( !isset($val['children']) && empty($var['children']))
<b>{{ $val['data']['name'] }}</b>
#else
<b>{{ app()->getLocale() == 'ar' ? $val['data']->ar_name : $val['data']->display_name }}</b>
#endif
#if( isset($val['children']) and !empty($val['children']))
<ul>
#foreach( $val['children'] as $c=>$child)
<li>
{{ $child->name }} :
<div class="form-group">
<input type="hidden" name="user_id" value="{{ $user_id }}">
<input type="hidden" name="page_id" value="{{ $child->name }}">
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="view">View
</label>
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="edit">Edit
</label>
<label class="checkbox-inline">
<input type="checkbox" name="arr[]" value="delete">Delete
</label>
</div>
</li>
#endforeach
</ul>
#endif
</li>
#endforeach
<br>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
but when i sent data, i can't get the correct page_id, is there anything else can i do
I solved it by adding $child->id
<div class="form-group">
<input type="hidden" name="user_id" value="{{ $user_id }}">
<input type="hidden" name="page_id[{{$child->id}}]" value="{{ $child->id }}">
<label class="checkbox-inline">
<input type="checkbox" name="view[{{$child->id}}]" value="1">View
</label>
<label class="checkbox-inline">
<input type="checkbox" name="edit[{{$child->id}}]" value="1">Edit
</label>
<label class="checkbox-inline">
<input type="checkbox" name="del[{{$child->id}}]" value="1">Delete
</label>
</div>

Request values of contoller not getting value of select

This is a noob question, I send a post request to my controller and all it can get is the request of the input. What I want is I can also get the value of $cat array.
Here's my blade:
#if (!$categories->isEmpty())
<form action="{{route('subcategory.store')}}" method="POST">
#csrf
<div class="row">
<div class="input-group mb-3 col-md-4">
<div class="input-group-prepend">
<label class="input-group-text" for="inputGroupSelect01">Choose Category:</label>
</div>
<select class="custom-select" id="inputGroupSelect01">
#foreach ($categories as $cat)
<option value="{{$cat->id}}">{{$cat->name}}</option>
#endforeach
</select>
</div>
<input type="text" name="subcat" id="subcat" class="form-control col-md-3 ml-5">
<button type="submit" class="btn btn-success btn-lg ml-3">Save Sub Category</button>
</div>
</form>
#endif
And here's my returned value.
{"_token":"XlzwIt2jFqcybqqSjwLLJIAChG5tui7gvHzlaeAq","subcat":"xx"}
You forgot to add name="cat" in select i guess
<select class="custom-select" id="inputGroupSelect01">
simple add name="" attr
<select class="custom-select" id="inputGroupSelect01" name="cat">

Controller Update method is not working in laravel

I am using laravel 5.2. Controller method store, show, and edit is working fine but update method is not working.
edit.blade is as:
#extends ('layouts.backend')
#section ('content')
<div class="pull-right">
<a class="btn btn-primary" href="{{ route('backend.index') }}"> Back</a>
</div>
<form method="PATCH" action="{{ route('backend.update', $reports->id) }}">
<div>
create report
</div>
<div>
<table>
<tr>
<td>
<div class="form-group required">
<label for="category_id" class="control-label">ID</label>
<input class="form-control" name="id" id="id" type="text" value="{{ $reports->id}}">
</div>
<div class="form-group required">
<label for="category_id" class="control-label">Category</label>
<select class="form-control" id="category" name="category"><option value="" selected="selected">Select</option><option value="1">Agriculture</option><option value="2">Food</option><option value="3">Beverage</option></select>
</div>
<div class="form-group required">
<label for="name" class="control-label">Name</label>
<input class="form-control" name="name" id="name" type="text" value="{{ $reports->name}}">
</div>
<div class="form-group required">
<label for="url" class="control-label">Url</label>
<input class="form-control" name="slug" id="slug" type="text" value="{{ $reports->slug}}">
</div>
<div class="form-group required">
<label>Brief Description</label></br>
<textarea id="brief_des" name="brief_des" rows="12" cols="70" style="width: 80%" >
{{ $reports->brief_des}}
</textarea>
</div>
<div class="form-group required">
<label>Full Description</label></br>
<textarea id="full_des" name="full_des" rows="12" cols="70" style="width: 80%">
{{ $reports->full_des}}
</textarea>
</div>
<input type="submit" value="Update"/>
</td>
<td>
</td>
</tr>
</table>
</div>
{{ Form::token() }}
</form>
#endsection
and Controller update method code is as:
public function update(Request $request, $id){
report::find($id)->update($request->all());
return redirect()->route('backend.index')
->with('success','Product Updated Successfully');
}
and route.php is as:
Route::resource('backend', 'ReportController');
when I am submitting the form then I am getting any update in my database and redirect is not working also.

Laravel - Request does not contain my inputs

I have the following form :
<div class="container text-center search-container">
<form id="search-form" action="" method="post">
<div class="nopadding col-xs-12 col-sm-2">
<select name="huge" class="form-control input-block-level selectpicker search-selector" data-style="btn-default btn-fill btn-block">
<option disabled>Search in...</option>
<option value="1" selected>foo1</option>
<option value="2">foo2</option>
<option value="3">foo3</option>
</select>
</div>
<div class="nopadding col-xs-12 col-sm-8">
<input type="text" class="form-control input-block-level search-bar" id="searchbar">
</div>
{{ csrf_field() }}
<div class="nopadding col-xs-12 col-sm-2">
<button class="btn btn-info btn-fill search-button input-block-level" type="submit">Search</button>
</div>
</form>
</div>
I use bootstrap-select for the select element.
This calls a method in my controller, however when I do a dd($request->all(), here is what I do obtain :
array:2 [▼
"huge" => "1"
"_token" => "somethingReallyLongAndBoring"
]
Basically, I am not able to retrieve my inputs. Any idea what I did wrong ?
You have to add name attribute to the input field.
<input type="text" class="form-control input-block-level search-bar" id="searchbar" name="searchbar">

Resources