Laravel : Trying to access array offset on value of type null - laravel

I have the following in my livewire file to create a contact and create an new entreprise for him.
public $contact;
public $entreprise;
public function saveContact()
{
$this->validate();
$newentreprise = Entreprise::create([
'nom' => $this->entreprise['nom'],
'adresse' => '1',
'cle' => Str::random(32),
'code_postal' => '95959',
'ville' => 'meknes',
]);
Contact::create([
'nom' => $this->contact['nom'],
'prenom' => $this->contact['prenom'],
'e_mail' => $this->contact['e_mail'],
'cle' => Str::random(32),
'telephone_mobile' => '',
'telephone_fixe' => '',
'entreprise_id' => $newentreprise->id,
]);
$this->confirmingContactAdd = false ;
}
The contact is perfectly created, but I have a problem in the creation of the entreprise. It seems like the input of 'nom' in $newentreprise is not requested correctly. Where do I have the problem ?
contact.blade.php of displaying the inputs ( The 3 inputs of contact are requested correctly ) :
<div class="flex mt-6">
<div class="flex-1 text-left mr-2"><div class="col-span-6 sm:col-span-4">
<x-jet-label class="font-bold" for="prenom" value="{{ __('Prénom') }}" />
<x-jet-input id="prenom" type="text" class="mt-1 block w-full" wire:model.defer="contact.prenom" />
<x-jet-input-error for="contact.prenom" class="mt-2" />
</div></div>
<div class="flex-1 text-left ml-2"><div class="col-span-6 sm:col-span-4">
<x-jet-label class="font-bold" for="nom" value="{{ __('Nom') }}" />
<x-jet-input id="nom" type="text" class="mt-1 block w-full" wire:model.defer="contact.nom" />
<x-jet-input-error for="contact.nom" class="mt-2" />
</div></div>
</div>
<div class="flex-1 text-left mt-4"><div class="col-span-6 sm:col-span-4">
<x-jet-label class="font-bold" for="e-mail" value="{{ __('E-mail') }}" />
<x-jet-input id="e-mail" type="text" class="mt-1 block w-full" wire:model.defer="contact.e_mail" />
<x-jet-input-error for="contact.e_mail" class="mt-2" />
</div></div>
<div class="flex-1 text-left mt-4"><div class="col-span-6 sm:col-span-4">
<x-jet-label class="font-bold" for="entreprise" value="{{ __('Entreprise') }}" />
<x-jet-input id="entreprise" type="text" class="mt-1 block w-full" wire:model.defer="contact.entreprise.nom" />
<x-jet-input-error for="contact.entreprise.nom" class="mt-2" />
</div></div>

Related

AlpineJS x-for showing an extra iteration

