lundi 1 mai 2017

Laravel Mass Assignment Error

I have tried multiple troubleshooting steps on this site about this error. Already added a $fillable and updated my composer but I am still getting the same error here is a screen shot of the error that I am experiencing and the codes for my Model.php

Laravel Error message

Model Codes



via Chebli Mohamed

How to check and uncheck checkbox automatically

I'm doing a page where i can display a list of data with checkboxes. then i will automatically check with database and if the data is there, the checkbox will be checked. What i want to do now is, lets say the checkbox is checked automatically, andwhen i unchecked it myself and save, it will update the data in database.

Example of my coding:

<form action="" method="get">
    <input name="_token" type="hidden" value=""/>
    @foreach($var as $v)
    <table style="margin-left:20px;">
         <caption></caption>
         @foreach($name as $n)
         <?php if($n->module_groupname == $v->module_groupname){?>
         <tr><td width="80"><input type="checkbox" name="module_code[]" value=""
         <?php foreach($priv as $p){
         if($p->user_id == $d->id){
             if($p->module_code == $n->module_code){
                 echo 'checked="checked"';
             }
         }
     }?>></td>
     <td width="150"></td>
     <td width="200"></td>
    </tr> <?php } ?>
 @endforeach
</table>
<hr>
@endforeach
<input type="submit" value="select" class="btn btn-s btn-success">
</form>

i've been advised to lookup for PLUS statement oracle but i couldnt find how



via Chebli Mohamed

TinyMCE and Laravel 5.3 TokenMismatchException

I'm trying to implement TinyMCE image uploads, using Laravel 5.3 on the server side:

here is my JS for TinyMCE, which is currently in a blade template:

<script src=""></script>
<script>
    tinymce.init({
        selector: 'textarea',
        plugins: [
            "advlist autolink lists link image charmap print preview hr anchor pagebreak",
            "searchreplace wordcount visualblocks visualchars code fullscreen",
            "insertdatetime media nonbreaking save table contextmenu directionality",
            "emoticons template paste textcolor colorpicker textpattern"
        ],
        toolbar: "insertfile undo redo | styleselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image media",
        relative_urls: false,

        image_title: true,

        automatic_uploads: true,

        images_upload_url: '/discussions/save_images/',

        file_picker_types: 'image',

        images_upload_credentials: true,

        file_picker_callback: function(cb, value, meta) {
            var input = document.createElement('input');
            input.setAttribute('type', 'file');
            input.setAttribute('accept', 'image/*');
            input.onchange = function() {
                var file = this.files[0];
                var id = 'blobid' + (new Date()).getTime();
                var blobCache = tinymce.activeEditor.editorUpload.blobCache;
                var blobInfo = blobCache.create(id, file);
                blobCache.add(blobInfo);
                cb(blobInfo.blobUri(), { title: file.name });
            };
            input.click();
        }
    });
</script>

My route to handle the POST request made by TinyMCE:

Route::post("/discussions/save_images/", 'Discussion\DiscussionController@saveImages');

My action to handle each upload:

public function saveImages(Request $request) {
    $filename = sha1(uniqid()).'.'.request()->file("name")->getClientOriginalExtension();
    $request->file("name")->move('/images/discussions/', $filename);
    return json_encode(["location"=>"/images/discussions/".$filename]);
}

Laravel throws a TokenMismatchException. How can I pass the CSRF token into the POST request that TinyMCE makes?

I know that in general this token can be accessed in a template via , but I'm not sure about the correct configuration in regards to TinyMCE.



via Chebli Mohamed

Updating multiple rows on same page, Laravel 5.2

I'm displaying the entire contents of a DB table onto a single page in a similar format to what looks like an Excel spreadsheet.

I need to be able to save all the rows that the user has edited, my Controller function to handle the save is not quite right and I'm not sure whats the most efficient way to get this to work.

I think adding a loop inside of the function and adding a count, so that the 10 same input fields that belong to different ID's/Rows would save respectively.

Any help is greatly appreciated. Thanks.

Controller:

protected function CreateUpdateCompetitorRates(Request $request)

{
    $competitor_bluestone_rates_mrfs = Competitor_Bluestone_rates::where('id', $request->id)->first();

    $competitor_bluestone_rates_mrfs->id = $request->id;
    $competitor_bluestone_rates_mrfs->Portfolio = (empty($request->Portfolio)) ? '' : $request->Portfolio;
    $competitor_bluestone_rates_mrfs->Product_Type = (empty($request->Product_Type)) ? '' : $request->Product_Type;
    $competitor_bluestone_rates_mrfs->Doc_Type = (empty($request->Doc_Type)) ? '' : $request->Doc_Type;
    $competitor_bluestone_rates_mrfs->Product_Name = (empty($request->Product_Name)) ? '' : $request->Product_Name;
    $competitor_bluestone_rates_mrfs->LVR_Up_To = (empty($request->LVR_Up_To)) ? '' : $request->LVR_Up_To;
    $competitor_bluestone_rates_mrfs->Rate = (empty($request->Rate)) ? '' : $request->Rate;
    $competitor_bluestone_rates_mrfs->Mortgage_Risk_Fee = (empty($request->Mortgage_Risk_Fee)) ? '' : $request->Mortgage_Risk_Fee;

    $competitor_bluestone_rates_mrfs->save();

} 

