samedi 2 octobre 2021

Delete function in Laravel

I'm stuck in this function. I want to delete a document which has a many to many relationship between library and client. I think I'm doing something entirely wrong here.

Documents have id, id_client, id_biblio and file

I have no idea how to delete the file from the clientController

public function delete($id)
{
    $client_docs = ClientDocument::find($id);
    $client_docs->delete();

    return redirect()->back()->with('success', 'client has been Deleted');
}


via Chebli Mohamed

laravel controller : update function for multiple dynamic rows

invoice_service Table - Invoice Table

the edit function works well in EditInvoice blade and the specific rows are displayed with their value. Example : if I choose to edit invoice N17 , in EditInvoice blade 2 results will be appear (see table image).

for the next step , I need to update this 2 results value(invoice services). I try many ways in update function but didnt work.

  • store function : works well
public function store(Request $request) : 
...
.....
$designation = $request->designation;
$QTE = $request->QTE;
$PU = $request->PU;
$total = $request->total;

$c = count($request->designation);
for ( $i=0; $i< $c; ++$i) { //get all rows(dynamic add/remove)
   $invoice_number = $request->invoice_number;
   $invoice_id = Invoices::latest()->first()->id;
   $services = new invoice_services();
   $services->invoice_number = $invoice_number;
   $services->invoice_id = $invoice_id;
   $services->designation = $designation[$i];
   $services->QTE = $QTE[$i];
   $services->PU = $PU[$i];
   $services->total = $QTE[$i] * $PU[$i];
   $services->save();
}   
..
...
  • Update function : need editing
public function update(Request $request )
    { 
//Here we need to update the rows !! I tried many ways but it didnt work
}
  • edit function : works well
public function edit($id) { 
$invoices = invoices::where('id', $id)->first();
$services = invoice_services::where('invoice_id' , $id)->get();
$clients = clients::all();
return view('invoices.edit_invoice', compact('clients', 'services' , 'invoices'));
}
  • edit_invoice view : works well
@foreach ($services as $service) 
       <tr>
   <td><input type="text" name="designation[]"  value="" class="form-control"></td>
  <td><input type="text" name="QTE[]" id="QTE'+incrementalRow+'" value="" class="form-control"></td>
   <td><input type="text" name="PU[]" id="PU'+incrementalRow+'" value="" class="form-control"></td>
   <td><input type="text" class="form-control form-control-lg" name="total[]" value="" id="totali'+incrementalRow+'" readonly ></td>
       </tr>
       @endforeach


via Chebli Mohamed

Laravel vendor/autoload.php missing

It's my first time with Laravel. I've install Laravel application via composer but when i try to run artisan command (like: php artisan serve) a fatal error is shown:

Fatal error: require(): Failed opening required 'C:\Apache24\htdocs\app-test\bootstrap/../vendor/autoload.php' (include_path='.;C:\php\pear') in C:\Apache24\htdocs\app-test\bootstrap\autoload.php on line 17

I check in the vendor folder and autoload.php file doesn't exist. I try tu run:

composer update --no-scripts

or

composer install

but nothing is changed.

I use laravel v5.4.30 and PHP7.

Why composer doesn't create autoload.php file in vendor folder?



via Chebli Mohamed

vendredi 1 octobre 2021

Laravel 5.4 + WP fatal error on only some routes

I inherited a Laravel + Wordpress integrated website and had to migrate to a new server. The Wordpress website appears to work fine, and some Laravel routes work, but others do not.

For example, on the website homepage, we get the following error:

Call to a member function route() on string

in Kernel.php line 231
at Kernel->gatherRouteMiddleware()
in Kernel.php line 204
at Kernel->terminateMiddleware()
in Kernel.php line 189
at Kernel->terminate()
in index.php line 61

On other routes, such as /donor/ and /parent/, the browser simply redirects to the homepage.

