Laravel 5.5 - Save multiple data continiously from blade - laravel

I want to make a PHP method using laravel. I want to do the comparison of criteria and criteria. Here is the controller code :
public function create()
{
$kriteria1 = Model\Kriteria::pluck('nama_kriteria', 'id');
$kriteria2 = Model\Kriteria::pluck('nama_kriteria', 'id');
return view('kriteria_kriterias.create')->with('kriteria1', $kriteria1)->with('kriteria2', $kriteria2)->with('data', $data);
}
and this is the blade code :
It will make the form appear as total of criteria#
The problem is, I can't save it all to database. How do I get it to do this?

Updated method in the controller to the following:
public function create()
{
$kriteria1 = Model\Kriteria::pluck('nama_kriteria', 'id');
$kriteria2 = Model\Kriteria::pluck('nama_kriteria', 'id');
$data = [
'kriteria1' => $kriteria1,
'kriteria2' => $kriteria2
];
return view('kriteria_kriterias.create')->with($data);
}
How to output in the blade file:
{{ $kriteria1 }}
{{ $kriteria2 }}
Or you update the controller to pass the complete results:
public function create($id1, $id2)
{
$kriteria1 = Model\Kriteria::find($id1);
$kriteria2 = Model\Kriteria::find($id2);
$data = [
'kriteria1' => $kriteria1,
'kriteria2' => $kriteria2
];
return view('kriteria_kriterias.create')->with($data);
}
And the in the blade you can accss the data in various ways, one way is a foreach loop using blade in the blade template:
#foreach($kriteria1 as $k1)
{{ $k1 }}
#endforeach
#foreach($kriteria2 as $k2)
{{ $k2 }}
#endforeach'
To accept multiple values dynamicaly in the controller you can try something like this:
public function create($ids)
{
$results = collect([]);
foreach($ids as $id) {
$kriteria = Model\Kriteria::findOrFail($id);
if($kriteria) {
$results->put('kriteria' . $id, $kriteria);
}
}
return view('kriteria_kriterias.create')->with($results);
}
Then use the same looping method mentioned above to display them in the blade or a for loop that gets the count and displays accordingly.

maybe you forgot to add the opening tag ;)
{!! Form::open(array('url' => 'foo/bar')) !!}
//put your code in here (line 1-34)
{!! Form::close() !!}

Related

laravel controller returns only 2 values

I have a database with 3 tables. A separate model is connected to each table, and there is a controller that accepts values from all models. On the site page, I will have 3 tables that will be populated from a mysql table.
When I connected 2 models, everything worked fine. But after connecting 3, I get an error
undefined variable: sec_3.
If you delete one of the variables, then everything will work fine. It seems to me that the problem is either with the controller or with the file blade.php but I do not know how to fix it so that everything works properly. How to fix it?
My code:
Controller:
class PreschoolInstitution3Controller extends Controller {
public function index(){
$context=['bbs' =>PreschoolInstitution3::latest()->get()];
$context_2=['s' =>PreschoolInstitution::latest()->get()];
$context_3=['sec_3' => TrainingPrograms::latest()->get()];
return view('our_employees', $context, $context_2, $context_3);
}
}
web.php:
Route::get('/OurEmployees',[PreschoolInstitution3Controller::class,'index'] )->name('OurEmployees');
blade.php:
#foreach ($s as $section_2) <tr> <td>{{$section_2->number}}<td> <td>{{$section_2->fullname }}<td> <td>{{$section_2->post }}<td> <td>{{$section_2->telephone }}</td> <td>{{$section_2->email }}</td>
#endforeach #foreach ($bbs as $section )
{{$section->number}} {{$section->full_name}} {{$section->post}} {{$section->education}} {{$section->category}} {{$section->teaching_experience}} {{$section->professional_development}}
#endforeach #foreach ($sec_3 as $section_3)
{{ $section_3->number }}
{{ $section_3->level }}
{{ $section_3->directions }}
{{ $section_3->type_of_educational_program }}
{{ $section_3->period_of_assimilation }}
{{ $section_3->number_of_students }}
#endforeach
You should pass an array of data to view:
class PreschoolInstitution3Controller extends Controller {
public function index(){
$context = [
'bbs' => PreschoolInstitution3::latest()->get(),
's' => PreschoolInstitution::latest()->get(),
'sec_3' => TrainingPrograms::latest()->get()
];
return view('our_employees', $context);
}
}
https://laravel.com/docs/9.x/views#passing-data-to-views
Another one is that add a second parameter of an array with name to view( ) .
class PreschoolInstitution3Controller extends Controller {
public function index(){
$bbs = PreschoolInstitution3::latest()->get();
$s = PreschoolInstitution::latest()->get();
$sec_3 = TrainingPrograms::latest()->get();
return view('our_employees', [
'bbs' => $bbs,
's' => $s,
'sec_3' => $sec_3
]);
}
}

When rendering a model, how to use a blade template instead of json as default?

Is it possible to assign a blade template to a model?
Instead of doing this:
#php $contact = Contact::find(1); #endphp
#include('contact', ['contact' => $contact])
I'd like to just do:
#php $contact = Contact::find(1); #endphp
{{ $contact }}
But latter obviously just spits out the model in json.
It is possible with PHP's __toString() magic method: https://www.php.net/manual/en/language.oop5.magic.php#object.tostring
Let's make an example for default User.php model.
First, create a blade file for that model, lets create that as /resources/views/model/user.blade.php and a dummy component;
<h1>{{ $user->name }}</h1>
<p>{{ $user->created_at->diffForHumans() }}</p>
Now make this default __toString() for User model.
Add this to app/Models/User.php ;
/**
* #return string
*/
public function __toString(): string
{
return view('model.user', ['user' => $this])->render();
}
Now you can test it directly in your routes/web.php ;
Route::get('test', function () {
echo \App\Models\User::first();
});
Or try to echo it in any view as;
{!! $user !!}
You can't use {{ $user }} because you need that HTML tags, so you have to use it as {!! $user !!}