View

        @foreach($competitor_la_trobe_rates_mrfs as $competitor_la_trobe_rates_mrf)
            <tr>
                                   <input type="text" value="" readonly>
                <input type="text" id="Portfolio_" value="">
                <input type="text" id="Product_Type_" value="Product_Type">
                <input type="text" id="Doc_Type_" value="">
                <input type="text" id="Product_Name_" value="">
                <input type="text" id="LVR_Up_To" value="">
                <input type="text" id="Rate_" value="">
                <input type="text" id="Mortgage_Risk_fee_" value="">


            </tr>
        @endforeach



via Chebli Mohamed

On Laravel I'm trying to update an image and It works, but when I decide not to add a new file I get this error: General error: 1364

I'm trying to update a table and it works perfectly but when I try to update whiteout changing the file it fails and gives me this error.

The error:

SQLSTATE[HY000]: General error: 1364 Field 'poster' doesn't have a default value (SQL: insert into patrocinadores (nombre, link, categoria, updated_at, created_at) values (Patrocinadores, http://ift.tt/2ppC6Sm, 3, 2017-05-01 23:44:44, 2017-05-01 23:44:44))

Here is my controller store()

public function store(Request $request){

    $this->validate(request(), [

        'nombre' => 'required',
        'link' => 'required',
        'poster' => 'image|image_size:<=1000',
        'categoria' =>'required',
    ]);

    $patrocinadores = new Patrocinadores;

    $patrocinadores->nombre = request('nombre');
    $patrocinadores->link = request('link');
    $newFoto=request()->file('poster');
    if($newFoto){
      $name=$newFoto. '.' . $newFoto->getClientOriginalExtension();
      $patrocinadores->poster = $newFoto->move('./uploads/', $name);
    }
    $patrocinadores->categoria = request('categoria');

    $patrocinadores->save();


    return redirect('/patrocinadores'); }

HTML:

  <div class="form-control-file">
    <label for="poster">Póster</label>

      <input type="file" class="form-control-file" id="poster" name="poster" aria-describedby="fileHelp"  accept="image/*" >

      <br>
      <div id="preview"><img src="" ></div>


    </div>



via Chebli Mohamed

Laravel 5: Validating Edits In-Place

I had previously asked a question here in regards for setting up in-place editing with Laravel 5 and AJAX. I hadn't updated it because I had managed offline to figure out what was wrong with it.

While the table is capable of editing user rows in-place, I'm now trying to add validation on top of it, intending on utilizing Laravel's built-in validator. However, for some reason it doesn't seem to be working. When I try to pass in the failed validator back through the JSON, it spits out every possible error I'm checking for. It's as if the validator is treating every input as empty, which doesn't make sense, as the rest of the function is clearly taking in the inputs as intended.

The code snippets in my previous question are still mostly relevant, but there have been updates to HomeController.php, as can be seen below:

public function updateTable(Users $users){

    $user = request()->input('user');

    $first_name = request()->input('first_name');
    $last_name = request()->input('last_name');

    $validator = Validator::make(request()->all(), [
      'firstName'                     => 'required|alpha',
      'lastName'                      => 'required|alpha'
    ], [
      'firstName.required'            => 'You need to give a first name!',
      'firstName.alpha'               => 'A first name can only contain letters!',
      'lastName.required'             => 'You need to give a last name!',
      'lastName.alpha'                => 'A last name can only contain letters!'
    ]);

    if ($validator->fails()) {

      return response()->json($validator, 404);

    }

    $employees->editUser($user, $first_name, $last_name);

    return response()->json($user);

}



via Chebli Mohamed

Trying to get property of non-object Laravel 5.3, profile.blade.php.

I am trying to show the users profile information who are logged in,I have two tables one is Message and other one is User, when user login it see his\her profile, but there is an error.

profile.blade:

<table class="table table-bordered">
    <thead>
        <tr>
            <th>
                Name
            </th>
            <th>
                Email
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach($users as $usr)
        <tr>
            <td>
                
            </td>
            <td>
                
            </td>
        </tr>
        @endforeach
    </tbody>

Controller:

public function GetUserInfo($id){
    $user = DB::table('users')
        ->join('messages', 'messages.user_id', '=', 'users.id')
        ->where('users.id', '=', $id)
        ->get();
    if($user->count()){
        $user = $user->first();
        return view('profile')->with('users',$user);
    }
}

Model:

    class Messages extends Model
{
    protected $table = 'messages';
    protected $fillable = [
        'message','user_id'
    ];

    public function user()
    {
        return $this->hasOne('App\User', 'id', 'user_id');
    }
}



via Chebli Mohamed