Route::group(['namespace' => 'Donor', 'prefix' => 'donor'], function () {

    Route::group(['prefix' => 'application'], function () {
        Route::get('/', [
            'middleware' => ['guest'],
            'as'         => 'donor.initial-application',
            'uses'       => 'InitialApplicationController@showApplication',
        ]);
        Route::post('/', [
            'middleware' => ['guest'],
            'uses'       => 'InitialApplicationController@saveApplication',
        ]);
        Route::get('submitted', [
            'as'         => 'donor.submitted',
            'uses'       => 'InitialApplicationController@applicationSubmitted',
        ]);
        Route::get('rejected', [
            'as'         => 'donor.rejected',
            'uses'       => 'InitialApplicationController@applicationRejected',
        ]);
    });

    Route::group(['middleware' => 'auth.donor'], function () {
        Route::get('/', [
            'as'   => 'donor.dashboard',
            'uses' => 'DashboardController@dashboard',
        ]);
        Route::get('personal', [
            'as'   => 'donor.application.personal',
            'uses' => 'PersonalController@showForm',
        ]);
        
        .... etc ......


And on other routes, such as /account/ or /admin/ and its children, load without issue.

Route::group(['middleware' => 'auth', 'namespace' => 'Account', 'prefix' => 'account'], function () {
    Route::get('/', [
        'as'   => 'account',
        'uses' => 'ProfileController@profile',
    ]);
    Route::get('edit', [
        'middleware' => 'profile',
        'as'         => 'account.edit',
        'uses'       => 'ProfileController@edit',
    ]);
    Route::post('edit', [
        'middleware' => 'profile',
        'uses'       => 'ProfileController@update',
    ]);
    Route::get('password', [
        'as'   => 'account.password',
        'uses' => 'PasswordController@edit',
    ]);
    Route::post('password', [
        'uses' => 'PasswordController@update',
    ]);

});

I have tried running commands: php artisan optimize php artisan cache:clear php artisan route:clear and did not resolve the issue.

Laravel resides in /

Wordpress index and config resides in /public/

Wordpress core resides in /public/wp/

Another interesting note, at first Wordpress was encountering an endless redirect loop. I was able to fix that by commenting out the following lines in .htaccess:

    # Redirect Trailing Slashes If Not A Folder...
    #RewriteCond %{REQUEST_FILENAME} !-d
    #RewriteRule ^(.*)/$ /$1 [L,R=301]

I've tried reverting this and it does not seem to affect my problem above. Not experienced in Laravel so I am unsure what else to try.



via Chebli Mohamed

string validation max 15000 characters not working

I am trying to validate a textarea with max 15000 characters validation rule.

This is the rule I created:

array:1 [▼
    "text" => "required|string|max:15000"
]

Somewhere before I concat two inputs in this way:

$request->merge(['text' => $request->input('text') . PHP_EOL . PHP_EOL . $request->input('sign']);

The code does not pass the validation even if the sum of text input and sign input is (according to this website) is 14988.
To calculate it, I just past the textarea value to the website and I checked the characters count.



via Chebli Mohamed

How to Decode Json object in laravel and apply foreach loop on that in blade file laravel5.5

I am inserting data field "data_all_tab" of children elements in json format in database. I need to decode that data and foreach loop to inside element "data_all_tab" . need to display that in blade file

public function check(Request $request,$label)
    {
        $label = Label::where("name",$label)->get();
        $types = Type::whereIn('id',$type_check)->get();
        return view('indocount.check',['label'=>$label],['types'=>$types]); 

    }

In label i am getting below output in blade file

{"id":23,"name":"TEST","image":"626547997.png","seltype":"2,5,12,13,15","created_at":"2021-10-01 17:18:49","updated_at":"2021-10-01 17:53:01","image2":"1717423306.png","banner":"1586871240.png","image3":"209824040.png","image4":"209824040.png","image5":"1097836203.png","image6":"124898669.png","data_all_tab":"{\r\n \"disableOneColumnMode\": true,\r\n \"children\": [\r\n {\r\n \"x\": 0,\r\n \"y\": 0,\r\n \"w\": 12,\r\n \"h\": 4,\r\n \"content\": \"6\"\r\n },\r\n {\r\n \"x\": 0,\r\n \"y\": 4,\r\n \"w\": 12,\r\n \"h\": 4,\r\n \"content\": \"1\"\r\n },\r\n {\r\n \"x\": 0,\r\n \"y\": 8,\r\n \"w\": 12,\r\n \"h\": 4,\r\n \"content\": \"2\"\r\n },\r\n {\r\n \"x\": 0,\r\n \"y\": 12,\r\n \"w\": 12,\r\n \"h\": 4,\r\n \"content\": \"3\"\r\n },\r\n {\r\n \"x\": 0,\r\n \"y\": 16,\r\n \"w\": 12,\r\n \"h\": 4,\r\n \"content\": \"4\"\r\n },\r\n {\r\n \"x\": 0,\r\n \"y\": 20,\r\n \"w\": 12,\r\n \"h\": 4,\r\n \"content\": \"5\"\r\n }\r\n ]\r\n}"}

I want get data of "data_all_tab" of children elements inside blade and apply foreach loop then display all elements in blade file.

If anyone have idea then let me know



via Chebli Mohamed

Laravel - Element displaying twice while looping inside a condition

This is the example while looping

despite having a condition and relation many too many it seems that the case

  @foreach (\App\ClientDocument::where('id_client', $clients->id)->get() as $item)    
                  @if (\App\Document::where('id',$item->id_document)->get())
                  <a class="img-fluid " href="" download="" alt="" name="download"  > Telecharger </a>
            
                  @endif  
               @endforeach


via Chebli Mohamed