Having a strange issue, when using x-for to loop through an array i am getting an additional blank set of form elements. If i take out all of the code between the templates and just replace with a h1 with x-text as event.name - i only get the 2 iterations.
Puling my hair out, any ideas why this might be occuring? Here is the code.
Appreciate your assistance.
I have confirmed that the array being passed in only has 2 entries.
<div x-data="updateEvent()">
<template x-for="event in events">
<form action="test10" #submit.prevent="submitData">
<div class="grid grid-cols-4 gap 4 flex">
<div>
</div>
<div class="flex items-center content-center justify-center py-5">
<img class="w-full rounded" :src="imagePath + event.image" alt="" />
</div>
<div>
<div class="pl-4 my-10">
<div>
<x-label for="name" :value="__('Name')" class="mb-2" />
<x-input id="name" class="block mt-1 w-full" type="text" name="name"
::value="" x-model="event.name" x-bind:disabled="!disabled" required
autofocus />
</div>
<div>
<x-label for="date" :value="__('Date')" />
<x-input id="date" class="block mt-1 w-full" type="text" name="date"
::value="" x-model="event.date" required autofocus />
</div>
<div>
<x-label for="statsLink" :value="__('Stats Link')" />
<x-input id="statsLink" class="block mt-1 w-full" type="text"
name="statsLink" ::value="" x-model="event.statsLink" required
autofocus />
</div>
<div>
<x-label for="frontLink" :value="__('Front Link')" />
<x-input id="frontLink" class="block mt-1 w-full" type="text"
name="frontLink" ::value="" x-model="event.frontLink" required
autofocus />
</div>
</div>
</div>
<div class="flex items-center content-center justify-center ">
<x-button class="ml-3" x-on:click="disabled = !disabled, save = !save">
Edit Event
</x-button>
<br />
<x-button class="ml-3" x-bind:disabled="!disabled">
Update Event
</x-button>
</div>
</div>
</form>
</template>
</div>
I have tried cutting down the code to H1 and then it seems to iterate correctly - only twice.
Adding in the updateEvent() code for reference.
`<script>
function updateEvent() {
return {
events: [{
id: 1,
name: 'Event Name 1',
date: '2023-02-11 00:00:00',
image: '/eventCoverImages/EventCoverImage1.jpg',
cards: null
},
{
id: 2,
name: 'Event Name 2',
date: '2023-01-14 00:00:00',
image: '/eventCoverImages/EventCoverImage2.jpg',
cards: null
}
],
show: true,
disabled: false,
save: true,
imagePath: "{{ asset('app') . '/' }}",
}
}
`

Laravel : Next Record in a random Order

im creating a quiz app for driving lessons. the quiz page have all the data but i want to do next button to take you to the next question but in in a random order.
this is my controller's function for the next but it's not working But it worked with me in another project
public function submitans(Request $request) {
$nextq = Session::get('nextq');
$wrongans = Session::get('wrongans');
$correctans = Session::get('correctans');
$validate = $request->validate([
'ans' => "required",
'dbans' => 'required'
]);
$nextq = Session::get('nextq');
$nextq += 1;
if ($request->dbans == $request->ans) {
$correctans += 1;
} else {
$wrongans += 1;
}
Session::put("nextq", $nextq);
Session::put("wrongans", $wrongans);
Session::put("correctans", $correctans);
$i = 0;
$questions = question::all();
foreach ($questions as $question) {
$i++;
if ($questions->count() < $nextq) {
return view('pages.end');
}
if ($i == $nextq) {
// $question = Question::where('id', '>', $question->id)->orderBy('id')->first();
return view('pages.questions.quiz')->with(['question' => $question]);
}
}}
and this and this is a part of the view :
<form method="POST" action="/submitans">
#csrf
<h5 class="mt-1 ml-2">{{ $question->title }}</h5>
</div>
<div class="text-center">
<img src="{{ asset('storage/' . $question->image) }}" alt="image" class="rounded">
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" checked="true" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse1 }}</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse2 }}</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse3 }}</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="flexCheckDefault"
name="ans" />
<label class="form-check-label" for="flexCheckDefault">{{ $question->reponse4 }}</label>
</div>
<input value="{{ $question->ans }}" style="visibility: hidden" name="dbans">
</div>
<div class="d-flex flex-row justify-content-between align-items-center p-3 bg-white">
<button class="btn btn-primary border-success align-items-center btn-success"
type="submit">Next<i class="fa fa-angle-right ml-2"></i></button>
and the routing is :
Route::get('/quiz',[QuestionController::class,'index']);
Route::any('/submitans', [QuestionController::class, 'submitans']);

Laravel Jetstream Livewire undefined variable

