I want to pass the selected item's $id to my controller for doing changing on it .
it's my index.blade.php (view)code
<table>
#foreach($posts as $p)
<tr>
<td>{{$p->title}}</td>
<td>{{substr($p->body,0,120).'[...]'}}</td>
<td>{{HTML::link('posts_show',' Preview',array($p->id))}}</td>
<td>{{HTML::link('posts_edit','Edit',array($p->id))}}</td>
<td>
{{Form::open(array('method'=>'DELETE','url'=>array('posts.delete',$p->id)))}}
{{Form::submit('Delete')}}
{{Form::close()}}
</td>
</tr>
#endforeach
</table>
but it doesnt pass $id to my controller's methods.
thanks for your time.
What you need to do is to set route parameter. Your route should be like that.
Route::get('post','postController#index');
Route::get('posts_create', function (){ return View::make('admin.newPost'); });
Route::get('posts_show/{id}','postController#show');
Route::get('posts_edit/{id}','postController#edit');
Route::post('posts_delete/{id}','postController#destroy');
If you want to use named route {{ Form::open(array('url' => route('posts.edit', $p->id))) }}, you need to set name like that.
Route::post('posts_edit/{id}', array('uses' => 'postController#edit',
'as' => 'posts.edit'));
You can check routing in laravel official documentation.
Edit
For now, your form in view look like that.
{{ Form::open(array('url' => route('posts.edit', $post->id), 'method' => 'POST')) }}
In route,
Route::post('posts_edit/{id}', array('uses' => 'postController#edit',
'as' => 'posts.edit'));
In PostController,
public function edit($id)
{
// do something
}
I hope it might be useful.
Looks like your route has a name posts.destroy, if that is the case you should use route instead of url as a parameter
{{Form::open(array('method'=>'DELETE','route'=>array('posts.destroy',$p->id)))}}
it's my route:
Route::get('post','postController#index');
Route::get('posts_create', function (){ return View::make('admin.newPost'); });
Route::get('posts_show','postController#show');
Route::get('posts_edit','postController#edit');
Route::post('posts_delete','postController#destroy');
it's my postController:
class postController extends BaseController{
public function show($id){
$post=Post::find($id);
$date=$post->persian_date;
return View::make('posts.show')->with('post',$post)->with('date',$date);
}
public function edit($id){
$post=Post::find($id);
if(is_null($post)){
return Redirect::route('posts.index');
}
return View::make('posts.edit')->with('post',$post);
}
public function update($id){
$input=array_except(Input::all(),'_method');
Post::find($id)->update($input);
return Redirect::route('posts.index');
}
public function destroy($id)
{
Post::find($id)->delete();
return Redirect::route('posts.index');
}
Related
The blade code:
<td>{{ $employee->first_name }} {{ $employee->last_name }}</td>
<td>{{ __('app-text.indexEdit') }}</td>
<td><form action="{{ route('employee.delete', ['lang' => app()->getLocale(), 'employee' => $employee->id]) }}" method="post">
The controller function:
public function edit(Employee $employee)
{
$companies = Company::get();
return view('employee.edit', compact('employee', 'companies'));
}
The error:
TypeError
Argument 1 passed to App\Http\Controllers\EmployeesController::edit() must be an instance of App\Employee, string given
http://localhost:8000/fr/employee/edit/1
The routes:
Route::group(['prefix' => '{lang}'], function() {
Route::prefix('employee')->name('employee.')->group(function() {
Route::get('/edit/{employee}', 'EmployeesController#edit')->name('edit');
Route::patch('/edit/{employee}', 'EmployeesController#update')->name('update');
I'm trying to make the application a multi-language application so just after I added the lang variable the route won't pass the $employee->id variable. Should I add a variable that's passable to my controller for the lang variable?
any solution? Many thanks.
first you can make a route to change language
Route:: get('lang/{lang}', function ($locale) {
session(['locale' => $locale]);
return \Redirect::back();
})
step 2: middleware
public function handle($request, Closure $next)
{
App::setLocale(session('locale'));
return $next($request);
}
after you can make a group
Route::group(['middleware' => 'language'],function(){
//routes with u want change language
Route::get('/edit/{employee}', 'EmployeesController#edit')->name('edit');
Route::patch('/edit/{employee}', 'EmployeesController#update')->name('update');
});
and you forget to send the language in each route
Your parameters are wrong. As the stack trace says the controller method is expecting an instance of your Employee model but you are passing in a string
Change
public function edit(Employee $employee)
To
public function edit(Request $request, $employeeId) // you can remove $request if you dont intend to perform redirects
So in the end your code looks like
public function edit(Request $request, $employeeId)
{
$employee = Employee::find($employeeId);
$companies = Company::all(); // use all instead of get if you arent going to perform selections.
return view('employee.edit', compact('employee', 'companies'));
}
Note: you may need to handle cases where employee is not found based on the $employeeId supplied
I think you have to modify your routes like these
in web.php
Route::get('your-route/{lang}/{id}','YourController#edit');
In your controller
public function edit($lang,Employee $employee)
{
$companies = Company::get();
return view('employee.edit', compact('employee', 'companies'));
}
if you are passing locale as well in the route then it should be as below:
web.php
Route::get('your-Own-route/{lang}/{employee}','YourController#edit');
Controller edit method
public function edit($lang,Employee $employee)
{
$companies = Company::get();
return view('employee.edit', compact('employee', 'companies'));
}
PostController
public function index()
{
$posts=Post::all();
return view('home')->with('posts', $posts);
}
web.php
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::resource('posts','PostController');
home
#foreach($posts as $post)
<p>{{$post['content']}}</p>
#endforeach
I get this error
Facade\Ignition\Exceptions\ViewException
Undefined variable: posts (View: C:\xampp\htdocs\lts\resources\views\home.blade.php)
$posts are undefined
Make the variable optional in the blade template. Replace {{ $posts }} with {{ $posts ?? '' }}
Thanks for help everyone I was able to fix it by adding
Route::get('/home', 'PostController#index');
I would love to know why this problem was caused in first place Route:: resource('posts','PostController'); should have handled it.
There are two ways to do this:
First One:
If you want to use HomeController for /home route then add following code in the HomeController.
HomeController:
public function index()
{
$posts=Post::all();
return view('home')->with('posts', $posts);
}
Second One
You have used the resource method in the web.php so your 'PostController' URL starts from posts But you have used /home.
So change your route like this:
In web.php
Route::get('/home', 'PostController#index')->name('home');
Route::resource('posts','PostController');
Chnage in your controller
public function index()
{
$posts=Post::all();
return view('home')->with('posts', $posts);
}
change in your blade
#foreach($posts as $post)
{{ $post['content'] }}
#endforeach
Try this instead of what you have given for loop
#foreach($posts as $post)
{{ $post->content}}
#endforeach
Try replacing
return view('home')->with('posts', $posts);
with
return view('home', ['posts' => $posts]);
and
<p>{{$post['content']}}</p>
with
<p>{{ optional($post)->content }}</p>
I am trying to execute 2 queries at the same time on the same function on my application. This function on the controller registers a new house in the system and at the same time, its suppose to feed a field in the Users table that has been null, and this field will then be updated with the new id of the house i have just created. For some reason, the first query works fine but i have been struggling with the second. I believe my logical is fine, maybe the Laravel syntax is making me a bit confused. Can someone help me?
This is my controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\House;
use App\User;
class AdminHouseController extends Controller
{
public function index(){
}
public function create($role_id){
if($role_id == 1){
return view('admin.house.create');
}else{
return redirect('home');
}
}
public function store(Request $request){
House::create($request->all());
$house_admin = Auth::user()->id;
$house = House::where('house_admin', $house_admin)->first();
User::findOrFail($house_admin)->update(['house_id' => $house->id]);
return redirect('home');
}
public function show($id){
}
public function edit($id){
}
public function update(Request $request, $id){
}
public function destroy($id){
}
}
This is my form
//i believe the problem is not here, but anyway
#extends('layouts.app')
#section('content')
<p><b>Register your house</b></p>
{!! Form::open(['method'=>'post', 'action'=>'AdminHouseController#store']) !!}
{!! Form::text('house_address', null,['placeholder'=>'House Address']) !!}
<input type="hidden" name="house_admin" value="{{Auth::user()->id}}">
{!! Form::number('nflatmates', null, ['placeholder'=>'How many flatmates']) !!}
{!! Form::submit('Register', ['class'=>'ui-btn buttonDefault']) !!}
{!! Form::close() !!}
#stop
And finnaly, this my router web.php
//i also believe the problem is not here, but in my controller
use App\User;
Route::get('/', function () {
return view('welcome');
});
Auth::routes();
Route::get('/home', 'HomeController#index')->name('home');
Route::get('/house/{role_id}', 'AdminHouseController#create')->name('house');
Route::post('store', [
'uses' => 'AdminHouseController#store'
]);
You need to add the house_id field to the $fillable array in the User model:
protected $fillable = ['house_id', 'other_fields'];
Also, you have two foreign keys instead of one which is redundant. You might want to keep only house_admin foreign key.
I'm looking for some help. I've searched on other topics, and saw what is the problem approximatively, but didn't succeed to fix it on my code.
Now the question is: I have NotFoundHttpException when i try to submit an update on my code.
Here is the Controller and my function update
<?php
namespace App\Http\Controllers;
use Request;
use App\Http\Requests;
use App\T_collaborateurs_table;
class testing extends Controller
{
public function index()
{
$user = T_collaborateurs_table::all();
return view ("read", compact("user"));
}
public function create()
{
return view("create");
}
public function store(Request $Request)
{
T_collaborateurs_table::create(Request::all());
return redirect("index");
}
public function show($id)
{
$user=T_collaborateurs_table::find($id);
return view("show", compact("user"));
}
public function edit($id)
{
$user=T_collaborateurs_table::find($id);
return view("update", compact("user"));
}
public function update(Request $Request, $id)
{
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
}
Now the routes
Route::get("create", "testing#create");
Route::post("store", "testing#store");
Route::get("index", "testing#index");
Route::get("show/{id}", "testing#show");
Route::get("edit/{id}", "testing#edit");
Route::patch("update/{id}", "testing#update");
And now the view update.blade.php
<body>
{{Form::model($user, ['method'=>'patch', 'action'=>['testing#update',$user->id]])}}
{{Form::label('Id_TCa', 'ID')}}
{{Form::text('Id_TCa')}}
{{Form::label('Collaborateur_TCa', 'collab')}}
{{Form::text('Collaborateur_TCa')}}
{{Form::label('Responsable_TCa', 'resp')}}
{{Form::text('Responsable_TCa')}}
{{Form::submit("update")}}
{{Form::close()}}
</body>
Here the route:list
I'm sorry if my words are not very understable...
Thank you all for your time.
{{Form::model($user, ['method'=>'PATCH', 'action'=> ['testing#update',$user->id]])}}
Or try to use 'route' instead of 'action',to use 'route' you just need a little edit in your update route.
Route::patch("update/{id}", array('as' => 'task-update', 'uses'=>'testing#update'));
in your view:
{{Form::model($user, ['method'=>'PATCH', 'route'=>['task-update',$user->id]])}}
And please follow the convention of class naming. Your class name should be 'TestingController' or 'Testing'.
You could try method spoofing by adding
{{ method_field('PATCH') }}
in your form and change the form method to POST
{{ Form::model($user, ['method'=>'POST', 'action'=>['testing#update', $user->id]]) }}
add the id as an hidden field
{{ Form::hidden('id', $user->id) }}
access the id in the controller as
public function update(Request $Request)
{
$id = Input::get('id');
$user = T_collaborateurs_table::find($id);
$user->update(Request::all());
return redirect("index");
}
also need to modify your route accordingly
Route::patch("update", "testing#update");
Try using on function update:
return redirect()->route('index');
I'm trying to pass a variable from one view to a controller to another view. I'm not getting any errors, but when it gets to the last view, it doesn't show the variable like it's supposed to. In the first view, I'm just getting a name.
{{ Form::open(array('route' => 'form', 'method'=>'post')) }}
{{ $name = Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
Here is my HomeController.php.
public function view1()
{
return View::make('stuff');
}
public function postView1($name)
{
return Redirect::route('view2')->with($name);
}
public function view2($name)
{
return View::make('view2')->with($name);
}
routes.php
Route::get('/', array('as' => 'stuff', 'uses' => 'HomeController#stuff'));
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController#postView1'));
Route::get('view2/{name}', array('as' => 'view2', 'uses' => 'HomeController#view2'));
view2.blade.php
{{ $name = Input::get('name') }}
<p> Hello, {{ $name }} </p>
So why isn't it showing up?
First you should change your postView function into:
public function postView1()
{
return Redirect::route('view2', ['name' => Input::get('name')]);
}
And your route:
Route::post('form/{name}', array('as' => 'form', 'uses'=>'HomeController#postView1'));
into:
Route::post('form', array('as' => 'form', 'uses'=>'HomeController#postView1'));
Now, you should change your view2 function into:
public function view2($name)
{
return View::make('view2')->with('name',$name);
}
Now in your view2.blade.php you should be able to use:
<p> Hello, {{ $name }} </p>
You need to name the variable:
public function view2($name)
{
return View::make('view2')->with('name', $name);
}
class HomeController extends Controller {
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
}
public function index()
{
$data = array (
'title'=>'My App yo',
'Description'=>'This is New Application',
'author'=>'foo'
);
return view('home')->with($data);;
}
}
Try form will be if you using POST method why setting variable in route it will get directly on your function with post data.
{{ Form::open(array('url' => 'form', 'method'=>'post')) }}
{{Form::text('name') }}
{{ Form::submit('Go!') }}
{{ Form::close() }}
route :-
Route::post('form','HomeController#postView1');
controller function :-
public function postView1() {
$data = Input::all();
return Redirect::route('view2')->with('name', $data['name']);
}
and get data on view2 :-
<p> Hello, {{ $name }} </p>
For more follow HERE
Here's what other answers are missing, straight from Laravel docs:
Since the with method flashes data to the session, you may retrieve the data using the typical Session::get method.
So instead of {{$name}} write {{Session::get('name')}}.