Don't show the URL's parameters in Laravel 4.1 - laravel-4

I have a CRUD for Users in Laravel 4.1
When the user want see his data, the url is: mydomain.com/public/users/3 (the show method).
How I can hide the id "3" in the url? So, if the number is visible, the user can see the data of other users (as 4,5 or others id)?
Thanks
In my filter.php I have:
Route::group(array('before' => 'auth'), function()
{
Route::resource('users', 'UserController');
});

Finally solved.
In UserController.php:
public function show()
{
//
$usuario = Auth::user();
// show the view and pass the nerd to it
return View::make('users.show')
->with('elusuario', $usuario);
}
In show.blade.php
#extends ("layout/layout")
#section ("principal")
<div class="form-group">
{{ Form::label('nombre', 'Nombre') }}
{{ Form::text('nombre',null, array('class' => 'form-control','placeholder'=>$elusuario->nombre,'disabled'=>'disabled')) }}
</div>
<div class="form-group">
{{ Form::label('email', 'Email') }}
{{ Form::email('email',null, array('class' => 'form-control','placeholder'=>$elusuario->email,'disabled'=>'disabled')) }}
</div>
<a class="btn btn-success" href="{{ URL::to('users/' . $elusuario->id . '/edit') }}">Modificar datos</a>
#stop
That solved the issue and the url don't show the id of user. Simply called as mydomain.com/public/users/perfil and has the data (id, name, etc) from session variable.

Related

Return Logged User's Posts Into laravelcollective/html Select Dropdown Menu Within a Form

I am trying to pass only the posts created by the logged in user into a laravelcollective/html select drop down menu within a form.
In my code I have two examples. Using the variable example shows how I can get the dropdown select menu to show all results from the posts table. Using the variable posts in a foreach loop shows how I can return just the posts created by the logged user, but not in a select menu.
I need to have the dropdown menu function as in the form using example but displaying results of the foreach posts loop.
Controller
public function createPost()
{
$example = Post::pluck('title', 'id')->all();
$posts = Posts::all();
return view('post.create', compact('posts', 'example'));
}
Example View
<div class="form-group">
{!! Form::label('example', 'Posts:') !!}
{!! Form::select('example', ['' => 'Select'] + $example, null) !!}
</div>
Foreach Loop Posts View
#foreach($posts as $post)
#if(Auth::user()->id == $post->user_id)
{{ $post->title }} <br>
#endif
#endforeach
try $posts = Posts::where('user_id',\Auth::id())->get()->pluck('title','');. It will return only posts of logged in user.
{{ Form::select('example', $posts) }}
You are using select box wrong.
#foreach($posts as $post)
#if(Auth::user()->id == $post->user_id)
{{ $post->title }} <br>
#endif
#endforeach
I would update your controller to only return posts by the user rather than rely on the foreach check if Auth::user()->id == $post->user_id
public function createPost()
{
$posts = Posts::where('user_id', auth()->user()->id)->get();
return view('post.create', compact('posts'));
}
As a side note, your method should probably just be create() to keep inline with the standard CRUD.
Then in your blade,
<div class="form-group">
{!! Form::label('post', 'Posts:') !!}
{!! Form::select('post', ['' => 'Select'] + $posts, null) !!}
</div>

Laravel 5.0: Route with query string

I am using laravel 5.0, I am sending a query string on A tag with an id.
I am getting id but not the query string data
Below is the code:
View
<div class="col-xs-12 col-sm-4 col-md-4 col-lg-4">
<div class="img-decor">
<a href="{{url('buycarddetail/'.$retailer->id)}}" class="">
<img src="{{ assetnew('uploads/client_image/'.$retailer->image) }}" alt="..." class="img-rounded" width="200">
</a>
<div class="deals-title">
{{ $retailer->name }}
<div class="sub-details">Save up to {{ $retailer->discount }}%</div>
</div>
</div>
</div>
Controller
public function buycarddetail($id = null, Request $request)
{
echo $id;
echo '<pre>'; $data = $request->all(); exit;
return view('buycarddetail');
}
Route
Route::get('buycarddetail/{id}', ['as' => 'buycarddetail', 'uses' => 'HomeController#buycarddetail']);
I want to use the query string data for further process on controller
Please help
Based on your code you're not actually appending any query string when generating the link {{url('buycarddetail/'.$retailer->id)}}.
As per your comments you can do this to generate a link to your route with the query string.
{{ route('buycarddetail', ['id' => $retailer->id, '_token' => csrf_token(), 'brand' => 'test', 'buybrand' => 'example']) }}
This example would generate a link like
http://example.com/buycarddetail/17?_token=QHE8va7stXUOPabwTjKmXyJxdsuPSZ9VbH3uThwx&brand=test&buybrand=example