I duplicated <x-slot name="form"> with <x-slot name="form2"> and inserted variable $form2 in form-section.blade.php but get an error for undefined $form2. I don't know why update-profile-information-form.blade.php didn't send $form2 HTML. I am using Laravel 8.
Error: ErrorException Undefined variable: form2 (View:
\resources\views\vendor\jetstream\components\form-section.blade.php)
form-section.blade.php
#props(['submit'])
<div {{ $attributes->merge(['class' => '']) }}>
<x-jet-section-title>
<x-slot name="title">{{ $title }}</x-slot>
<x-slot name="description">{{ $description }}</x-slot>
</x-jet-section-title>
<div class="md:grid md:grid-cols-2 md:gap-6">
<div class="mt-2 md:mt-0 md:col-span-1">
<form wire:submit.prevent="{{ $submit }}">
<div class="px-4 py-5 bg-white sm:p-6 shadow {{ isset($actions) ? 'sm:rounded-tl-md sm:rounded-tr-md' : 'sm:rounded-md' }}">
<div class="grid grid-cols-6 gap-6">
{{ $form }}
</div>
</div>
#if (isset($actions))
<div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
{{ $actions }}
</div>
#endif
</form>
</div>
<div class="mt-2 md:mt-0 md:col-span-2">
<form wire:submit.prevent="{{ $submit }}">
<div class="px-4 py-5 bg-white sm:p-6 shadow {{ isset($actions) ? 'sm:rounded-tl-md sm:rounded-tr-md' : 'sm:rounded-md' }}">
<div class="grid grid-cols-6 gap-6">
{{ $form2 }}
</div>
</div>
#if (isset($actions))
<div class="flex items-center justify-end px-4 py-3 bg-gray-50 text-right sm:px-6 shadow sm:rounded-bl-md sm:rounded-br-md">
{{ $actions }}
</div>
#endif
</form>
</div>
</div>
</div>
update-profile-information-form.blade.php
<x-jet-form-section submit="updateProfileInformation">
<x-slot name="title">
{{ __('Profile Information') }}
</x-slot>
<x-slot name="description">
{{ __('Update your account\'s profile information and email address.') }}
</x-slot>
<x-slot name="form">
<!-- Name -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="state.name" autocomplete="name" />
<x-jet-input-error for="name" class="mt-2" />
</div>
<!-- Email -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="email" value="{{ __('Email') }}" />
<x-jet-input id="email" type="email" class="mt-1 block w-full" wire:model.defer="state.email" />
<x-jet-input-error for="email" class="mt-2" />
</div>
</x-slot>
<x-slot name="form2">
<!-- Profile Photo -->
#if (Laravel\Jetstream\Jetstream::managesProfilePhotos())
<div x-data="{photoName: null, photoPreview: null}" class="col-span-6 sm:col-span-4">
<!-- Profile Photo File Input -->
<input type="file" class="hidden"
wire:model="photo"
x-ref="photo"
x-on:change="
photoName = $refs.photo.files[0].name;
const reader = new FileReader();
reader.onload = (e) => {
photoPreview = e.target.result;
};
reader.readAsDataURL($refs.photo.files[0]);
" />
<x-jet-label for="photo" value="{{ __('Photo') }}" />
<!-- Current Profile Photo -->
<div class="mt-2" x-show="! photoPreview">
<img src="{{ $this->user->profile_photo_url }}" alt="{{ $this->user->name }}" class="rounded-full h-20 w-20 object-cover">
</div>
<!-- New Profile Photo Preview -->
<div class="mt-2" x-show="photoPreview">
<span class="block rounded-full w-20 h-20"
x-bind:style="'background-size: cover; background-repeat: no-repeat; background-position: center center; background-image: url(\'' + photoPreview + '\');'">
</span>
</div>
<x-jet-secondary-button class="mt-2 mr-2" type="button" x-on:click.prevent="$refs.photo.click()">
{{ __('Select A New Photo') }}
</x-jet-secondary-button>
#if ($this->user->profile_photo_path)
<x-jet-secondary-button type="button" class="mt-2" wire:click="deleteProfilePhoto">
{{ __('Remove Photo') }}
</x-jet-secondary-button>
#endif
<x-jet-input-error for="photo" class="mt-2" />
</div>
#endif
</x-slot>
<x-slot name="actions">
<x-jet-action-message class="mr-3" on="saved">
{{ __('Saved.') }}
</x-jet-action-message>
<x-jet-button wire:loading.attr="disabled" wire:target="photo">
{{ __('Save') }}
</x-jet-button>
</x-slot>
</x-jet-form-section>
I just found the problem, in form-section.blade.php you have: #props(['submit', 'test' => '']) if you don't put default value for that variable test, the variable we'll be see it like undefined

