vendredi 24 avril 2020

Laravel One to one relasionship showing property non object

I am trying basic laravel one to one relationship but there is an error. I have a table called posts(i will attach the image) and a Model Post.php. When I am trying to get the result from Route.php it is showing an error.MySql tableError message User.php

public function post()
{
    return $this->hasOne('App\Post');
}

Route.php

Route::get('/user/post', function () {
 $post = User::find(1)->post;
return $post;

});



via Chebli Mohamed

Remove parameters in the url in the Laravel controller

I am currently working with an existing project that is for maintenance. So per testing, there's a scenario that is having an error

Request-URI Too Large

The requested URL's length exceeds the capacity limit for this server.

This is error happens because our clients uses the GET method, and I don't wanna change their settings since there are other complex logic that might be affected. So I tried the approach to change the URL using javascript but still the same error. Now what I'm thinking, is it possible for us to change the URL in the controller? Like use the url current path:

url()->current();

rather than the fullpath ?

url()->full();

I badly need your help on this one, I'm stuck on this part for days already.



via Chebli Mohamed

Dynamic If else statement, value for condition is from database

is there a way to make this if else condition:

$searchUserTypeName = UserType::findOrFail(1);

if ($searchUserTypeName->name == "Captain" || $searchUserTypeName->name == "Member") {
  echo "This is Captain or Member"
} else if ($searchUserTypeName->name == "Mentor" || $searchUserTypeName->name == "Mentee") {
  echo "This is Mentor or Mentee"
}

to dynamic?

The value Captain, Member, Mentor and Mentee is from a mysql table

hope you can help

thanks



via Chebli Mohamed

Missing required parameters for route issue in laravel

In my laravel based application I have following link in my admin.blade.php

<ul class="nav nav-treeview">
              <li class="nav-item">
                <a href="" class="nav-link">
                  <i class="far fa-circle nav-icon"></i>
                  <p></p>
                </a>
              </li>

In my project I have another blade called, create.blade.php which is in the following path

views/cms/home/create.blade.php

I have a controller called, CmsHomeController.php for that blade

In CmsHomeController I have a method called create

public function create()
    {

        return view('cms.home.create'); 
    }

Once the user clicks on the above mentioned link in the admin.blade.php, user should go to the create.blade.php blade.

And in my web.php I have registered my route as follows,

Route::resource('cms.home','CmsHomeController');

But now the issue is,

When I click on that link in admin blade, I'm getting an error saying

Facade\Ignition\Exceptions\ViewException
Missing required parameters for [Route: cms.home.create] [URI: cms/{cm}/home/create]. (View: C:\xampp\htdocs\mylaravelproject\resources\views\layouts\admin.blade.php) 

In create.blade.php , I'm having just a simple form

Where am I doing wrong and what would be the correct fix?

UPDATE:

I tried running

php artisan route:list

This is what I got

enter image description here

I don't have such a param called, 'cm'..



via Chebli Mohamed

jeudi 23 avril 2020

Expected status code 200 but received 302. Failed asserting that false is true. using phpunit test in laravel 5.4

Hi i have this page which is /dno-personal/cebu-properties. I tried to run in using PHPUNIT test in my laravel. Now i created this test file below

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Response;


class DnoPersonalTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @test
     */
    public function add_cebu_properties_page()
    {


        $response = $this->get('/dno-personal/cebu-properties');
        $response->assertStatus(200);

    }
}

Now in my route file i i did not create a route for dno-personal/cebu-properties yet which i run test into my phpunit it throws an error of

Expected status code 200 but received 404.
Failed asserting that false is true.

C:\xampp\htdocs\dnogroup\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:79
C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php:24

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

which i think is okay cause i have no route yet which throws an error of 404. Now when i add into the route

Route::get('/dno-personal/cebu-properties',
        'DnoPersonalController@cebuProperties')
        ->name('dno-personal.cebuProperties');

without a method into my controller cebuProperties when i run test PHPUNIT it throws

Expected status code 200 but received 302.
Failed asserting that false is true.

C:\xampp\htdocs\dnogroup\vendor\laravel\framework\src\Illuminate\Foundation\Testing\TestResponse.php:79
C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php:24

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

it throws an error of 302. Now i want that it throws the method is not yet created instead of 302. now when i add $this->withoutExceptionHandling(); this throws me an error

PHP Fatal error:  Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling() in C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php on line 22

In DnoPersonalTest.php line 22:

  Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling()



Fatal error: Call to undefined method Tests\Feature\DnoPersonalTest::withoutExceptionHandling() in C:\xampp\htdocs\dnogroup\tests\Feature\DnoPersonalTest.php on line 22

it cannot see the $this->withoutExceptionHandling(); can someone help me figured this out? Any help is muchly appreciated. TIA



via Chebli Mohamed

Laravel 7.0 project run Without PHP artisan serve

I have issue in this., I change server.php in root folder to index.php and .htaccess file in public folder move to root folder But even though i am getting This page isn’t working HTTP ERROR 500



via Chebli Mohamed

Change the currently used URL in PHP Laravel

I am currently working with an existing project that is for maintenance. So per testing, there's a scenario that is having an error

Request-URI Too Large

The requested URL's length exceeds the capacity limit for this server.

This is because it uses the HTTP GET method. Now, I tried to update it using a POST method instead but I failed to do it because of some other constraints. Now, is it possible to get the filters from the url in my controller side and use it as my new URL instead? Like currently this is how my url looks like :

https://my-domain/mypage?filter%5Bstatus_id%5D=2&filter%5Bcompany_id%5D%5B0%5D=......

this could still be longer depending on the filtered data. Is it possible for my url to be like

https://my-domain/mypage

Is this possible to remove the filter data? I can get the full URL and the URL in my controller but I just don't know how I'm going to implement it. Looking forward for someone to help me figure this out.



via Chebli Mohamed