laravel model binding for new record - php

i use model binding for profile update like this :
{{ Form::model($profile, [
'route' => [
'client.update'
,$profile->user_id
]
, 'method' => 'POST'
, 'class' => 'form-horizontal'
])
}}
but if the user has no profile information than the page get error because of :
$profile->user_id cos it does not exist in db
what can i do now ?
if the user has profile it could be update but not it could be save

{{ Form::model($profile, [
'route' => [
($profile->exists)
? 'client.update', $profile->user_id
: 'client.create'
]
, 'method' => 'POST'
, 'class' => 'form-horizontal'
])
}}
If the profile exists you know it's an existing record, so you can choose another route (and also another HTTP method. Typically you post for creation and put for update.)

Related

How to have multiple products that depend on user input Laravel

I am using the srmklive/laravel-paypal package for my paypal API.
Currently, in my view I have multiple instances of this code block with varying product ids
{!! Form::open(['action' => 'PaypalController#expressCheckout']) !!}
{{ Form::hidden('item', 1) }}
{{ Form::hidden('name', 'm') }}
{{ Form::hidden('price', 25) }}
{{Form::submit('Pay via Paypal', array('class' => 'btn-info btn'))}}
{!! Form::close() !!}
However, when I use this method with my getCart function,
if($product == 1) {
return [
'items' => [
[
'name' => 'Mystery Core',
'price' => 25,
'qty' => 1,
],
],
'return_url' => url('/paypal/express-checkout-success'),
'invoice_id' => config('paypal.invoice_prefix') . '_' . $invoice_id,
'invoice_description' => "Order #" . $invoice_id . " Invoice",
'cancel_url' => url('/'),
'total' => 25,
];
}
}
the variables return null since they're coming from paypal's end, giving me a null error.
"Trying to access array offset on value of type null"
How can I go about having multiple products? I have heard of the addOptions function but I cannot understand it.
Even a different package suggestion is welcome!

Laravel - Select input not keeping old value