Livewire profile edit form content on the page does not change

I edited the laravel livewire profile and updated the blade in the livewire resource and the content on the page does not change. How I can update content on page after changing code? I can remove all this code, but it's still remaining on page:
<x-jet-form-section submit="updateProfileInformation">
<x-slot name="title">
{{ __('Profile Information') }}
</x-slot>
<x-slot name="description">
{{ __('Update your account\'s profile information and email address.') }}
</x-slot>
<x-slot name="form">
<!-- Profile Photo -->
#if (Laravel\Jetstream\Jetstream::managesProfilePhotos())
<div x-data="{photoName: null, photoPreview: null}" class="col-span-6 sm:col-span-4">
<!-- Profile Photo File Input -->
<input type="file" class="hidden"
wire:model="photo"
x-ref="photo"
x-on:change="
photoName = $refs.photo.files[0].name;
const reader = new FileReader();
reader.onload = (e) => {
photoPreview = e.target.result;
};
reader.readAsDataURL($refs.photo.files[0]);
" />
<x-jet-label for="photo" value="{{ __('Photo') }}" />
<!-- Current Profile Photo -->
<div class="mt-2" x-show="! photoPreview">
<img src="{{ $this->user->profile_photo_url }}" alt="{{ $this->user->name }}" class="rounded-full h-20 w-20 object-cover">
</div>
<!-- Name -->
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input id="name" type="text" class="mt-1 block w-full" wire:model.defer="state.name" autocomplete="name" />
<x-jet-input-error for="name" class="mt-2" />
</div>
</x-jet-form-section>

Contact Form error MethodNotAllowedHttpException

I'm trying to make a Contact form in Laravel
when i submit the message i got this error
i'm mostly new to laravel so i don't know why it's appearing
Routes.php
Route::post('/contact/sendmail', [
'uses' => 'ContactMessageController#postSendMessage',
'as' => 'contact.send'
]);
ContactMessageController.php
class ContactMessageController extends Controller
{
public function postSendMessage(Request $request)
{
$this->validate($request, [
'email' => 'required|email',
'name' => 'required|max:100',
'subject' => 'required|max:140',
'message' => 'required|min:10'
]);
$message = new ContactMessage();
$message->email = $request['email'];
$message->sender = $request['name'];
$message->subject = $request['subject'];
$message->body = $request['message'];
$message->save();
return redirect()->route('contact')->with(['success' => 'Message Succesfully sent']);
}
}
and
contact.blade.php
#extends ('layouts.master')
#section('title')
Contact
#endsection
#section('styles')
<link rel="stylesheet" href="{{ URL::secure('src/css/form.css') }}" />
#endsection
#section('content')
#include('includes.info-box')
<form action="{{ route('contact.send') }}" mathod="post" id="contact-form">
<div class="input-group">
<label for="name">Your Name</label>
<input type="text" name="name" id="name" value="{{ Request::old('name') }}" />
</div>
<div class="input-group">
<label for="email">Your E-Mail</label>
<input type="text" name="email" id="email" value="{{ Request::old('email') }}" />
</div>
<div class="input-group">
<label for="subject">Subject</label>
<input type="text" name="subject" id="subject" value="{{ Request::old('subject') }}" />
</div>
<div class="input-group">
<label for="message">Your Message</label>
<textarea name="message" id="message" rows="10">{{ Request::old('message') }}</textarea>
</div>
<button type="submit" class="btn">Submit Message</button>
<input type="hidden" value="{{ Session::token() }}" name="_token" />
</form>
#endsection
I cant find why i got this error
MethodNotAllowedHttpException in RouteCollection.php line 218:
Just a typo. You need to change mathod="post" to method="post".

Resources