I'm new with Laravel and I'm having problems while I try to detach some values from my pivot tables.
The pivot tables I'm woking with are:
excercise_workout (ejercicio_rutina) & excise_day (ejercicio_dia) (those tables have just two id's each one). The relations are ManyToMany. A workout have a lot of days and a day have a lot of excercises.
I've created these routes:
Route::resource('rutinas','RutinasController');
Route::delete('rutinas.destroyEjercicio/{id}','RutinasController#destroyEjercicio');
I have this button that is a form and call the delete:
{!! Form::open([
'method' => 'DELETE',
'action' => [ 'RutinasController#destroyEjercicio', $ejercicio->id ]
]) !!}
{!! Form::submit('Borrar Ejercicio', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
The destroyEjercicio is the part I don't know how to hadle with:
public function destroyEjercicio($id)
{
$ejercicio = Ejercicio::findOrFail($id);
dias()->$ejercicio->detach($id);
//EXAMPLE DELETE WITH QUERY
// DB::delete('delete from toy_user where toy_id = ? AND user_id = ? AND status = 0', array($toy->id,Auth::id()));
return redirect('rutinas');
}
I recieve this error when I try to submit the delete form:
Call to undefined function App\Http\Controllers\dias()
I've tried a lot of other methods but I don't know how to do it.
I need to know the ID of the day and the ID of the excercise I want to delete. And I don't know how the past it by the post request.
UPDATE -> THIS IS THE FORM I DO NOT HAVE INPUTS TO REQUEST...
#foreach ($rutina->dias as $dia)
<div class="col-md-6 col-md-offset-3">
<div class="panel panel-default">
<div class="panel-body">
Ejercicios para el <strong>{{ $dia->nombre }}</strong>
<hr>
#foreach ($dia->ejercicios as $ejercicio)
<ul>
<li>{{ $ejercicio->nombre }}</li>
</ul>
{!! Form::open([
'method' => 'DELETE',
'action' => [ 'RutinasController#destroyEjercicio', $ejercicio->id ]
]) !!}
{!! Form::submit('Borrar Ejercicio', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
#endforeach
</div>
</div>
</div>
#endforeach
You need something like this:
public function destroyEjercicio($ejercicioId, $diasId)
{
$ejercicio = Ejercicio::findOrFail($ejercicioId);
$ejercicio->dias()->detach($diasId);
https://laravel.com/docs/5.1/eloquent-relationships#inserting-many-to-many-relationships
Update
How to use Request object. If you're using forms, Reuqest object will contain all form fields. Just do something like this:
public function destroyEjercicio(Request $request)
{
$ejercicioId = $request->get('ejercicioIdField');
$diasId = $request->get('diasIdField');
I solved this the following way. First I create this new route:
Route::post('rutinas.destroyEjercicio/{ejercicioId}/{diaId}', [
'as' => 'destroy',
'uses' => 'RutinasController#destroyEjercicio'
]);
You must add this array at the form:
{!! Form::open([
'method' => 'POST',
*******'route' => ['destroy', $ejercicio->id, $dia->id]******
]) !!}
{!! Form::submit('Borrar Ejercicio', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
Then in your controller you pass the two id's that you need:
public function destroyEjercicio($ejercicioId, $diaId)
{
$ejercicio = Ejercicio::findOrFail($ejercicioId);
$ejercicio->dias()->detach($diaId);
return redirect('rutinas');
}
Related
I´ve this error: The DELETE method is not supported for this route. Supported methods: GET, HEAD, POST.
But i´m not trying to delete something
My view
{!! BootForm::open(['id'=>'form', 'method' => 'POST','route'=>['declaracionenviar.enviarTodo']]); !!}
{!! BootForm::checkboxElement('ids[]', ' ', $declaracion->id, '', false, ["class"=>'check i-checks']); !!}
{!! Form::submit('Send something',['class' => 'btn btn-primary']); !!}
{!! BootForm::close() !!}
My route
Route::post('/declaracionenviar/enviarTodo','DeclaracionEnviarController#enviarTodo')->name('declaracionenviar.enviarTodo');
My controller
public function enviarTodo(Request $request)
{
$ids= $request->get('ids');
dd($ids);
}
I would appreciate some help!
I am using laravel chart(laraveldaily)
controller
$options = [
'chart_title' => 'Animals by type',
'chart_type' => 'pie',
'report_type' => 'group_by_relationship',
'model' => 'App\Animal',
'relationship_name' => 'type', // represents function type() on Transaction model
'group_by_field' => 'category', // types.category
'filter_months' => 12, // show only transactions for last 30 days
];
$chart2 = new LaravelChart($options);
return view('admin.index', compact('users', 'chart2'));
In my view I have
<div class="row pl-lg-5" style="width: 40%;">
<h1>{{ $chart2->options['chart_title'] }}</h1>
{!! $chart2->renderHtml() !!}
<hr>
</div>
#section('scripts')
{!! $chart2->renderJs() !!}
{!! $chart2->renderChartJsLibrary() !!}
#endsection
I am getting only the title and there is no chart
Include the ChartJS library from CloudFlare before you render the JS code that uses Chart
#section('scripts')
{!! $chart2->renderChartJsLibrary() !!}
{!! $chart2->renderJs() !!}
#endsection
Hope this helps
I wish to make search query by datepicker and select field.
How could I get the requests values from below view file to controller?
Where could I modify in the code? thanks.
index.blade.php
<div class="form-group col-sm-6">
{!! Form::open(array('class' => 'form', 'method' => 'get', 'url' => url('/pdfs/job_finished_search'))) !!}
{!! Form::input('text', 'datepicker_from', null, ['placeholder' => 'Fra', 'id' => 'datepicker_from']) !!}
{!! Form::input('text', 'datepicker_to', null, ['placeholder' => 'Til', 'id' => 'datepicker_to']) !!}
{!! Form::select('customer_name', $jobs->pluck('customer_name', 'customer_name')->all(), null, ['class' => 'form-control']) !!}
{!! Form::submit('Søke', ['class' => 'btn btn-success btn-sm']) !!}
{!! Form::close() !!}
</div>
Controller.php
public function job_finished_search(Request $request, Job $jobs)
{
$jobs = Job::onlyTrashed()
->whereBetween('created_at', array(
(Carbon::parse($request->input('datepicker_from'))->startOfDay()),
(Carbon::parse($request->input('datepicker_to'))->endOfDay())))
->where('customer_name', 'like', '%'.$request->customer_name.'%')
->orderBy('deleted_at', 'desc')
->paginate(15);
if (empty($jobs)){
Flash::error('Search result not found');
}
return view('pdfs.index', ['jobs' => $jobs]);
}
There are multiple way to get the request data e.g to get datepicker_from value you can use any of the below
$request->datepicker_from
$request->input('datepicker_from')
$request->get('datepicker_from')
choose the one you like the most
refer to https://laravel.com/docs/5.5/requests
To get request values you can use the get method, try:
$customer = $request->get('customer_name','default_value');
To get request values, you can set an object like $input= $request->all(). Then you can make use of the object which is an array to get at specific fields, e.g to access your date picker, you can write $input['datepicker_from']. You need to place $input= $request->all() before you declare the $jobs object in your code.
I am trying to build a site and I get to the point where I would like to exploit the Form model binding
I set in the boot method of the RouteServiceProvider the binding between 'material' and 'App\Material' class
This is the creation Form:
{!! Form::open(array('url'=>'material', 'files' => true, 'class' => 'form-horizontal')) !!}
#include('admin_panel.partials.material_form',['submitButtonText' => 'Add'])
{!! Form::close() !!}
This is the include file:
{!! Form::label('title','Title',array('class' => 'control-label col-sm-2')) !!}
{!! Form::text('title','',array('class' => 'form-control')) !!}
{!! Form::label('published_at', 'Ready on:') !!}
{!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}
{!! Form::label('file','Choose the material: ',array('class' => 'col-sm-2')) !!}
{!! Form::file('file') !!}
{!! Form::submit($submitButtonText, array('class' => 'btn btn-warning')) !!}
The operation of creating the material and save it to the db works perfectly.
When i want to edit a material before calling the edit view in the edit method of the MaterialController I use dd($material) to check if it is the correct object. The attributes (title, published_at,...) in the object on the screen are the correct one, so I am sure that the object I am passing to the edit view is the right one.
When i call the Edit View I print {!! $material->title !!} before the Form and the string is correct. This is the Form:
{!! Form::model($material, array('method' => 'PATCH', 'route'=>array('material.update', $material), 'files' => true, 'class' => 'form-horizontal')) !!}
#include('admin_panel.partials.material_form',['submitButtonText' => 'Update']);
{!! Form::close() !!}
The problem is that i don't see anything in the fields of the form...
I don't know where I made a mistake.
Thank you.
Well I made a stupid mistake.
In the Form::text (and I think also the other fields) of the include form I forced the second argument (the text displaying in the input text form to be empty instead of NULL, thus overriding the value of the bind model.
my mistake.
I can't seem to find a way to put image as submit button in blade, is there a way to do this?
{!! Form::submit('Search', array('class'=>'btn')) !!}
Form::input should do the trick:
{!! Form::input('image', 'Search', array('class'=>'btn', 'src' => '...')) !!}
You can create the submit with pure html instead of laravelcollective like that
<input type="image" src="sourse for image" width="48" height="48" alt="Submit" />
For Laravel Collective version 6.2 the solution This small example shows the sending of a variable by means of a button with an image (src='../path/to/file) using the 'submit' passing as parameters the dimensions of the button (30,30).
Note: var_to_send can have a string like $ansver - > 'Hello World' or another value and it will pass it from the controller and it will print it.
// new.blade.php
{!! Form::open([ 'action'=> 'StorageController#save', 'method' => 'POST', 'files' => true]) !!}
{!! Form::hidden('var_to_send', $answer) !!}
{!! Form::image(url('../img/A.png'), 'submit', ['width' => '30', 'height' => '30']) !!}
{!! Form::close() !!}
// StorageController.php
class StorageController extends Controller
{
public function save(Request $request)
{
return $request->input('var_to_send');
}
}
// route wep.php
Route::post('storage/create', 'StorageController#save');