get id from url and passing into form in laravel 5

i defined a route as
Route::get('roundtables/{name}/tags/store',['as'=>'tags.store','uses'=>'TagController#store','middleware'=>['owner','auth']]);
In my view, i have a form in this url
http://localhost:8000/roundtables/1/tags
<div class="col-md-3">
<div class="well">
{!! Form::open(['route'=>'tags.store','method'=>'GET']) !!}
<h2>New Tags</h2>
{{ Form::label('name','Name') }}
{{ Form::text('name',null,['class'=>'form-control']) }}
{{Form::submit('Create New Tag',['class'=>'btn btn-primary btn-block btn-h1-spacing'])}}
</div>
</div>
My problem is, how to get the id from url which is id '1' and passing into the form when user clicked submit.
My controller
public function store(Request $request,$name)
{
$this->validate($request,array('name'=>'required|max:255'));
$tag=new Tag;
$tag->name=$request->name;
$tag->roundtable_id=$name;
$tag->save();
Session::flash('success','New Tag was successfully added');
return redirect()->route('tags.index');
}
When you're using custom routes for CRUD, avoid using standard RESTful method and route names. Before building the form, you need to pass this variable to the view:
public function createTag($name)
{
....
return view('form', compact('name'));
}
Define your route as:
Route::get('roundtables/{name}/tags/storeTag',['as'=>'tags.storeTag','uses'=>'TagController#storeTag','middleware'=>['owner','auth']]);
Then pass variable to from the form:
{!! Form::open(['route' => ['tags.storeTag', $name], 'method'=>'GET']) !!}
And get it in the controller:
public function storeTag(Request $request, $name)
{
echo $name;
You get the wildcard value by using request() helper method easily.
{{request()->route('name')}}
In your TagController
public function index($name)
{
$tags= Tag::where($name)->first();
// add name parameter to return
return view('tags.index')->withTags($tags)->withName($name);
}
And in your view
<div class="col-md-3">
<div class="well">
//edit this form tag
{!! Form::open(['route'=>['tags.store',$name],'method'=>'GET']) !!}
<h2>New Tags</h2>
{{ Form::label('name','Name') }}
{{ Form::text('name',null,['class'=>'form-control']) }}
{{Form::submit('Create New Tag',['class'=>'btn btn-primary btn-block btn-h1-spacing'])}}
</div>
</div>
And in your TagController#store
public function store(Request $request,$name){
echo $name;
}
This should work.
Request::segment(2)
Or pass it from controller:
public function index($name)
{
$tags= Tag::where($name)->first();
return view('tags.index')->withTags($tags)->with('name', $name);
}
Then just pass it into the form:
{!! Form::open(['route'=>['tags.store', $name],'method'=>'GET']) !!}

How to save post form in Laravel-4

I am creating parent email form in this form parent insert his email id.
I facing problem is when I submit form that showing error ,Method [save] does not exist
and my code is
registration form
<div class="form">
{{ Form::open(array('url' => '/api/v1/parents/registration_step_2', 'class' => "worldoo-form form form-horizontal", 'id' => "signupForm", 'method' => "post" )) }}
<div class="form-group ">
{{ Form::label('cemail', 'Please enter your parents e-mail:', array('class' => "control-label"));}}
<div class="">
{{ Form::email('cemail', '', array('class' => "form-control", 'id' => "cemail"));}}
<i class="sprite success form-control-feedback"></i> </div>
<h5 class="regular-font text-left">Your parents will need to activate your account before you can access worldoo.</h5>
</div>
<div class="form-group">
<div class="text-center">
{{Form::submit('Next', array('class' => "btn btn-primary"));}}
</div>
</div>
{{ Form::close() }}
</div>
my controller
public function registrationStepTwo()
{
$cemail = $_REQUEST['cemail'];
if($cemail != '')
{
$parent = new Parent;
$parent->email = $cemail;
$parent->save();
}
}
I had the save issues, with the exact same class name "Parent" at it also throws
Method [save] does not exist
The reason you can save the $parent model data is because of the name that you have given to the model as
Parent
Change the model name to something else and it will work as expected. Looks like the class Parent must be laravel's system class and we cannot give that name to our custom classes.
your Model is not set up correctly you find these in app/models/ there needs to be a Parent.php file that defines how laravel should use your information in the database. Eloquent Models - Laravel 4 Documentation
I have tested, this code is working properly. May be try composer update.
composer update

Laravel pre-filling multiple forms if validation failed

One of the coolest Laravel feature is, Laravel pre-filled the form fields if validation error occurred. However, if a page contain more than one form, and form fields have same name, Laravel pre-filling all forms fields.
For example:
I have a page where i have two forms to create new users or whatever.
<h1>Create user1</h2>
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name', null) }}
{{ Form::email('email', null) }}
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => 'foo/bar')) }}
{{ Form::text('name', null) }}
{{ Form::email('email', null) }}
{{ Form::close() }}
Controller
class UsersController extends BaseController
{
public function store()
{
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make(Input::all(), $rules);
if ($validation->fails()) {
return Redirect::back()->withInput()->withErrors($validation);
}
}
}
As i didn't fill up the email, Laravel will throw validation error and pre-filling the forms as following:
How to tell Laravel that do not fill-up the second form?
There's no Laravel way of doing this, but you can use HTML basic form arrays to make it work. You need to understand that you have to identify your forms and fields so Laravel knows exactly where the data came from and where to send it back to. If all your fields have the same name how could it possibly know?
This is a proof of concept that will work straight from your routes.php file.
As I did it all and tested here before posting the answer I used Route::get() and Route::post(), to not have to create a controller and a view just to test something I will not use. While developing this you will have to put this logic in a controller and in a view, where I think they are alredy in.
To test it the way it is, you just have to point your browser to the following routes:
http://yourserver/form
and when you push a button it will automatically POST tho the route:
http://yourserver/post
I'm basically giving all forms a number and giving the buttons the number that we will usin in Laravel to get the form data and validate it.
Route::get('form', function()
{
return Form::open(array('url' => URL::to('post'))).
Form::text('form[1][name]', null).
Form::email('form[1][email]', null).
'<button type="submit" name="button" value="1">submit</button>'.
Form::close().
Form::open(array('url' => URL::to('post'))).
Form::text('form[2][name]', null).
Form::email('form[2][email]', null).
'<button type="submit" name="button" value="2">submit</button>'.
Form::close();
});
And here we get the data, select the form and pass all of it to the validator:
Route::post('post', function()
{
$input = Input::all();
$rules = [
'name' => 'required',
'email' => 'required'
];
$validation = Validator::make($input['form'][$input['button']], $rules);
return Redirect::back()->withInput();
});
This is how you use it in a Blade view, now using 3 forms instead of 2 and you can have as many forms as you need:
<h1>Create user1</h2>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[1][name]', null) }}
{{ Form::email('form[1][email]', null) }}
<button type="submit" name="button" value="1">submit</button>
{{ Form::close() }}
</h1>Create user2</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[2][name]', null) }}
{{ Form::email('form[2][email]', null) }}
<button type="submit" name="button" value="2">submit</button>
{{ Form::close() }}
</h1>Create user3</h1>
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text('form[3][name]', null) }}
{{ Form::email('form[3][email]', null) }}
<button type="submit" name="button" value="3">submit</button>
{{ Form::close() }}
And you can even use a loop to create 100 forms in blade:
#for ($i=1; $i <= 100; $i++)
User {{$i}}
{{ Form::open(array('url' => URL::to('post'))) }}
{{ Form::text("form[$i][name]", null) }}
{{ Form::email("form[$i][email]", null) }}
<button type="submit" name="button" value="{{$i}}">submit</button>
{{ Form::close() }}
#endfor
Use old input with $request->flash().
https://laravel.com/docs/5.2/requests#old-input

Resources