I have a piece of code:
{!! Form::select('option_employee_review', old('option_employee_review', $employeeReviews), $employeeReviews, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
It saves the value to the database correct. When i go to edit the item again the select input does not keep the old value that's in the database. How do i make it so that the select input does keep its old value.
$employeeReviews:
[
2843 => "Medewerker review 1"
2849 => "Medewerker review 2"
]
I am not using your syntax, but something like this will do I think.
<option value="{{$channel->id}}" {{ (old("channel_id") == $channel->id ? "selected" : "" ) }}>{{$channel->title}}</option>
Second parameter to select function must be array of options.
Try changing it like this
{!! Form::select('option_employee_review', $employeeReviews, old('option_employee_review', $employeeReviews), ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}
Or based on your parent object let's say employee you can try
{!! Form::select('option_employee_review', $employeeReviews, $employee->option_employee_review, ['id' => 'option_employee_review', 'class' => 'form-control ']); !!}

PostsController#destroy not defined in Laravel

stuck with this error any help would be much appreciated. The error I'm getting is below:
Action App\Http\Controllers\PostsController#destroy not defined. (View: C:\xampp\htdocs\lsapp\resources\views\posts\show.blade.php)
I do have the "destroy" method in the PostsController and using the latest vision of Laravel.
{!!Form::open(['action' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
Try this.
I changed ACTION to URL.
{!!Form::open(['url' => ['posts', $post->id], 'method' => 'POST', 'class' => 'pull-right'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
Syntax gets changing in laravel versions frequently. So always check your version.
Right now i think you might be using Laravel version 5.4.36 or something.
So I think try by changing 'ACTION' to 'URL' as below.
{!!Form::open(['url' => ['PostsController#destroy', $post->id], 'method' => 'POST', 'class' => 'pull-right'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
Your action method is fine the way you wrote it. That error literally means that you don't have a controller named PostsController#destroy.
Just run:
php artisan route:list
If the controller exist, the name column would give you the prefix and method Post.destroy and in the same row the action column would give you the controller name PostsController#destroy. Obviously if those two conditions are not there; you get the error:
Action App\Http\Controllers\PostsController#destroy not defined.
You can try to use the action helper function like this action('WelcomeController#log_in') or you can just set the action to a route that provides you the controller.
You've missed a step.
Think back to what the action attribute in an html form looks like:
action="{{ you put a url here, not a controller action }}"
Then consider your routes file, where you should add whatever route you decide to reference within {{ }} above, and map it to your controller action in the routes file.
I'd advise doing this without using Form::, then you will understand it better (and it's no more complicated).
juste you need to change method form inside your form to delete like this :
{!!Form::open(['action' => ['PostsController#destroy', $post->id], 'method' => 'DELETE', 'class' => 'pull-right'])!!}
{{Form::hidden('_method', 'DELETE')}}
{{Form::submit('Delete', ['class' => 'btn btn-danger'])}}
{!!Form::close()!!}
in Laravel 8 full description of the route is required as follows:
{!!Form::open(['action' => ['App\Http\Controllers\PostsController#destroy', $post->id],
'method' => 'POST', 'class' => 'float-right'])!!}

backpack laravel crud custom column

I have a task - insert a custom column in one of our admin pages, where I can to call method from Model for each entry, generated by backpack CRUD generator.
I have found in official documentations statement that looks like what I need here:
https://laravel-backpack.readme.io/v3.0/docs/crud-columns-types#section-roll-your-own
But there is nothing about how to implement this in the controller right way.
So I have tried to do just like this
Controller:
$status = [
'label' => 'Status',
'name' => 'status',
'type' => 'text'
];
$this->crud->addColumn($status);
and as mentinoned in documentation, I have create my own blade file in
resources\views\vendor\backpack\crud\columns
Here it is:
{{-- status --}}
<td>{{ $entry->isBlocked }}</td>
Where isBlocked is method in my Model. I have an error about database and nothing is working.
Please say is it even possible to do what I wnat and if it is - please say how to do it right way both in view and controller
Thankyou!
Let's check your code
$status = [
'label' => 'Status',
'name' => 'isBlocked', // your column name
'type' => 'status' // your blade name, e.g status.blade.php
];
$this->crud->addColumn($status);
and inside status.blade.php
{{-- status --}}
<td>{{ $entry->{$column['name'] }}</td>
Any question, please comment

passing two parameter in form cant seem to figue out

Basically I have an event ID and User ID i need to pass in the form to store... however when i hit create it comes up with
Route pattern "/roles/{id}/{{id}}" cannot reference variable name "id" more than once.
However if i hit enter in the URL bar it works... so not to sure whats happening here... help would be greatful here the code.
Route file
// POST Add Users Race
Route::post('racehistory/{event_id}/store/{user_id}/race/', 'racehistoryController#store');
// GET Current Races
Route::get('events/currentRace', 'racingeventController#viewCurrentRace');
// GET Users
Route::get('events/{event_id}/users', 'racingeventController#users');
// GET Users with Group ID
Route::get('events/{event_id}/{group_id}', 'racingeventController#grouped');
// GET Add Users Race Form
Route::get('events/{event_id}/user/{user_id}/addrace', 'racingeventController#addUserRace');
// Add User to Event
Route::get('events/{event_id}/user/{user_id}', 'racingeventController#addUserToEvent');
// DELETE Remove User from Race Event
Route::get('events/{event_id}/delete/user/{user_id}', 'racingeventController#deleteUserToEvent');
// DELETE Race Event
Route::get('events/delete/{event_id}', 'racingeventController#destroy');
Route::resource('events', 'racingeventController');
Form View
{{ Form::open(array('class' => 'form-horizontal', 'method' => 'post', 'action' => array('racehistoryController#store', $user->id, $event->id))) }}
Controller - racehistoryController
public function store($event_id, $user_id)
{
$rules = array(
'start_event' => 'required',
'end_event' => 'required',
'pool_type' => 'required|max:3|min:3',
'name' => 'required|max:35|min:3',
'location' => 'required|max:35|min:3',
);
$validator = Validator::make(Input::all(), $rules);
if ($validator->fails()) {
return 'form works';
}
}
Name your route:
Route::get(
'racehistory/{event_id}/store/{user_id}/race/',
['as' => 'store', 'uses' => 'racehistoryControlle#store']
);
Fix Form::open() helper:
{{
Form::open([
'class' => 'form-horizontal',
'method' => 'post',
'route' => ['store', $user->id, $event->id]
])
}}

Categories