Undefined property: Illuminate\Database\MySqlConnection::$quizUser

I run the laravel code for fetching the data from database but i got the error.
This is controller's code name is UserProfileController.blade.php
public function FetchUserQus()
{
$data = DB::table('userquestion')->where('userEmail', '=', '{{
Auth::user()->email }}');
return view('designpages/userqus', ['data' => $data]);
}
Route::get('designpages/userqus',
'UserProfileController#FetchUserQus')->name('designpages/userqus');
This is view page code saved with name designpages/userqus.blade.php
#foreach($data as $datas)
<p><b>Question: {!! $datas->quizUser !!}</b></p>
<p><b>Answer: </b>{!! $datas->ansAdmin !!}</p>
#endforeach
You cannot use blade syntax in the controller: So change this:
$data = DB::table('userquestion')->where('userEmail', '=', '{{ Auth::user()->email }}');
to this:
$data = DB::table('userquestion')->where('userEmail', '=', auth()->user()->email)->get();
And also I use get() to return a collection, without it it returns a Illuminate\Database\Query\Builder instance.

Missing required parameters for in a Update Form Laravel 5.2

I've been working on a webapp recently in laravel and i wanted to have a eddit function within tthe application. but im getting this error Missing required parameters for [Route: producten.update] [URI: producten/{producten}], and i dont know what i've done wrong.
This is the Routes im using:
Route::resource('producten', 'ProductenController', ['only' => ['index', 'store', 'update', 'delete', 'edit', 'destroy', 'create']]);
This is the controller function im using for showing the edit page and updating.
The Edit function
public function edit(Request $request, Product $product)
{
// $product = Product::FindorFail($id);
// Product is a table with all products, with sellprice and buy price
// fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
The Update Function:
public function update(Request $request, Product $product)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
and this is the view i use for it.
{{Form::model($model, ['method' => 'PATCH', 'action' => 'ProductenController#update', $model ]) }}
{{ Form::label('naam:')}}
{{ Form::text('naam') }} <br>
{{ Form::label('inkoopPrijs:')}}
{{ Form::text('inkoopPrijs') }} <br>
{{ Form::label('verkoopPrijs:') }}
{{ Form::text('verkoopPrijs') }} <br>
{{Form::label('Fabrieken', 'Fabrieken Id:') }}
{{ Form::select('Fabrieken_Id', $fabrieks)}} <br>
{{ Form::submit('edit')}}
{{ Form::close() }}
if there is anything else that i need to add to the question just let me know and i'll add it
Missing thing is the id you are not getting id there in your edit function
your edit function should as i am assuming that you are just showing the form from this method where user can edit
public function edit($id)
{
$product = Product::FindorFail($id);
//Product is a table with all products, with sellprice and buy price
//fabriek = table that has a foreign key attached to the product table
return view('producten.edit', [
'model' => $product,
'fabrieks' => Fabriek::lists('Id')
]);
}
your update method should seem like this
public function update(Request $request, $id)
{
$product->update($request->all());
return redirect(Route('producten.index'));
}
your routes should like this no need for only
Route::resource('/producten', 'productionController');
edit route will be as
<a href="{{ route('production.edit', $model->id) }}">
Try this hope it will help

Laravel - How to return last value from user input into a view

I'm building a backend for my website in Laravel. I need only few text boxes to populate a template. So far I've managed to do that, but every time I pass a value to a template I see also all previous values, not only the last one.
So for example if I'm editing slider title, I see also all previous slider titles - I want to see only last one.
My controler:
public function store()
{
$input = Input::all();
homepage::create([
'slider_title' => $input['slider_title'],
]);
return Redirect::to('/');
}
Part of view responsible for displaying slider title:
#foreach ($homepage as $home)
{{$home->slider_title}}
#endforeach
View for user input in admin:
{{ Form::open(['url' => '/admin']) }}
<div>
{{ Form::label('slider_title', 'Slider title:') }}
{{ Form::text('slider_title') }}
</div>
<div>
{{ Form::submit('Change a slider title') }}
</div>
{{ Form::close() }}
Route for admin:
Route::post('/admin', 'AdminController#store');
Route for main page:
Route::get('/', 'PageController#home');
PageController home method:
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
I tried changing PageControl with:
$homepage = Homepage::orderBy('id', 'desc')->first();
but I'm getting this error:
ErrorException: Trying to get property of non-object on my view page
public function home()
{
$homepage = Homepage::all();
return View::make('home')->with('homepage',$homepage);
}
You are seeing all slides because you are selecting all of them:
$homepage = Homepage::all();
This should give you the last one:
$homepage = Homepage::orderBy('id', 'desc')->first();
or
$homepage = Homepage::orderBy('created_at', 'desc')->first();
Remember that now you are getting only one result and you cannot use foreach anymore:
foreach ($homepage as $home)
You can refer to it directly:
$homepage->slider_title
If you still want to return an array to use foreach, this is an option:
$homepage = array( Homepage::orderBy('created_at', 'desc')->first() );

Resources